-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
53 lines (46 loc) · 1.36 KB
/
option.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
package batch
import (
"context"
"time"
)
const (
// DefaultBatchSize represents the default value for the size of each batch.
DefaultBatchSize = 10
// DefaultMaxWait represents the default value of the maximum waiting time for filling each batch.
DefaultMaxWait = 1 * time.Second
)
// OptionFunc defines type for Options setter function.
type OptionFunc func(*Options)
// Options sets configuration options for the go-batch instance.
type Options struct {
// Size specifies the maximum number of items that can be in a batch.
//
// Default: 10.
Size int
// MaxWait specifies the maximum waiting time for filling a batch.
//
// Default: 1 * time.Second.
MaxWait time.Duration
// Context represents the context that the Batch instance will use for its life cycle.
//
// Default: context.Background().
Context context.Context
}
// WithSize sets the maximum number of items that can be in each batch.
func WithSize(size int) OptionFunc {
return func(opts *Options) {
opts.Size = size
}
}
// WithMaxWait sets the maximum waiting time for filling each batch.
func WithMaxWait(maxWait time.Duration) OptionFunc {
return func(opts *Options) {
opts.MaxWait = maxWait
}
}
// WithContext sets the context that the Batch instance will use for its life cycle.
func WithContext(ctx context.Context) OptionFunc {
return func(opts *Options) {
opts.Context = ctx
}
}