forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batcher.go
75 lines (63 loc) · 1.93 KB
/
batcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sync2
import (
"time"
)
// Batcher delays concurrent operations for a configurable interval in order to
// batch them up or otherwise clock their operation to run concurrently.
//
// It is implemented as a channel of int32s. Each waiter blocks on the channel
// from which it gets a sequentially increasing batch ID when the timer elapses.
//
// Hence a waiter is delayed for at most the batch interval.
type Batcher struct {
interval time.Duration
queue chan int
waiters AtomicInt32
nextID AtomicInt32
}
// NewBatcher returns a new Batcher
func NewBatcher(interval time.Duration) *Batcher {
return &Batcher{
interval: interval,
queue: make(chan int),
waiters: NewAtomicInt32(0),
nextID: NewAtomicInt32(0),
}
}
// Wait adds a new waiter to the queue and blocks until the next batch
func (b *Batcher) Wait() int {
numWaiters := b.waiters.Add(1)
if numWaiters == 1 {
b.newBatch()
}
return <-b.queue
}
// newBatch starts a new batch
func (b *Batcher) newBatch() {
go func() {
time.Sleep(b.interval)
id := b.nextID.Add(1)
// Make sure to atomically reset the number of waiters to make
// sure that all incoming requests either make it into the
// current batch or the next one.
waiters := b.waiters.Get()
for !b.waiters.CompareAndSwap(waiters, 0) {
waiters = b.waiters.Get()
}
for i := int32(0); i < waiters; i++ {
b.queue <- int(id)
}
}()
}