/
registry_selector.go
97 lines (75 loc) · 1.92 KB
/
registry_selector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package registry
import (
"fmt"
"sync/atomic"
"time"
"github.com/Allenxuxu/stark/registry"
"github.com/Allenxuxu/stark/registry/cache"
sr "github.com/Allenxuxu/stark/rpc/client/resolver"
"github.com/Allenxuxu/stark/rpc/client/selector"
"google.golang.org/grpc/resolver"
)
const scheme = "stark-registry"
var _selector atomic.Value
func registerSelector(s selector.Selector) {
_selector.Store(s)
}
func init() {
resolver.Register(sr.NewBuilder(scheme, &_selector))
}
type registrySelector struct {
opts selector.Options
rc cache.Cache
}
func NewSelector(rg registry.Registry, opt ...selector.Option) (selector.Selector, error) {
var opts selector.Options
for _, opt := range opt {
opt(&opts)
}
cacheOpts := make([]cache.Option, 0, 1)
if opts.Context != nil {
if t, ok := opts.Context.Value(ttlKey).(time.Duration); ok {
cacheOpts = append(cacheOpts, cache.WithTTL(t))
}
}
s := ®istrySelector{
opts: opts,
rc: cache.New(rg, cacheOpts...),
}
// fixme do better
registerSelector(s)
return s, nil
}
func (c *registrySelector) Options() selector.Options {
return c.opts
}
func (c *registrySelector) GetService(service string) ([]*registry.Service, error) {
services, err := c.rc.GetService(service)
if err != nil {
if err == registry.ErrNotFound {
return nil, selector.ErrNotFound
}
return nil, err
}
for _, filter := range c.opts.Filters {
services = filter(services)
}
if len(services) == 0 {
return nil, selector.ErrNoneAvailable
}
return services, nil
}
func (c *registrySelector) Watch(service string) (registry.Watcher, error) {
return c.rc.Watch(registry.WatchService(service))
}
// Close stops the watcher and destroys the cache
func (c *registrySelector) Close() error {
c.rc.Stop()
return nil
}
func (c *registrySelector) Address(service string) string {
return fmt.Sprintf("%s:///%s", scheme, service)
}
func (c *registrySelector) String() string {
return "registry"
}