File tree 3 files changed +51
-10
lines changed
3 files changed +51
-10
lines changed Original file line number Diff line number Diff line change @@ -15,25 +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
24
- busyWorkers uint64
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
+ metric Metric
25
25
}
26
26
27
27
func (s * Consumer ) incBusyWorker () {
28
- atomic . AddUint64 ( & s . busyWorkers , 1 )
28
+ s . metric . IncBusyWorker ( )
29
29
}
30
30
31
31
func (s * Consumer ) decBusyWorker () {
32
- atomic . AddUint64 ( & s . busyWorkers , ^ uint64 ( 0 ) )
32
+ s . metric . DecBusyWorker ( )
33
33
}
34
34
35
35
func (s * Consumer ) BusyWorkers () uint64 {
36
- return atomic . LoadUint64 ( & s . busyWorkers )
36
+ return s . metric . BusyWorkers ( )
37
37
}
38
38
39
39
// BeforeRun run script before start worker
@@ -168,6 +168,7 @@ func NewConsumer(opts ...Option) *Consumer {
168
168
stop : make (chan struct {}),
169
169
logger : o .logger ,
170
170
runFunc : o .fn ,
171
+ metric : o .metric ,
171
172
}
172
173
173
174
return w
Original file line number Diff line number Diff line change
1
+ package queue
2
+
3
+ import "sync/atomic"
4
+
5
+ // Metric interface
6
+ type Metric interface {
7
+ IncBusyWorker ()
8
+ DecBusyWorker ()
9
+ BusyWorkers () uint64
10
+ }
11
+
12
+ type metric struct {
13
+ busyWorkers uint64
14
+ }
15
+
16
+ func newMetric () Metric {
17
+ return & metric {}
18
+ }
19
+
20
+ func (m * metric ) IncBusyWorker () {
21
+ atomic .AddUint64 (& m .busyWorkers , 1 )
22
+ }
23
+
24
+ func (m * metric ) DecBusyWorker () {
25
+ atomic .AddUint64 (& m .busyWorkers , ^ uint64 (0 ))
26
+ }
27
+
28
+ func (m * metric ) BusyWorkers () uint64 {
29
+ return atomic .LoadUint64 (& m .busyWorkers )
30
+ }
Original file line number Diff line number Diff line change 12
12
defaultTimeout = 60 * time .Minute
13
13
defaultNewLogger = NewLogger ()
14
14
defaultFn = func (context.Context , QueuedMessage ) error { return nil }
15
+ defaultMetric = newMetric ()
15
16
)
16
17
17
18
// Option for queue system
@@ -38,6 +39,13 @@ func WithLogger(l Logger) Option {
38
39
}
39
40
}
40
41
42
+ // WithMetric set custom Metric
43
+ func WithMetric (m Metric ) Option {
44
+ return func (q * Options ) {
45
+ q .metric = m
46
+ }
47
+ }
48
+
41
49
// WithWorker set custom worker
42
50
func WithWorker (w Worker ) Option {
43
51
return func (q * Options ) {
@@ -66,6 +74,7 @@ type Options struct {
66
74
queueSize int
67
75
worker Worker
68
76
fn func (context.Context , QueuedMessage ) error
77
+ metric Metric
69
78
}
70
79
71
80
func NewOptions (opts ... Option ) * Options {
@@ -76,6 +85,7 @@ func NewOptions(opts ...Option) *Options {
76
85
logger : defaultNewLogger ,
77
86
worker : nil ,
78
87
fn : defaultFn ,
88
+ metric : defaultMetric ,
79
89
}
80
90
81
91
// Loop through each option
You can’t perform that action at this time.
0 commit comments