From 073873a6738ab714d69a7bce98ac8adc3b237720 Mon Sep 17 00:00:00 2001 From: Mark Dickson Jr Date: Wed, 5 Feb 2020 22:52:42 -0500 Subject: [PATCH] Removed sparse utilitization.go file --- dispatcher.go | 10 ++++++++++ split.go | 5 ++--- utilization.go | 12 ------------ worker.go | 24 ++++++++++++------------ 4 files changed, 24 insertions(+), 27 deletions(-) delete mode 100644 utilization.go diff --git a/dispatcher.go b/dispatcher.go index e680bdb..e0bb9f2 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -5,6 +5,16 @@ import ( "time" ) +type Utilization struct { + ByWorker []WorkerUtilization + PercentUtilization float32 +} + +type WorkerUtilization struct { + PercentUtilization float32 + Id int +} + func NewDispatcher( maxJobQueueSize, maxWorkers int, diff --git a/split.go b/split.go index 1bbe3ba..77c448b 100644 --- a/split.go +++ b/split.go @@ -2,9 +2,8 @@ package work import "math" -// Split will make "parts" evenly-sized slices, with the last slice -// smaller than the others in the event len(data) is not divisible by -// parts +// Split will make "parts" evenly-sized slices, with the last slice smaller than the others in the event len(data) is +// not divisible by parts func Split(data []interface{}, parts int) [][]interface{} { if parts < 2 { return [][]interface{}{ data } diff --git a/utilization.go b/utilization.go deleted file mode 100644 index c6c84c7..0000000 --- a/utilization.go +++ /dev/null @@ -1,12 +0,0 @@ -package work - -type Utilization struct { - ByWorker []WorkerUtilization - PercentUtilization float32 -} - -type WorkerUtilization struct { - PercentUtilization float32 - Id int -} - diff --git a/worker.go b/worker.go index 7ed8c2c..7a301dd 100644 --- a/worker.go +++ b/worker.go @@ -9,7 +9,8 @@ type Job struct { Context interface{} } -// provides a mechanism for shared data and context across calls to the work function +// Context provides a mechanism for shared data and context across calls to the work function, as well as to provide +// the id of the worker for tracking/logging purposes to the job functions type Context struct { Data interface{} Id int @@ -42,6 +43,7 @@ type Worker struct { workerContext Context } +// GetRunningCount returns the total number of running workers this worker should count as (either 1 or 0) func (w Worker) GetRunningCount() int32 { return w.runningCount } @@ -51,17 +53,17 @@ func (w Worker) GetRunTimeNs() int64 { return time.Now().Sub(w.startTime).Nanoseconds() } -// how long the worker spent doing things across all runs +// GetTotalActiveTimeNs returns how long the worker spent doing things across all runs func (w Worker) GetTotalActiveTimeNs() int64 { return w.totalProcessingTimeNs } -// how long the worker spent doing nothing across all runs +// GetTotalIdleTimeNs returns how long the worker spent doing nothing across all runs func (w Worker) GetTotalIdleTimeNs() int64 { return time.Now().Sub(w.startTime).Nanoseconds() - w.GetTotalActiveTimeNs() } -// how much of the time the worker spent doing things across all runs, by % +// GetPercentUtilization returns how much of the time the worker spent doing things across all runs, by % func (w Worker) GetPercentUtilization() float32 { if w.GetRunTimeNs() == 0 { return 0.0 @@ -80,22 +82,22 @@ func (w *Worker) start() { case job := <-w.jobQueue: workFnStart := time.Now() atomic.AddInt32(&w.runningCount, 1) - _, _ = w.log("worker%d: started job", w.workerContext.Id) + w.log("worker%d: started job", w.workerContext.Id) err := w.workFn(job, &w.workerContext) atomic.AddInt32(&w.runningCount, -1) atomic.AddInt64(&w.totalProcessingTimeNs, time.Now().Sub(workFnStart).Nanoseconds()) if err != nil { - _, _ = w.log("worker%d: had error: %s", w.workerContext.Id, err.Error()) + w.log("worker%d: had error: %s", w.workerContext.Id, err.Error()) w.error(job, &w.workerContext, err) } // nil out data to clue GC job.Context = nil - _, _ = w.log("worker%d: completed job", w.workerContext.Id) + w.log("worker%d: completed job", w.workerContext.Id) case <-w.quitChan: - _, _ = w.log("worker%d stopping", w.workerContext.Id) + w.log("worker%d stopping", w.workerContext.Id) return } } @@ -108,12 +110,10 @@ func (w Worker) stop() { }() } -func (w Worker) log(format string, a ...interface{}) (n int, err error) { +func (w Worker) log(format string, a ...interface{}) { if w.logFn != nil { - return w.logFn(format, a...) + _, _ = w.logFn(format, a...) } - - return 0, nil } func (w Worker) error(job Job, workerContext *Context, err error) {