1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/selector/roundrobin/round_robin_selector.go

93 lines
1.6 KiB
Go
Raw Normal View History

2015-12-09 21:23:16 +02:00
package roundrobin
2015-12-09 02:02:45 +02:00
import (
"sync"
2015-12-09 21:23:16 +02:00
2015-12-09 21:38:50 +02:00
"github.com/micro/go-micro/cmd"
2015-12-09 21:23:16 +02:00
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector"
2015-12-09 02:02:45 +02:00
)
type roundRobinSelector struct {
2015-12-09 21:23:16 +02:00
so selector.Options
2015-12-09 02:02:45 +02:00
}
2015-12-09 21:38:50 +02:00
func init() {
cmd.Selectors["roundrobin"] = NewSelector
}
2015-12-09 21:23:16 +02:00
func (r *roundRobinSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
var sopts selector.SelectOptions
2015-12-09 02:02:45 +02:00
for _, opt := range opts {
opt(&sopts)
}
// get the service
services, err := r.so.Registry.GetService(service)
if err != nil {
return nil, err
}
// apply the filters
for _, filter := range sopts.Filters {
services = filter(services)
}
// if there's nothing left, return
if len(services) == 0 {
2015-12-09 21:23:16 +02:00
return nil, selector.ErrNotFound
2015-12-09 02:02:45 +02:00
}
2015-12-09 21:23:16 +02:00
var nodes []*registry.Node
2015-12-09 02:02:45 +02:00
for _, service := range services {
for _, node := range service.Nodes {
nodes = append(nodes, node)
}
}
if len(nodes) == 0 {
2015-12-09 21:23:16 +02:00
return nil, selector.ErrNotFound
2015-12-09 02:02:45 +02:00
}
var i int
var mtx sync.Mutex
2015-12-09 21:23:16 +02:00
return func() (*registry.Node, error) {
2015-12-09 02:02:45 +02:00
mtx.Lock()
defer mtx.Unlock()
i++
return nodes[i%len(nodes)], nil
}, nil
}
2015-12-09 21:23:16 +02:00
func (r *roundRobinSelector) Mark(service string, node *registry.Node, err error) {
2015-12-09 02:02:45 +02:00
return
}
func (r *roundRobinSelector) Reset(service string) {
return
}
func (r *roundRobinSelector) Close() error {
return nil
}
2015-12-19 23:56:14 +02:00
func (r *roundRobinSelector) String() string {
return "roundrobin"
}
2015-12-09 21:38:50 +02:00
func NewSelector(opts ...selector.Option) selector.Selector {
2015-12-09 21:23:16 +02:00
var sopts selector.Options
2015-12-09 02:02:45 +02:00
for _, opt := range opts {
opt(&sopts)
}
if sopts.Registry == nil {
2015-12-09 21:23:16 +02:00
sopts.Registry = registry.DefaultRegistry
2015-12-09 02:02:45 +02:00
}
return &roundRobinSelector{sopts}
}