@@ -26,9 +26,9 @@ import (
26
26
var ErrProcessorStopped = errors .New ("processor is stopped" )
27
27
28
28
// Processor manages the queue of items and processes them at the correct time.
29
- type Processor [T queueable ] struct {
29
+ type Processor [K comparable , T queueable [ K ] ] struct {
30
30
executeFn func (r T )
31
- queue queue [T ]
31
+ queue queue [K , T ]
32
32
clock kclock.Clock
33
33
lock sync.Mutex
34
34
wg sync.WaitGroup
@@ -40,10 +40,10 @@ type Processor[T queueable] struct {
40
40
41
41
// NewProcessor returns a new Processor object.
42
42
// executeFn is the callback invoked when the item is to be executed; this will be invoked in a background goroutine.
43
- func NewProcessor [T queueable ] (executeFn func (r T )) * Processor [T ] {
44
- return & Processor [T ]{
43
+ func NewProcessor [K comparable , T queueable [ K ]] (executeFn func (r T )) * Processor [K , T ] {
44
+ return & Processor [K , T ]{
45
45
executeFn : executeFn ,
46
- queue : newQueue [T ](),
46
+ queue : newQueue [K , T ](),
47
47
processorRunningCh : make (chan struct {}, 1 ),
48
48
stopCh : make (chan struct {}),
49
49
resetCh : make (chan struct {}, 1 ),
@@ -52,14 +52,14 @@ func NewProcessor[T queueable](executeFn func(r T)) *Processor[T] {
52
52
}
53
53
54
54
// WithClock sets the clock used by the processor. Used for testing.
55
- func (p * Processor [T ]) WithClock (clock kclock.Clock ) * Processor [T ] {
55
+ func (p * Processor [K , T ]) WithClock (clock kclock.Clock ) * Processor [K , T ] {
56
56
p .clock = clock
57
57
return p
58
58
}
59
59
60
60
// Enqueue adds a new item to the queue.
61
61
// If a item with the same ID already exists, it'll be replaced.
62
- func (p * Processor [T ]) Enqueue (r T ) error {
62
+ func (p * Processor [K , T ]) Enqueue (r T ) error {
63
63
if p .stopped .Load () {
64
64
return ErrProcessorStopped
65
65
}
@@ -79,7 +79,7 @@ func (p *Processor[T]) Enqueue(r T) error {
79
79
}
80
80
81
81
// Dequeue removes a item from the queue.
82
- func (p * Processor [T ]) Dequeue (key string ) error {
82
+ func (p * Processor [K , T ]) Dequeue (key K ) error {
83
83
if p .stopped .Load () {
84
84
return ErrProcessorStopped
85
85
}
@@ -99,7 +99,7 @@ func (p *Processor[T]) Dequeue(key string) error {
99
99
100
100
// Close stops the processor.
101
101
// This method blocks until the processor loop returns.
102
- func (p * Processor [T ]) Close () error {
102
+ func (p * Processor [K , T ]) Close () error {
103
103
defer p .wg .Wait ()
104
104
if p .stopped .CompareAndSwap (false , true ) {
105
105
// Send a signal to stop
@@ -114,7 +114,7 @@ func (p *Processor[T]) Close() error {
114
114
115
115
// Start the processing loop if it's not already running.
116
116
// This must be invoked while the caller has a lock.
117
- func (p * Processor [T ]) process (isNext bool ) {
117
+ func (p * Processor [K , T ]) process (isNext bool ) {
118
118
// Do not start a loop if it's already running
119
119
select {
120
120
case p .processorRunningCh <- struct {}{}:
@@ -140,7 +140,7 @@ func (p *Processor[T]) process(isNext bool) {
140
140
}
141
141
142
142
// Processing loop.
143
- func (p * Processor [T ]) processLoop () {
143
+ func (p * Processor [K , T ]) processLoop () {
144
144
defer func () {
145
145
// Release the channel when exiting
146
146
<- p .processorRunningCh
@@ -209,7 +209,7 @@ func (p *Processor[T]) processLoop() {
209
209
}
210
210
211
211
// Executes a item when it's time.
212
- func (p * Processor [T ]) execute (r T ) {
212
+ func (p * Processor [K , T ]) execute (r T ) {
213
213
// Pop the item now that we're ready to process it
214
214
// There's a small chance this is a different item than the one we peeked before
215
215
p .lock .Lock ()
0 commit comments