/
options.go
85 lines (74 loc) · 2.15 KB
/
options.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
76
77
78
79
80
81
82
83
84
85
package server
import (
"github.com/pkg/errors"
libetcd "social/lib/etcd"
pkgsubbench "social/pkg/subbench"
)
type OnDefaultHandler func(v interface{}) error
// Options contains Options to configure a server instance. Each option can be set through setter functions. See
// documentation for each setter function for an explanation of the option.
type Options struct {
subBench pkgsubbench.ISubBench
defaultHandler OnDefaultHandler // default 处理函数
etcdHandler libetcd.OnFunc // etcd 处理函数
timerEachSecond *NormalTimerSecond
timerEachDay *NormalTimerSecond
}
// NewOptions 新的Options
func NewOptions() *Options {
return new(Options)
}
func (p *Options) WithTimerEachSecond(timerEachSecond *NormalTimerSecond) *Options {
p.timerEachSecond = timerEachSecond
return p
}
func (p *Options) WithTimerEachDay(timerEachDay *NormalTimerSecond) *Options {
p.timerEachDay = timerEachDay
return p
}
func (p *Options) WithSubBench(subBench pkgsubbench.ISubBench) *Options {
p.subBench = subBench
return p
}
func (p *Options) WithDefaultHandler(defaultHandler OnDefaultHandler) *Options {
p.defaultHandler = defaultHandler
return p
}
func (p *Options) WithEtcdHandler(etcdHandler libetcd.OnFunc) *Options {
p.etcdHandler = etcdHandler
return p
}
// mergeOptions combines the given *Options into a single *Options in a last one wins fashion.
// The specified Options are merged with the existing Options on the server, with the specified Options taking
// precedence.
func mergeOptions(opts ...*Options) *Options {
so := NewOptions()
for _, opt := range opts {
if opt == nil {
continue
}
if opt.timerEachSecond != nil {
so.timerEachSecond = opt.timerEachSecond
}
if opt.timerEachDay != nil {
so.timerEachDay = opt.timerEachDay
}
if opt.subBench != nil {
so.subBench = opt.subBench
}
if opt.defaultHandler != nil {
so.defaultHandler = opt.defaultHandler
}
if opt.etcdHandler != nil {
so.etcdHandler = opt.etcdHandler
}
}
return so
}
// 配置
func configure(opt *Options) error {
if opt.etcdHandler == nil {
return errors.WithMessage(errors.New("etcdHandler is nil"), "etcdHandler is nil")
}
return nil
}