| 1 |
package sturdyc |
| 2 |
|
| 3 |
import ( |
| 4 |
"time" |
| 5 |
) |
| 6 |
|
| 7 |
// buffer represents a buffer for a batch refresh. |
| 8 |
type buffer struct { |
| 9 |
channel chan []string |
| 10 |
ids []string |
| 11 |
} |
| 12 |
|
| 13 |
// createBuffer should be called WITH a lock when a refresh buffer is created. |
| 14 |
func (c *Client[T]) createBuffer(permutation string, ids []string) { |
| 15 |
bufferIDs := make([]string, 0, c.bufferSize) |
| 16 |
bufferIDs = append(bufferIDs, ids...) |
| 17 |
buf := &buffer{ |
| 18 |
channel: make(chan []string), |
| 19 |
ids: bufferIDs, |
| 20 |
} |
| 21 |
c.permutationBufferMap[permutation] = buf |
| 22 |
} |
| 23 |
|
| 24 |
// deleteBuffer should be called WITH a lock when a buffer has been processed. |
| 25 |
func (c *Client[T]) deleteBuffer(permutation string) { |
| 26 |
delete(c.permutationBufferMap, permutation) |
| 27 |
} |
| 28 |
|
| 29 |
// bufferBatchRefresh will buffer the batch of IDs until the batch size is reached or the buffer duration is exceeded. |
| 30 |
func bufferBatchRefresh[T any](c *Client[T], ids []string, keyFn KeyFn, fetchFn BatchFetchFn[T]) { |
| 31 |
if len(ids) == 0 { |
| 32 |
return |
| 33 |
} |
| 34 |
|
| 35 |
// If we got a perfect batch size, we can refresh the records immediately. |
| 36 |
if len(ids) == c.bufferSize { |
| 37 |
c.refreshBatch(ids, keyFn, fetchFn) |
| 38 |
return |
| 39 |
} |
| 40 |
|
| 41 |
c.batchMutex.Lock() |
| 42 |
|
| 43 |
// If the ids are greater than our batch size we'll have to chunk them. |
| 44 |
if len(ids) > c.bufferSize { |
| 45 |
idsToRefresh := ids[:c.bufferSize] |
| 46 |
overflowingIDs := ids[c.bufferSize:] |
| 47 |
c.batchMutex.Unlock() |
| 48 |
|
| 49 |
// These IDs are the size we want, so we'll refresh them immediately. |
| 50 |
c.safeGo(func() { |
| 51 |
c.refreshBatch(idsToRefresh, keyFn, fetchFn) |
| 52 |
}) |
| 53 |
|
| 54 |
// We'll continue to process the remaining IDs recursively. |
| 55 |
c.safeGo(func() { |
| 56 |
bufferBatchRefresh(c, overflowingIDs, keyFn, fetchFn) |
| 57 |
}) |
| 58 |
|
| 59 |
return |
| 60 |
} |
| 61 |
|
| 62 |
// Extract the permutation string from the ids. |
| 63 |
permutationString := extractPermutation(keyFn(ids[0])) |
| 64 |
|
| 65 |
// Check if we already have a batch for this set of options. |
| 66 |
if buf, ok := c.permutationBufferMap[permutationString]; ok { |
| 67 |
// There is a small chance that another goroutine manages to write to the channel |
| 68 |
// and fill the buffer as we unlock this mutex. Therefore, we'll add a timer so |
| 69 |
// that we can process these ids again if that were to happen. |
| 70 |
c.batchMutex.Unlock() |
| 71 |
timer, stop := c.clock.NewTimer(time.Millisecond * 10) |
| 72 |
select { |
| 73 |
case buf.channel <- ids: |
| 74 |
stop() |
| 75 |
case <-timer: |
| 76 |
c.safeGo(func() { |
| 77 |
bufferBatchRefresh(c, ids, keyFn, fetchFn) |
| 78 |
}) |
| 79 |
} |
| 80 |
return |
| 81 |
} |
| 82 |
|
| 83 |
// There is no existing batch buffering for this permutation |
| 84 |
// of options. Hence, we'll create a new one. |
| 85 |
c.createBuffer(permutationString, ids) |
| 86 |
c.batchMutex.Unlock() |
| 87 |
|
| 88 |
c.safeGo(func() { |
| 89 |
timer, stop := c.clock.NewTimer(c.bufferTimeout) |
| 90 |
c.batchMutex.Lock() |
| 91 |
idStream := c.permutationBufferMap[permutationString].channel |
| 92 |
c.batchMutex.Unlock() |
| 93 |
|
| 94 |
for { |
| 95 |
select { |
| 96 |
// If the buffer times out, we'll refresh the records regardless of the buffer size. |
| 97 |
case _, ok := <-timer: |
| 98 |
if !ok { |
| 99 |
return |
| 100 |
} |
| 101 |
|
| 102 |
// We reached the deadline for this batch. |
| 103 |
c.batchMutex.Lock() |
| 104 |
buffer := c.permutationBufferMap[permutationString] |
| 105 |
c.deleteBuffer(permutationString) |
| 106 |
c.batchMutex.Unlock() |
| 107 |
|
| 108 |
c.safeGo(func() { |
| 109 |
c.refreshBatch(buffer.ids, keyFn, fetchFn) |
| 110 |
}) |
| 111 |
return |
| 112 |
|
| 113 |
case additionalIDs, ok := <-idStream: |
| 114 |
if !ok { |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
// Lock the mutex, and add the additional IDs to the buffer. |
| 119 |
c.batchMutex.Lock() |
| 120 |
buffer := c.permutationBufferMap[permutationString] |
| 121 |
buffer.ids = append(buffer.ids, additionalIDs...) |
| 122 |
|
| 123 |
// If we haven't reached the batch size yet, we'll wait for more ids. |
| 124 |
if len(buffer.ids) < c.bufferSize { |
| 125 |
c.batchMutex.Unlock() |
| 126 |
continue |
| 127 |
} |
| 128 |
|
| 129 |
// At this point, we have either reached or exceeded the batch size. We'll stop the timer and drain the channel. |
| 130 |
if !stop() { |
| 131 |
<-timer |
| 132 |
} |
| 133 |
|
| 134 |
// Grab a reference to the IDs, and then delete the buffer. |
| 135 |
permIDs := buffer.ids |
| 136 |
c.deleteBuffer(permutationString) |
| 137 |
c.batchMutex.Unlock() |
| 138 |
|
| 139 |
idsToRefresh := permIDs[:c.bufferSize] |
| 140 |
overflowingIDs := permIDs[c.bufferSize:] |
| 141 |
|
| 142 |
// Refresh the first batch of IDs immediately. |
| 143 |
c.safeGo(func() { |
| 144 |
c.refreshBatch(idsToRefresh, keyFn, fetchFn) |
| 145 |
}) |
| 146 |
|
| 147 |
// If we exceeded the batch size, we'll continue to process the remaining IDs recursively. |
| 148 |
if len(overflowingIDs) > 0 { |
| 149 |
c.safeGo(func() { |
| 150 |
bufferBatchRefresh(c, overflowingIDs, keyFn, fetchFn) |
| 151 |
}) |
| 152 |
} |
| 153 |
return |
| 154 |
} |
| 155 |
} |
| 156 |
}) |
| 157 |
} |