-
Notifications
You must be signed in to change notification settings - Fork 35
/
options.go
103 lines (88 loc) · 1.82 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package pool
import (
"errors"
"math/rand"
"sync"
"time"
)
var (
errClosed = errors.New("pool is closed")
errInvalid = errors.New("invalid config")
errRejected = errors.New("connection is nil. rejecting")
errTargets = errors.New("targets server is empty")
)
func init() {
rand.NewSource(time.Now().UnixNano())
}
//Options pool options
type Options struct {
lock sync.RWMutex
//targets node
targets *[]string
//targets channel
input chan *[]string
//InitTargets init targets
InitTargets []string
// init connection
InitCap int
// max connections
MaxCap int
DialTimeout time.Duration
IdleTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
}
// Input is the input channel
func (o *Options) Input() chan<- *[]string {
return o.input
}
// update targets
func (o *Options) update() {
//init targets
o.targets = &o.InitTargets
go func() {
for targets := range o.input {
if targets == nil {
continue
}
o.lock.Lock()
o.targets = targets
o.lock.Unlock()
}
}()
}
// NewOptions returns a new newOptions instance with sane defaults.
func NewOptions() *Options {
o := &Options{}
o.InitCap = 5
o.MaxCap = 100
o.DialTimeout = 5 * time.Second
o.ReadTimeout = 5 * time.Second
o.WriteTimeout = 5 * time.Second
o.IdleTimeout = 60 * time.Second
return o
}
// validate checks a Config instance.
func (o *Options) validate() error {
if o.InitTargets == nil ||
o.InitCap <= 0 ||
o.MaxCap <= 0 ||
o.InitCap > o.MaxCap ||
o.DialTimeout == 0 ||
o.ReadTimeout == 0 ||
o.WriteTimeout == 0 {
return errInvalid
}
return nil
}
//nextTarget next target implement load balance
func (o *Options) nextTarget() string {
o.lock.RLock()
defer o.lock.RUnlock()
tlen := len(*o.targets)
if tlen <= 0 {
return ""
}
//rand server
return (*o.targets)[rand.Int()%tlen]
}