From dc13f79d62a8bd64ee306a8fc134063af7df04c7 Mon Sep 17 00:00:00 2001 From: CMGS Date: Mon, 9 Nov 2020 14:30:21 +0800 Subject: [PATCH] decrease service put frequency --- discovery/helium/helium.go | 4 +--- store/etcdv3/service.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/discovery/helium/helium.go b/discovery/helium/helium.go index 96ab95105..d115ec65d 100644 --- a/discovery/helium/helium.go +++ b/discovery/helium/helium.go @@ -68,11 +68,9 @@ func (h *Helium) start(ctx context.Context) { Addresses: addresses, Interval: h.config.ServiceDiscoveryPushInterval * 2, } - h.dispatch(latestStatus) - case <-timer.C: - h.dispatch(latestStatus) } + h.dispatch(latestStatus) timer.Stop() timer.Reset(h.config.ServiceDiscoveryPushInterval) } diff --git a/store/etcdv3/service.go b/store/etcdv3/service.go index ebeb841f5..8f29bde6c 100644 --- a/store/etcdv3/service.go +++ b/store/etcdv3/service.go @@ -89,8 +89,22 @@ func (m *Mercury) RegisterService(ctx context.Context, serviceAddress string, ex if err != nil { return err } - - _, err = m.Put(ctx, key, "", clientv3.WithLease(lease.ID)) + tr, err := m.cliv3.Txn(ctx). + If(clientv3.Compare(clientv3.Version(key), "!=", 0)). + Then(clientv3.OpGet(key)). + Else(clientv3.OpPut(key, "", clientv3.WithLease(lease.ID))). + Commit() + if err != nil { + return err + } + if !tr.Succeeded { + return nil + } + oldLeaseID := clientv3.LeaseID(tr.Responses[0].GetResponseRange().Kvs[0].Lease) + if _, err = m.cliv3.Revoke(ctx, lease.ID); err != nil { + log.Warnf("[RegisterService] revoke lease failed %v", err) + } + _, err = m.cliv3.KeepAliveOnce(ctx, oldLeaseID) return err }