Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(contrib/registry/etcd): use service ID as Registry.ctxMap key instead of service pointer #3509

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions contrib/registry/etcd/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ type Registry struct {
ctxMap is used to store the context cancel function of each service instance.
When the service instance is deregistered, the corresponding context cancel function is called to stop the heartbeat.
*/
ctxMap map[*registry.ServiceInstance]context.CancelFunc
ctxMap map[string]*serviceCancel
}

type serviceCancel struct {
service *registry.ServiceInstance
cancel context.CancelFunc
}

// New creates etcd registry
Expand All @@ -73,7 +78,7 @@ func New(client *clientv3.Client, opts ...Option) (r *Registry) {
opts: op,
client: client,
kv: clientv3.NewKV(client),
ctxMap: make(map[*registry.ServiceInstance]context.CancelFunc),
ctxMap: make(map[string]*serviceCancel),
}
}

Expand All @@ -94,7 +99,10 @@ func (r *Registry) Register(ctx context.Context, service *registry.ServiceInstan
}

hctx, cancel := context.WithCancel(r.opts.ctx)
r.ctxMap[service] = cancel
r.ctxMap[service.ID] = &serviceCancel{
service: service,
cancel: cancel,
}
go r.heartBeat(hctx, leaseID, key, value)
return nil
}
Expand All @@ -107,9 +115,9 @@ func (r *Registry) Deregister(ctx context.Context, service *registry.ServiceInst
}
}()
// cancel heartbeat
if cancel, ok := r.ctxMap[service]; ok {
cancel()
delete(r.ctxMap, service)
if serviceCancel, ok := r.ctxMap[service.ID]; ok {
serviceCancel.cancel()
delete(r.ctxMap, service.ID)
}
key := fmt.Sprintf("%s/%s/%s", r.opts.namespace, service.Name, service.ID)
_, err := r.client.Delete(ctx, key)
Expand Down
Loading