mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-24 10:07:04 +02:00
Add static selector
This commit is contained in:
parent
4adc31e62d
commit
4692af4393
@ -26,6 +26,7 @@ import (
|
|||||||
|
|
||||||
// selectors
|
// selectors
|
||||||
"github.com/micro/go-micro/selector"
|
"github.com/micro/go-micro/selector"
|
||||||
|
"github.com/micro/go-micro/selector/static"
|
||||||
|
|
||||||
// transports
|
// transports
|
||||||
"github.com/micro/go-micro/transport"
|
"github.com/micro/go-micro/transport"
|
||||||
@ -178,6 +179,7 @@ var (
|
|||||||
DefaultSelectors = map[string]func(...selector.Option) selector.Selector{
|
DefaultSelectors = map[string]func(...selector.Option) selector.Selector{
|
||||||
"default": selector.NewSelector,
|
"default": selector.NewSelector,
|
||||||
"cache": selector.NewSelector,
|
"cache": selector.NewSelector,
|
||||||
|
"static": static.NewSelector,
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultServers = map[string]func(...server.Option) server.Server{
|
DefaultServers = map[string]func(...server.Option) server.Server{
|
||||||
|
71
selector/static/static.go
Normal file
71
selector/static/static.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
// Package static provides a static resolver which returns the name/ip passed in without any change
|
||||||
|
package static
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/registry"
|
||||||
|
"github.com/micro/go-micro/selector"
|
||||||
|
)
|
||||||
|
|
||||||
|
// staticSelector is a static selector
|
||||||
|
type staticSelector struct {
|
||||||
|
opts selector.Options
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *staticSelector) Init(opts ...selector.Option) error {
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&s.opts)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *staticSelector) Options() selector.Options {
|
||||||
|
return s.opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *staticSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
|
||||||
|
var port int
|
||||||
|
addr, pt, err := net.SplitHostPort(service)
|
||||||
|
if err != nil {
|
||||||
|
addr = service
|
||||||
|
port = 0
|
||||||
|
} else {
|
||||||
|
port, _ = strconv.Atoi(pt)
|
||||||
|
}
|
||||||
|
|
||||||
|
return func() (*registry.Node, error) {
|
||||||
|
return ®istry.Node{
|
||||||
|
Id: service,
|
||||||
|
Address: addr,
|
||||||
|
Port: port,
|
||||||
|
}, nil
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *staticSelector) Mark(service string, node *registry.Node, err error) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *staticSelector) Reset(service string) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *staticSelector) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *staticSelector) String() string {
|
||||||
|
return "static"
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSelector(opts ...selector.Option) selector.Selector {
|
||||||
|
var options selector.Options
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
return &staticSelector{
|
||||||
|
opts: options,
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user