-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.go
73 lines (60 loc) · 1.34 KB
/
profile.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
package flusher
import (
"context"
"fmt"
"sync/atomic"
"time"
)
var (
taskCount uint32
)
type flushOption struct {
ctx context.Context
TaskName string
BufferSize int
IntervalInMills time.Duration
Concurrent int
QueueSize int
}
type FlushOption func(option *flushOption)
func defaultOption() *flushOption {
option := &flushOption{
ctx: context.Background(),
TaskName: fmt.Sprintf("batch_flush_task_%d", atomic.AddUint32(&taskCount, 1)),
BufferSize: 10,
IntervalInMills: 5 * time.Second,
Concurrent: 1,
}
option.QueueSize = option.BufferSize * 5
return option
}
func WithCtx(ctx context.Context) FlushOption {
return func(option *flushOption) {
option.ctx = ctx
}
}
func WithTaskName(name string) FlushOption {
return func(option *flushOption) {
option.TaskName = name
}
}
func WithBufferSize(size int) FlushOption {
return func(option *flushOption) {
option.BufferSize = size
}
}
func WithIntervalInMills(intervalInMills time.Duration) FlushOption {
return func(option *flushOption) {
option.IntervalInMills = intervalInMills
}
}
func WithConcurrent(concurrent int) FlushOption {
return func(option *flushOption) {
option.Concurrent = concurrent
}
}
func WithQueueSize(size int) FlushOption {
return func(option *flushOption) {
option.QueueSize = size
}
}