/
init.go
54 lines (48 loc) · 1.04 KB
/
init.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
package window
import (
"context"
"sync/atomic"
"time"
"github.com/songzhibin97/gkit/options"
)
// SetSize 设置大小
func SetSize(size uint) options.Option {
return func(c interface{}) {
c.(*conf).size = size
}
}
// SetInterval 设置间隔时间
func SetInterval(interval time.Duration) options.Option {
return func(c interface{}) {
c.(*conf).interval = interval
}
}
// SetContext 设置context
func SetContext(context context.Context) options.Option {
return func(c interface{}) {
c.(*conf).ctx = context
}
}
// NewWindow 实例化
func NewWindow(options ...options.Option) SlidingWindow {
w := Window{
// 默认值:
conf: conf{
size: 5,
interval: time.Second,
ctx: context.Background(),
},
}
for _, option := range options {
option(&w.conf)
}
w.buffer = make([]atomic.Value, w.size)
for i := uint(0); i < w.size; i++ {
w.buffer[i].Store(make(map[string]uint))
}
w.communication = make(chan Index, w.size)
w.ctx, w.cancel = context.WithCancel(w.ctx)
// 开启哨兵
go w.sentinel()
return &w
}