-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct.go
51 lines (41 loc) · 978 Bytes
/
direct.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
package direct
import (
"context"
"github.com/zouchunxu/gof/selector"
"sync/atomic"
"time"
)
const (
defaultWeight = 100
)
var (
_ selector.WeightedNode = &node{}
_ selector.WeightedNodeBuilder = &Builder{}
)
// node is endpoint instance
type node struct {
selector.Node
// last lastPick timestamp
lastPick int64
}
// Builder is direct node builder
type Builder struct{}
// Build create node
func (*Builder) Build(n selector.Node) selector.WeightedNode {
return &node{Node: n, lastPick: 0}
}
func (n *node) Pick() selector.DoneFunc {
now := time.Now().UnixNano()
atomic.StoreInt64(&n.lastPick, now)
return func(ctx context.Context, di selector.DoneInfo) {}
}
// Weight is node effective weight
func (n *node) Weight() float64 {
if n.InitialWeight() != nil {
return float64(*n.InitialWeight())
}
return defaultWeight
}
func (n *node) PickElapsed() time.Duration {
return time.Duration(time.Now().UnixNano() - atomic.LoadInt64(&n.lastPick))
}