106 lines
2.8 KiB
Go
106 lines
2.8 KiB
Go
package discovery
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"errors"
|
|
"net"
|
|
"strconv"
|
|
"sync"
|
|
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
|
"go.etcd.io/etcd/client/v3/naming/endpoints"
|
|
)
|
|
|
|
type Registration struct {
|
|
client *clientv3.Client
|
|
lease clientv3.LeaseID
|
|
cancel context.CancelFunc
|
|
done chan struct{}
|
|
once sync.Once
|
|
closeError error
|
|
cfg Config
|
|
}
|
|
|
|
func ValidateAddress(address string) error {
|
|
host, port, err := net.SplitHostPort(address)
|
|
number, numberErr := strconv.Atoi(port)
|
|
if err != nil || numberErr != nil || number < 1 || number > 65535 || host == "" || host == "0.0.0.0" || host == "::" {
|
|
return errors.New("advertise_addr must contain a reachable host and a port between 1 and 65535")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Register(ctx context.Context, cfg Config, service, address string, ttl int64) (*Registration, error) {
|
|
if err := ValidateService(service); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := ValidateAddress(address); err != nil {
|
|
return nil, err
|
|
}
|
|
if ttl < 3 {
|
|
return nil, errors.New("etcd lease_ttl must be at least 3 seconds")
|
|
}
|
|
client, err := NewClient(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
registration := &Registration{client: client, cfg: cfg, done: make(chan struct{})}
|
|
opCtx, cancel := context.WithTimeout(ctx, cfg.DialTimeout)
|
|
lease, err := client.Grant(opCtx, ttl)
|
|
cancel()
|
|
if err != nil {
|
|
_ = client.Close()
|
|
return nil, errors.New("grant etcd lease failed")
|
|
}
|
|
registration.lease = lease.ID
|
|
manager, err := endpoints.NewManager(client, service)
|
|
if err != nil {
|
|
_ = registration.Close()
|
|
return nil, err
|
|
}
|
|
opCtx, cancel = context.WithTimeout(ctx, cfg.DialTimeout)
|
|
err = manager.AddEndpoint(opCtx, service+"/"+rand.Text(), endpoints.Endpoint{Addr: address}, clientv3.WithLease(lease.ID))
|
|
cancel()
|
|
if err != nil {
|
|
_ = registration.Close()
|
|
return nil, errors.New("register etcd endpoint failed")
|
|
}
|
|
keepAliveCtx, stop := context.WithCancel(ctx)
|
|
registration.cancel = stop
|
|
responses, err := client.KeepAlive(keepAliveCtx, lease.ID)
|
|
if err != nil {
|
|
_ = registration.Close()
|
|
return nil, errors.New("start etcd keepalive failed")
|
|
}
|
|
go func() {
|
|
defer close(registration.done)
|
|
for {
|
|
select {
|
|
case <-keepAliveCtx.Done():
|
|
return
|
|
case response, open := <-responses:
|
|
if !open || response == nil || response.TTL <= 0 {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
return registration, nil
|
|
}
|
|
|
|
func (registration *Registration) Done() <-chan struct{} { return registration.done }
|
|
|
|
func (registration *Registration) Close() error {
|
|
registration.once.Do(func() {
|
|
if registration.cancel != nil {
|
|
registration.cancel()
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), registration.cfg.DialTimeout)
|
|
defer cancel()
|
|
_, err := registration.client.Revoke(ctx, registration.lease)
|
|
registration.closeError = errors.Join(err, registration.client.Close())
|
|
})
|
|
return registration.closeError
|
|
}
|