|
| 1 | +package retry |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// Specifies an element of the retry queue. The element must have methods that process |
| 10 | +// and record failure to process. The processing method will return a boolean representing |
| 11 | +// whether processing should be tried again. |
| 12 | +type Element interface { |
| 13 | + // Process this element. Return true if the process shoud be tried again. |
| 14 | + Process() bool |
| 15 | + // Record failure to process. |
| 16 | + Fail() |
| 17 | +} |
| 18 | + |
| 19 | +// Retry is a queue with an automatic retry capability. Elements added to the queue are |
| 20 | +// processed and retried until the input is closed and |
| 21 | +type Retry struct { |
| 22 | + input chan Element |
| 23 | + retry chan Element |
| 24 | + wait sync.WaitGroup |
| 25 | +} |
| 26 | + |
| 27 | +// Build an empty retry queue |
| 28 | +func New(capacity uint) *Retry { |
| 29 | + r := &Retry{ |
| 30 | + input: make(chan Element), |
| 31 | + retry: make(chan Element, capacity), |
| 32 | + } |
| 33 | + r.run() |
| 34 | + return r |
| 35 | +} |
| 36 | + |
| 37 | +// Add a new element to the retry queue |
| 38 | +func (r *Retry) Push(e Element) { |
| 39 | + r.input <- e |
| 40 | +} |
| 41 | + |
| 42 | +// Close channel, indicating that no further input will be passed |
| 43 | +func (r *Retry) Close() { |
| 44 | + close(r.input) |
| 45 | +} |
| 46 | + |
| 47 | +func (r *Retry) Wait() { |
| 48 | + r.wait.Wait() |
| 49 | +} |
| 50 | + |
| 51 | +// run starts the processing of elements |
| 52 | +func (r *Retry) run() { |
| 53 | + |
| 54 | + inputTimeout := make(chan bool, 1) |
| 55 | + |
| 56 | + // input goroutine |
| 57 | + go func() { |
| 58 | + for elem := range r.input { |
| 59 | + tryagain := elem.Process() |
| 60 | + if tryagain { |
| 61 | + r.retry <- elem |
| 62 | + } |
| 63 | + } |
| 64 | + inputTimeout <- true |
| 65 | + }() |
| 66 | + |
| 67 | + // retry goroutine |
| 68 | + go func() { |
| 69 | + |
| 70 | + r.wait.Add(1) |
| 71 | + |
| 72 | + cntr := 0 |
| 73 | + rlen := 0 |
| 74 | + inputDone := false |
| 75 | + retryDone := false |
| 76 | + |
| 77 | + for elem := range r.retry { |
| 78 | + |
| 79 | + // if done, process all remaining elements |
| 80 | + if retryDone { |
| 81 | + elem.Fail() |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + // process current element |
| 86 | + tryagain := elem.Process() |
| 87 | + if tryagain { |
| 88 | + r.retry <- elem |
| 89 | + } |
| 90 | + |
| 91 | + // if the input channel is closed, start monitoring for shutdown conditions |
| 92 | + select { |
| 93 | + case <-inputTimeout: |
| 94 | + inputDone = true |
| 95 | + rlen = len(r.retry) |
| 96 | + case <-time.After(1 * time.Second): |
| 97 | + continue |
| 98 | + default: |
| 99 | + } |
| 100 | + |
| 101 | + // if input is done, loop through all elements once then shutdown |
| 102 | + if inputDone { |
| 103 | + fmt.Printf("Input closed, loop %d\n", cntr) |
| 104 | + cntr = cntr + 1 |
| 105 | + |
| 106 | + if cntr > rlen { |
| 107 | + retryDone = true |
| 108 | + close(r.retry) |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + r.wait.Done() |
| 116 | + |
| 117 | + }() |
| 118 | + |
| 119 | +} |
0 commit comments