forked from grab/async
/
batch_config.go
53 lines (45 loc) · 2.06 KB
/
batch_config.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
// Copyright (c) 2022 James Tran Dung, All rights reserved.
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
package batcher
import "time"
type batcherConfigs struct {
ticketBooth iTicketBooth
autoProcessSize int
autoProcessInterval time.Duration
shutdownGraceDuration time.Duration
}
type BatcherOption func(*batcherConfigs)
// WithAutoProcessByTicketBooth creates a synchronous ticket booth that can track
// how many ticket owners have arrived in order to start batch processing.
func WithAutoProcessByTicketBooth() BatcherOption {
return func(configs *batcherConfigs) {
configs.ticketBooth = newTicketBooth()
}
}
// WithAutoProcessSize sets the limit at which Batcher will automatically process
// the pending tasks. If `autoProcessSize <= 0`, the default behavior applies: no
// auto processing will be done based on size.
func WithAutoProcessSize(autoProcessSize int) BatcherOption {
return func(configs *batcherConfigs) {
configs.autoProcessSize = autoProcessSize
}
}
// WithAutoProcessInterval sets the interval at which Batcher will automatically process
// the pending tasks. If `autoProcessDurationInMilliseconds <= 0`, the default behavior
// applies: no periodic auto processing will be done.
//
// Note: if periodic auto processing is ON, clients MUST call Batcher.Shutdown() to clean
// up the timer goroutine properly in order to avoid memory leak.
func WithAutoProcessInterval(autoProcessIntervalInMilliseconds time.Duration) BatcherOption {
return func(configs *batcherConfigs) {
configs.autoProcessInterval = autoProcessIntervalInMilliseconds
}
}
// WithShutdownGraceDuration specifies how long Batcher will wait for the Shutdown operation
// to complete before returning. If `shutdownDurationInMilliseconds <= 0`, Batcher will block
// and wait until the shutdown operation fully completes.
func WithShutdownGraceDuration(shutdownDurationInMilliseconds time.Duration) BatcherOption {
return func(configs *batcherConfigs) {
configs.shutdownGraceDuration = shutdownDurationInMilliseconds
}
}