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

90 lines
1.4 KiB
Go
Raw Normal View History

2016-05-03 23:06:19 +02:00
package selector
import (
"math/rand"
"time"
"github.com/micro/go-micro/registry"
)
type defaultSelector struct {
2016-06-19 15:41:33 +02:00
so Options
2016-05-03 23:06:19 +02:00
}
func init() {
rand.Seed(time.Now().Unix())
}
func (r *defaultSelector) Init(opts ...Option) error {
for _, o := range opts {
o(&r.so)
}
return nil
}
func (r *defaultSelector) Options() Options {
return r.so
}
func (r *defaultSelector) Select(service string, opts ...SelectOption) (Next, error) {
sopts := SelectOptions{
Strategy: r.so.Strategy,
}
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 {
return nil, ErrNoneAvailable
2016-05-03 23:06:19 +02:00
}
return sopts.Strategy(services), nil
}
func (r *defaultSelector) Mark(service string, node *registry.Node, err error) {
2016-06-19 15:41:33 +02:00
return
2016-05-03 23:06:19 +02:00
}
func (r *defaultSelector) Reset(service string) {
2016-06-19 15:41:33 +02:00
return
2016-05-03 23:06:19 +02:00
}
func (r *defaultSelector) Close() error {
return nil
}
func (r *defaultSelector) String() string {
return "default"
}
func newDefaultSelector(opts ...Option) Selector {
sopts := Options{
Strategy: Random,
}
for _, opt := range opts {
opt(&sopts)
}
if sopts.Registry == nil {
sopts.Registry = registry.DefaultRegistry
}
2016-05-07 04:08:43 +02:00
return &defaultSelector{
2016-06-19 15:41:33 +02:00
so: sopts,
2016-05-07 00:12:37 +02:00
}
2016-05-03 23:06:19 +02:00
}