2016-05-04 00:06:19 +03:00
|
|
|
package selector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/registry"
|
2016-05-07 01:12:37 +03:00
|
|
|
"github.com/micro/go-micro/selector/internal/blacklist"
|
2016-05-04 00:06:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type defaultSelector struct {
|
2016-05-07 01:12:37 +03:00
|
|
|
so Options
|
|
|
|
exit chan bool
|
|
|
|
bl *blacklist.BlackList
|
2016-05-04 00:06:19 +03: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)
|
|
|
|
}
|
|
|
|
|
2016-05-07 01:12:37 +03:00
|
|
|
// apply the blacklist
|
|
|
|
services, err = r.bl.Filter(services)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-04 00:06:19 +03:00
|
|
|
// if there's nothing left, return
|
|
|
|
if len(services) == 0 {
|
2016-05-07 02:04:08 +03:00
|
|
|
return nil, ErrNoneAvailable
|
2016-05-04 00:06:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sopts.Strategy(services), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *defaultSelector) Mark(service string, node *registry.Node, err error) {
|
2016-05-07 01:12:37 +03:00
|
|
|
r.bl.Mark(service, node, err)
|
2016-05-04 00:06:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *defaultSelector) Reset(service string) {
|
2016-05-07 01:12:37 +03:00
|
|
|
r.bl.Reset(service)
|
2016-05-04 00:06:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *defaultSelector) Close() error {
|
2016-05-07 01:12:37 +03:00
|
|
|
select {
|
|
|
|
case <-r.exit:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(r.exit)
|
|
|
|
r.bl.Close()
|
|
|
|
}
|
2016-05-04 00:06:19 +03:00
|
|
|
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 05:08:43 +03:00
|
|
|
return &defaultSelector{
|
2016-05-07 01:12:37 +03:00
|
|
|
so: sopts,
|
|
|
|
exit: make(chan bool),
|
|
|
|
bl: blacklist.New(),
|
|
|
|
}
|
2016-05-04 00:06:19 +03:00
|
|
|
}
|