@@ -141,7 +141,7 @@ func newDelayingQueue[T comparable](clock clock.WithTicker, q TypedInterface[T],
141
141
clock : clock ,
142
142
heartbeat : clock .NewTicker (maxWait ),
143
143
stopCh : make (chan struct {}),
144
- waitingForAddCh : make (chan * waitFor , 1000 ),
144
+ waitingForAddCh : make (chan * waitFor [ T ] , 1000 ),
145
145
metrics : newRetryMetrics (name , provider ),
146
146
}
147
147
@@ -165,15 +165,15 @@ type delayingType[T comparable] struct {
165
165
heartbeat clock.Ticker
166
166
167
167
// waitingForAddCh is a buffered channel that feeds waitingForAdd
168
- waitingForAddCh chan * waitFor
168
+ waitingForAddCh chan * waitFor [ T ]
169
169
170
170
// metrics counts the number of retries
171
171
metrics retryMetrics
172
172
}
173
173
174
174
// waitFor holds the data to add and the time it should be added
175
- type waitFor struct {
176
- data t
175
+ type waitFor [ T any ] struct {
176
+ data T
177
177
readyAt time.Time
178
178
// index in the priority queue (heap)
179
179
index int
@@ -187,32 +187,32 @@ type waitFor struct {
187
187
// it has been removed from the queue and placed at index Len()-1 by
188
188
// container/heap. Push adds an item at index Len(), and container/heap
189
189
// percolates it into the correct location.
190
- type waitForPriorityQueue []* waitFor
190
+ type waitForPriorityQueue [ T any ] []* waitFor [ T ]
191
191
192
- func (pq waitForPriorityQueue ) Len () int {
192
+ func (pq waitForPriorityQueue [ T ] ) Len () int {
193
193
return len (pq )
194
194
}
195
- func (pq waitForPriorityQueue ) Less (i , j int ) bool {
195
+ func (pq waitForPriorityQueue [ T ] ) Less (i , j int ) bool {
196
196
return pq [i ].readyAt .Before (pq [j ].readyAt )
197
197
}
198
- func (pq waitForPriorityQueue ) Swap (i , j int ) {
198
+ func (pq waitForPriorityQueue [ T ] ) Swap (i , j int ) {
199
199
pq [i ], pq [j ] = pq [j ], pq [i ]
200
200
pq [i ].index = i
201
201
pq [j ].index = j
202
202
}
203
203
204
204
// Push adds an item to the queue. Push should not be called directly; instead,
205
205
// use `heap.Push`.
206
- func (pq * waitForPriorityQueue ) Push (x interface {}) {
206
+ func (pq * waitForPriorityQueue [ T ] ) Push (x interface {}) {
207
207
n := len (* pq )
208
- item := x .(* waitFor )
208
+ item := x .(* waitFor [ T ] )
209
209
item .index = n
210
210
* pq = append (* pq , item )
211
211
}
212
212
213
213
// Pop removes an item from the queue. Pop should not be called directly;
214
214
// instead, use `heap.Pop`.
215
- func (pq * waitForPriorityQueue ) Pop () interface {} {
215
+ func (pq * waitForPriorityQueue [ T ] ) Pop () interface {} {
216
216
n := len (* pq )
217
217
item := (* pq )[n - 1 ]
218
218
item .index = - 1
@@ -222,7 +222,7 @@ func (pq *waitForPriorityQueue) Pop() interface{} {
222
222
223
223
// Peek returns the item at the beginning of the queue, without removing the
224
224
// item or otherwise mutating the queue. It is safe to call directly.
225
- func (pq waitForPriorityQueue ) Peek () interface {} {
225
+ func (pq waitForPriorityQueue [ T ] ) Peek () interface {} {
226
226
return pq [0 ]
227
227
}
228
228
@@ -254,7 +254,7 @@ func (q *delayingType[T]) AddAfter(item T, duration time.Duration) {
254
254
select {
255
255
case <- q .stopCh :
256
256
// unblock if ShutDown() is called
257
- case q .waitingForAddCh <- & waitFor {data : item , readyAt : q .clock .Now ().Add (duration )}:
257
+ case q .waitingForAddCh <- & waitFor [ T ] {data : item , readyAt : q .clock .Now ().Add (duration )}:
258
258
}
259
259
}
260
260
@@ -273,10 +273,10 @@ func (q *delayingType[T]) waitingLoop() {
273
273
// Make a timer that expires when the item at the head of the waiting queue is ready
274
274
var nextReadyAtTimer clock.Timer
275
275
276
- waitingForQueue := & waitForPriorityQueue {}
276
+ waitingForQueue := & waitForPriorityQueue [ T ] {}
277
277
heap .Init (waitingForQueue )
278
278
279
- waitingEntryByData := map [t ]* waitFor {}
279
+ waitingEntryByData := map [T ]* waitFor [ T ] {}
280
280
281
281
for {
282
282
if q .TypedInterface .ShuttingDown () {
@@ -287,13 +287,13 @@ func (q *delayingType[T]) waitingLoop() {
287
287
288
288
// Add ready entries
289
289
for waitingForQueue .Len () > 0 {
290
- entry := waitingForQueue .Peek ().(* waitFor )
290
+ entry := waitingForQueue .Peek ().(* waitFor [ T ] )
291
291
if entry .readyAt .After (now ) {
292
292
break
293
293
}
294
294
295
- entry = heap .Pop (waitingForQueue ).(* waitFor )
296
- q .Add (entry .data .( T ) )
295
+ entry = heap .Pop (waitingForQueue ).(* waitFor [ T ] )
296
+ q .Add (entry .data )
297
297
delete (waitingEntryByData , entry .data )
298
298
}
299
299
@@ -303,7 +303,7 @@ func (q *delayingType[T]) waitingLoop() {
303
303
if nextReadyAtTimer != nil {
304
304
nextReadyAtTimer .Stop ()
305
305
}
306
- entry := waitingForQueue .Peek ().(* waitFor )
306
+ entry := waitingForQueue .Peek ().(* waitFor [ T ] )
307
307
nextReadyAtTimer = q .clock .NewTimer (entry .readyAt .Sub (now ))
308
308
nextReadyAt = nextReadyAtTimer .C ()
309
309
}
@@ -322,7 +322,7 @@ func (q *delayingType[T]) waitingLoop() {
322
322
if waitEntry .readyAt .After (q .clock .Now ()) {
323
323
insert (waitingForQueue , waitingEntryByData , waitEntry )
324
324
} else {
325
- q .Add (waitEntry .data .( T ) )
325
+ q .Add (waitEntry .data )
326
326
}
327
327
328
328
drained := false
@@ -332,7 +332,7 @@ func (q *delayingType[T]) waitingLoop() {
332
332
if waitEntry .readyAt .After (q .clock .Now ()) {
333
333
insert (waitingForQueue , waitingEntryByData , waitEntry )
334
334
} else {
335
- q .Add (waitEntry .data .( T ) )
335
+ q .Add (waitEntry .data )
336
336
}
337
337
default :
338
338
drained = true
@@ -343,7 +343,7 @@ func (q *delayingType[T]) waitingLoop() {
343
343
}
344
344
345
345
// insert adds the entry to the priority queue, or updates the readyAt if it already exists in the queue
346
- func insert (q * waitForPriorityQueue , knownEntries map [t ]* waitFor , entry * waitFor ) {
346
+ func insert [ T comparable ] (q * waitForPriorityQueue [ T ] , knownEntries map [T ]* waitFor [ T ] , entry * waitFor [ T ] ) {
347
347
// if the entry already exists, update the time only if it would cause the item to be queued sooner
348
348
existing , exists := knownEntries [entry .data ]
349
349
if exists {
0 commit comments