Update to new etcd clientv3 version
This commit is contained in:
parent
324d45b871
commit
fe505d6188
164
discovery.go
164
discovery.go
@ -1,127 +1,127 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/coreos/etcd/clientv3"
|
||||
"github.com/google/uuid"
|
||||
"path"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/google/uuid"
|
||||
"go.etcd.io/etcd/client/v3"
|
||||
"path"
|
||||
)
|
||||
|
||||
type Registry struct {
|
||||
client *clientv3.Client
|
||||
path string
|
||||
client *clientv3.Client
|
||||
path string
|
||||
}
|
||||
|
||||
func New(endpoints []string) (*Registry, error) {
|
||||
client, err := clientv3.New(clientv3.Config{
|
||||
Endpoints: endpoints,
|
||||
})
|
||||
client, err := clientv3.New(clientv3.Config{
|
||||
Endpoints: endpoints,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Registry{
|
||||
client: client,
|
||||
}, nil
|
||||
return &Registry{
|
||||
client: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Registry) Register(ctx context.Context, service string, host Host) error {
|
||||
resp, err := r.client.Grant(context.Background(), 30)
|
||||
resp, err := r.client.Grant(context.Background(), 30)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if host.UUID == "" {
|
||||
host.UUID = uuid.New().String()
|
||||
}
|
||||
if host.UUID == "" {
|
||||
host.UUID = uuid.New().String()
|
||||
}
|
||||
|
||||
b, err := json.Marshal(host)
|
||||
b, err := json.Marshal(host)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
key := path.Join(r.path, service, host.UUID)
|
||||
key := path.Join(r.path, service, host.UUID)
|
||||
|
||||
_, err = r.client.Put(context.Background(), key, string(b), clientv3.WithLease(resp.ID))
|
||||
_, err = r.client.Put(context.Background(), key, string(b), clientv3.WithLease(resp.ID))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ch, err := r.client.KeepAlive(ctx, resp.ID)
|
||||
ch, err := r.client.KeepAlive(ctx, resp.ID)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case ka := <-ch:
|
||||
if ka == nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case ka := <-ch:
|
||||
if ka == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Removed
|
||||
}
|
||||
}
|
||||
}()
|
||||
// Removed
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) Get(service string) ([]Host, error) {
|
||||
res, err := r.client.Get(context.Background(), path.Join(r.path, service), clientv3.WithPrefix())
|
||||
res, err := r.client.Get(context.Background(), path.Join(r.path, service), clientv3.WithPrefix())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var host Host
|
||||
var host Host
|
||||
|
||||
hosts := make([]Host, len(res.Kvs))
|
||||
hosts := make([]Host, len(res.Kvs))
|
||||
|
||||
for i, kv := range res.Kvs {
|
||||
host = Host{}
|
||||
for i, kv := range res.Kvs {
|
||||
host = Host{}
|
||||
|
||||
if err = json.Unmarshal(kv.Value, &host); err != nil {
|
||||
continue
|
||||
}
|
||||
if err = json.Unmarshal(kv.Value, &host); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
hosts[i] = host
|
||||
}
|
||||
hosts[i] = host
|
||||
}
|
||||
|
||||
return hosts, nil
|
||||
return hosts, nil
|
||||
}
|
||||
|
||||
func (r *Registry) Watch(service string) (chan Host, chan string) {
|
||||
ch := r.client.Watch(context.Background(), path.Join(r.path, service), clientv3.WithPrefix())
|
||||
ch := r.client.Watch(context.Background(), path.Join(r.path, service), clientv3.WithPrefix())
|
||||
|
||||
upChan := make(chan Host)
|
||||
downChan := make(chan string)
|
||||
upChan := make(chan Host)
|
||||
downChan := make(chan string)
|
||||
|
||||
go func() {
|
||||
for resp := range ch {
|
||||
for _, e := range resp.Events {
|
||||
if e.Type == clientv3.EventTypeDelete {
|
||||
// Removed
|
||||
downChan <- string(e.Kv.Key)
|
||||
} else if e.IsCreate() || e.IsModify() {
|
||||
host := Host{}
|
||||
go func() {
|
||||
for resp := range ch {
|
||||
for _, e := range resp.Events {
|
||||
if e.Type == clientv3.EventTypeDelete {
|
||||
// Removed
|
||||
downChan <- string(e.Kv.Key)
|
||||
} else if e.IsCreate() || e.IsModify() {
|
||||
host := Host{}
|
||||
|
||||
if err := json.Unmarshal(e.Kv.Value, &host); err != nil {
|
||||
continue
|
||||
}
|
||||
if err := json.Unmarshal(e.Kv.Value, &host); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
upChan <- host
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
upChan <- host
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return upChan, downChan
|
||||
return upChan, downChan
|
||||
}
|
||||
|
17
go.mod
17
go.mod
@ -3,6 +3,17 @@ module meow.tf/go/service-discovery
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/coreos/etcd v3.3.13+incompatible
|
||||
github.com/google/uuid v1.1.1
|
||||
)
|
||||
github.com/coreos/etcd v3.3.27+incompatible
|
||||
github.com/coreos/go-semver v0.3.0 // indirect
|
||||
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
|
||||
github.com/coreos/pkg v0.0.0-20220810130054-c7d1c02cb6cf // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/google/uuid v1.3.0
|
||||
go.etcd.io/etcd/client/v3 v3.5.6 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
go.uber.org/zap v1.23.0 // indirect
|
||||
golang.org/x/net v0.2.0 // indirect
|
||||
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
|
||||
google.golang.org/grpc v1.51.0 // indirect
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user