From 0cbd2bcc8dba799151f643635b3d66819b1e93af Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Sun, 14 Jun 2020 08:51:25 +0000 Subject: [PATCH] [networkinfo] gradually increase the peer finding interval Signed-off-by: Leo Chen --- api/service/networkinfo/service.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/api/service/networkinfo/service.go b/api/service/networkinfo/service.go index a5ca76667..674a78df6 100644 --- a/api/service/networkinfo/service.go +++ b/api/service/networkinfo/service.go @@ -45,9 +45,11 @@ var ( ) const ( - waitInRetry = 2 * time.Second + waitInRetry = 5 * time.Second connectionTimeout = 3 * time.Minute - findPeerInterval = 60 * time.Second + + minFindPeerInterval = 5 // initial find peer interval during bootstrap + maxFindPeerInterval = 1800 // max find peer interval, every 30 minutes // register to bootnode every ticker dhtTicker = 6 * time.Hour @@ -191,6 +193,9 @@ func (s *Service) DoService() { defer tick.Stop() ctx, cancel := context.WithCancel(context.Background()) defer cancel() + peerInterval := minFindPeerInterval + intervalTick := time.NewTicker(time.Duration(peerInterval) * time.Second) + defer intervalTick.Stop() for { select { case <-s.stopChan: @@ -211,7 +216,7 @@ func (s *Service) DoService() { utils.Logger().Info(). Str("Rendezvous", string(s.Rendezvous)). Msg("Successfully announced!") - case <-time.After(findPeerInterval): + case <-intervalTick.C: var err error s.peerInfo, err = s.discovery.FindPeers( ctx, string(s.Rendezvous), coredis.Limit(discoveryLimit), @@ -220,8 +225,13 @@ func (s *Service) DoService() { utils.Logger().Error().Err(err).Msg("FindPeers") return } + if peerInterval < maxFindPeerInterval { + peerInterval *= 2 + intervalTick.Stop() + intervalTick = time.NewTicker(time.Duration(peerInterval) * time.Second) + } - s.findPeers(ctx) + go s.findPeers(ctx) } } }