You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/discov/subscriber.go

194 lines
4.1 KiB
Go

4 years ago
package discov
import (
"sync"
4 years ago
"sync/atomic"
4 years ago
"github.com/tal-tech/go-zero/core/discov/internal"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/syncx"
4 years ago
)
type (
// SubOption defines the method to customize a Subscriber.
SubOption func(sub *Subscriber)
4 years ago
// A Subscriber is used to subscribe the given key on a etcd cluster.
4 years ago
Subscriber struct {
endpoints []string
exclusive bool
items *container
4 years ago
}
)
// NewSubscriber returns a Subscriber.
// endpoints is the hosts of the etcd cluster.
// key is the key to subscribe.
// opts are used to customize the Subscriber.
4 years ago
func NewSubscriber(endpoints []string, key string, opts ...SubOption) (*Subscriber, error) {
sub := &Subscriber{
endpoints: endpoints,
}
4 years ago
for _, opt := range opts {
opt(sub)
4 years ago
}
sub.items = newContainer(sub.exclusive)
4 years ago
4 years ago
if err := internal.GetRegistry().Monitor(endpoints, key, sub.items); err != nil {
return nil, err
}
4 years ago
4 years ago
return sub, nil
4 years ago
}
// AddListener adds listener to s.
4 years ago
func (s *Subscriber) AddListener(listener func()) {
s.items.addListener(listener)
}
// Values returns all the subscription values.
4 years ago
func (s *Subscriber) Values() []string {
return s.items.getValues()
}
// Exclusive means that key value can only be 1:1,
4 years ago
// which means later added value will remove the keys associated with the same value previously.
func Exclusive() SubOption {
return func(sub *Subscriber) {
sub.exclusive = true
}
}
// WithSubEtcdAccount provides the etcd username/password.
func WithSubEtcdAccount(user, pass string) SubOption {
return func(sub *Subscriber) {
RegisterAccount(sub.endpoints, user, pass)
}
}
// WithSubEtcdTLS provides the etcd CertFile/CertKeyFile/CACertFile.
func WithSubEtcdTLS(certFile, certKeyFile, caFile string, insecureSkipVerify bool) SubOption {
return func(sub *Subscriber) {
logx.Must(RegisterTLS(sub.endpoints, certFile, certKeyFile, caFile, insecureSkipVerify))
4 years ago
}
}
type container struct {
exclusive bool
values map[string][]string
mapping map[string]string
4 years ago
snapshot atomic.Value
dirty *syncx.AtomicBool
4 years ago
listeners []func()
4 years ago
lock sync.Mutex
}
func newContainer(exclusive bool) *container {
return &container{
exclusive: exclusive,
values: make(map[string][]string),
mapping: make(map[string]string),
4 years ago
dirty: syncx.ForAtomicBool(true),
4 years ago
}
}
func (c *container) OnAdd(kv internal.KV) {
c.addKv(kv.Key, kv.Val)
4 years ago
c.notifyChange()
4 years ago
}
func (c *container) OnDelete(kv internal.KV) {
c.removeKey(kv.Key)
4 years ago
c.notifyChange()
4 years ago
}
// addKv adds the kv, returns if there are already other keys associate with the value
func (c *container) addKv(key, value string) ([]string, bool) {
c.lock.Lock()
defer c.lock.Unlock()
4 years ago
c.dirty.Set(true)
4 years ago
keys := c.values[value]
previous := append([]string(nil), keys...)
early := len(keys) > 0
if c.exclusive && early {
for _, each := range keys {
c.doRemoveKey(each)
}
}
c.values[value] = append(c.values[value], key)
c.mapping[key] = value
if early {
return previous, true
}
return nil, false
4 years ago
}
4 years ago
func (c *container) addListener(listener func()) {
c.lock.Lock()
c.listeners = append(c.listeners, listener)
c.lock.Unlock()
}
4 years ago
func (c *container) doRemoveKey(key string) {
server, ok := c.mapping[key]
if !ok {
return
}
delete(c.mapping, key)
keys := c.values[server]
remain := keys[:0]
for _, k := range keys {
if k != key {
remain = append(remain, k)
}
}
if len(remain) > 0 {
c.values[server] = remain
} else {
delete(c.values, server)
}
}
func (c *container) getValues() []string {
4 years ago
if !c.dirty.True() {
return c.snapshot.Load().([]string)
}
4 years ago
c.lock.Lock()
defer c.lock.Unlock()
4 years ago
var vals []string
4 years ago
for each := range c.values {
4 years ago
vals = append(vals, each)
4 years ago
}
4 years ago
c.snapshot.Store(vals)
c.dirty.Set(false)
return vals
4 years ago
}
4 years ago
func (c *container) notifyChange() {
c.lock.Lock()
listeners := append(([]func())(nil), c.listeners...)
c.lock.Unlock()
for _, listener := range listeners {
listener()
}
}
4 years ago
// removeKey removes the kv, returns true if there are still other keys associate with the value
func (c *container) removeKey(key string) {
c.lock.Lock()
defer c.lock.Unlock()
4 years ago
c.dirty.Set(true)
4 years ago
c.doRemoveKey(key)
}