/
githubmirror
/
GoFrame
Обзор
Документация
Войти
/
githubmirror
/
GoFrame
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
net/gsel/gsel_selector_random.go
47 строк
1 KB
John Guo
Feature/gsvc interface (#1871)
23 май 2022, 10:08
Не верифицирован
23 май 2022, 10:08
ab50626
Код
Авторство
О чём код?
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://github.com/gogf/gf. package gsel import ( "context" "sync" "github.com/gogf/gf/v2/internal/intlog" "github.com/gogf/gf/v2/util/grand" ) const SelectorRandom = "BalancerRandom" type selectorRandom struct { mu sync.RWMutex nodes Nodes } func NewSelectorRandom() Selector { return &selectorRandom{ nodes: make([]Node, 0), } } func (s *selectorRandom) Update(ctx context.Context, nodes Nodes) error { intlog.Printf(ctx, `Update nodes: %s`, nodes.String()) s.mu.Lock() defer s.mu.Unlock() s.nodes = nodes return nil } func (s *selectorRandom) Pick(ctx context.Context) (node Node, done DoneFunc, err error) { s.mu.RLock() defer s.mu.RUnlock() if len(s.nodes) == 0 { return nil, nil, nil } node = s.nodes[grand.Intn(len(s.nodes))] intlog.Printf(ctx, `Picked node: %s`, node.Address()) return node, nil, nil }