You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

51 lines
820 B

package direct
import (
"context"
"git.diulo.com/mogfee/kit/selector"
"sync/atomic"
"time"
)
const (
defaultWeight = 100
)
var ()
type Node struct {
selector.Node
lastPick int64
}
func (n *Node) Raw() selector.Node {
return n.Node
}
func (n *Node) Weight() float64 {
if n.InitialWeight() != nil {
return float64(*n.InitialWeight())
}
return defaultWeight
}
func (n *Node) Pick() selector.DoneFunc {
now := time.Now().UnixNano()
atomic.StoreInt64(&n.lastPick, now)
return func(ctx context.Context, di selector.DoneInfo) {}
}
func (n *Node) PickElapsed() time.Duration {
return time.Duration(time.Now().UnixNano() - atomic.LoadInt64(&n.lastPick))
}
type Builder struct {
}
func (b *Builder) Build(node selector.Node) selector.WeightedNode {
return &Node{
Node: node,
lastPick: 0,
}
}