File tree Expand file tree Collapse file tree 7 files changed +63
-11
lines changed Expand file tree Collapse file tree 7 files changed +63
-11
lines changed Original file line number Diff line number Diff line change @@ -15,12 +15,25 @@ var errMaxCapacity = errors.New("max capacity reached")
15
15
16
16
// Worker for simple queue using channel
17
17
type Consumer struct {
18
- taskQueue chan QueuedMessage
19
- runFunc func (context.Context , QueuedMessage ) error
20
- stop chan struct {}
21
- logger Logger
22
- stopOnce sync.Once
23
- stopFlag int32
18
+ taskQueue chan QueuedMessage
19
+ runFunc func (context.Context , QueuedMessage ) error
20
+ stop chan struct {}
21
+ logger Logger
22
+ stopOnce sync.Once
23
+ stopFlag int32
24
+ busyWorkers uint64
25
+ }
26
+
27
+ func (s * Consumer ) incBusyWorker () {
28
+ atomic .AddUint64 (& s .busyWorkers , 1 )
29
+ }
30
+
31
+ func (s * Consumer ) decBusyWorker () {
32
+ atomic .AddUint64 (& s .busyWorkers , ^ uint64 (0 ))
33
+ }
34
+
35
+ func (s * Consumer ) BusyWorkers () uint64 {
36
+ return atomic .LoadUint64 (& s .busyWorkers )
24
37
}
25
38
26
39
// BeforeRun run script before start worker
@@ -39,7 +52,11 @@ func (s *Consumer) handle(job Job) error {
39
52
panicChan := make (chan interface {}, 1 )
40
53
startTime := time .Now ()
41
54
ctx , cancel := context .WithTimeout (context .Background (), job .Timeout )
42
- defer cancel ()
55
+ s .incBusyWorker ()
56
+ defer func () {
57
+ cancel ()
58
+ s .decBusyWorker ()
59
+ }()
43
60
44
61
// run the job
45
62
go func () {
Original file line number Diff line number Diff line change @@ -357,3 +357,30 @@ func TestTaskJobComplete(t *testing.T) {
357
357
}
358
358
assert .Equal (t , context .DeadlineExceeded , w .handle (job ))
359
359
}
360
+
361
+ func TestBusyWorkerCount (t * testing.T ) {
362
+ job := Job {
363
+ Timeout : 200 * time .Millisecond ,
364
+ Task : func (ctx context.Context ) error {
365
+ time .Sleep (100 * time .Millisecond )
366
+ return nil
367
+ },
368
+ }
369
+
370
+ w := NewConsumer ()
371
+
372
+ assert .Equal (t , uint64 (0 ), w .BusyWorkers ())
373
+ go func () {
374
+ assert .NoError (t , w .handle (job ))
375
+ }()
376
+ go func () {
377
+ assert .NoError (t , w .handle (job ))
378
+ }()
379
+
380
+ time .Sleep (50 * time .Millisecond )
381
+ assert .Equal (t , uint64 (2 ), w .BusyWorkers ())
382
+ time .Sleep (100 * time .Millisecond )
383
+ assert .Equal (t , uint64 (0 ), w .BusyWorkers ())
384
+
385
+ assert .NoError (t , w .Shutdown ())
386
+ }
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ func TestNewQueue(t *testing.T) {
29
29
assert .NotNil (t , q )
30
30
31
31
q .Start ()
32
+ assert .Equal (t , uint64 (0 ), w .BusyWorkers ())
32
33
q .Shutdown ()
33
34
q .Wait ()
34
35
}
@@ -48,6 +49,7 @@ func TestWorkerNum(t *testing.T) {
48
49
q .Start ()
49
50
time .Sleep (20 * time .Millisecond )
50
51
assert .Equal (t , 4 , q .Workers ())
52
+ assert .Equal (t , uint64 (0 ), w .BusyWorkers ())
51
53
q .Shutdown ()
52
54
q .Wait ()
53
55
}
@@ -203,6 +205,7 @@ func TestQueueTaskJob(t *testing.T) {
203
205
return nil
204
206
}))
205
207
time .Sleep (50 * time .Millisecond )
208
+ assert .Equal (t , uint64 (0 ), w .BusyWorkers ())
206
209
q .Shutdown ()
207
210
assert .Equal (t , ErrQueueShutdown , q .QueueTask (func (ctx context.Context ) error {
208
211
return nil
Original file line number Diff line number Diff line change @@ -16,6 +16,8 @@ type Worker interface {
16
16
Capacity () int
17
17
// Usage is how many message in queue
18
18
Usage () int
19
+ // BusyWorkers return count of busy worker currently
20
+ BusyWorkers () uint64
19
21
}
20
22
21
23
// QueuedMessage ...
Original file line number Diff line number Diff line change @@ -12,3 +12,4 @@ func (w *emptyWorker) Shutdown() error { return nil }
12
12
func (w * emptyWorker ) Queue (job QueuedMessage ) error { return nil }
13
13
func (w * emptyWorker ) Capacity () int { return 0 }
14
14
func (w * emptyWorker ) Usage () int { return 0 }
15
+ func (w * emptyWorker ) BusyWorkers () uint64 { return uint64 (0 ) }
Original file line number Diff line number Diff line change @@ -37,5 +37,6 @@ func (w *messageWorker) Queue(job QueuedMessage) error {
37
37
return errors .New ("max capacity reached" )
38
38
}
39
39
}
40
- func (w * messageWorker ) Capacity () int { return cap (w .messages ) }
41
- func (w * messageWorker ) Usage () int { return len (w .messages ) }
40
+ func (w * messageWorker ) Capacity () int { return cap (w .messages ) }
41
+ func (w * messageWorker ) Usage () int { return len (w .messages ) }
42
+ func (w * messageWorker ) BusyWorkers () uint64 { return uint64 (0 ) }
Original file line number Diff line number Diff line change @@ -38,5 +38,6 @@ func (w *taskWorker) Queue(job QueuedMessage) error {
38
38
return errors .New ("max capacity reached" )
39
39
}
40
40
}
41
- func (w * taskWorker ) Capacity () int { return cap (w .messages ) }
42
- func (w * taskWorker ) Usage () int { return len (w .messages ) }
41
+ func (w * taskWorker ) Capacity () int { return cap (w .messages ) }
42
+ func (w * taskWorker ) Usage () int { return len (w .messages ) }
43
+ func (w * taskWorker ) BusyWorkers () uint64 { return uint64 (0 ) }
You can’t perform that action at this time.
0 commit comments