From 0b28cf8544ae897495103fee8aa3bd25e259feac Mon Sep 17 00:00:00 2001 From: Lewis Marshall Date: Wed, 17 May 2017 21:56:06 -0700 Subject: [PATCH] swarm/network: Remove hive keep alive toggle Signed-off-by: Lewis Marshall --- swarm/network/hive.go | 36 ++++++++++-------------------------- swarm/network/hive_test.go | 36 +++--------------------------------- 2 files changed, 13 insertions(+), 59 deletions(-) diff --git a/swarm/network/hive.go b/swarm/network/hive.go index 1a361cb86f..52a3521d38 100644 --- a/swarm/network/hive.go +++ b/swarm/network/hive.go @@ -82,12 +82,11 @@ type Hive struct { store Store // bookkeeping - lock sync.Mutex - quit chan bool - toggle chan bool - more chan bool + lock sync.Mutex + quit chan bool + more chan bool - newTicker func() hiveTicker + tick <-chan time.Time } // Hive constructor embeds both arguments @@ -112,7 +111,6 @@ func (self *Hive) Start(server *p2p.Server) error { return err } } - self.toggle = make(chan bool) self.more = make(chan bool, 1) self.quit = make(chan bool) log.Debug("hive started") @@ -150,10 +148,9 @@ func (self *Hive) Start(server *p2p.Server) error { log.Info(fmt.Sprintf("%v", self)) select { - case self.toggle <- want: - log.Trace(fmt.Sprintf("keep hive alive: %v", want)) case <-self.quit: return + default: } } }() @@ -250,29 +247,16 @@ func (t *timeTicker) Ch() <-chan time.Time { // it goes to sleep mode if table is saturated // it restarts if the table becomes non-full again due to disconnections func (self *Hive) keepAlive() { - if self.newTicker == nil { - self.newTicker = func() hiveTicker { - return &timeTicker{time.NewTicker(self.KeepAliveInterval)} - } + if self.tick == nil { + ticker := time.NewTicker(self.KeepAliveInterval) + defer ticker.Stop() + self.tick = ticker.C } - ticker := self.newTicker() - tick := ticker.Ch() for { select { - case <-tick: + case <-self.tick: log.Debug("wake up: make hive alive") self.wake() - case need := <-self.toggle: - if ticker == nil && need { - ticker = self.newTicker() - tick = ticker.Ch() - } - // if hive saturated, no more peers asked - if ticker != nil && !need { - ticker.Stop() - ticker = nil - tick = nil - } case <-self.quit: return } diff --git a/swarm/network/hive_test.go b/swarm/network/hive_test.go index a37b5c27ba..5a1b3f6ca8 100644 --- a/swarm/network/hive_test.go +++ b/swarm/network/hive_test.go @@ -1,36 +1,12 @@ package network import ( - "sync" "testing" "time" - "github.com/ethereum/go-ethereum/p2p/simulations/adapters" p2ptest "github.com/ethereum/go-ethereum/p2p/testing" ) -type testConnect struct { - mu sync.Mutex - conns []string - connectf func(c string) error - ticker chan time.Time -} - -func (self *testConnect) Ch() <-chan time.Time { - return self.ticker -} - -func (self *testConnect) Stop() { -} - -func (self *testConnect) connect(na string) error { - self.mu.Lock() - defer self.mu.Unlock() - self.conns = append(self.conns, na) - self.connectf(na) - return nil -} - func newHiveTester(t *testing.T, params *HiveParams) (*bzzTester, *Hive) { // setup addr := RandomAddr() // tested peers peer address @@ -56,17 +32,11 @@ func TestRegisterAndConnect(t *testing.T) { pp.Register(ch) // start the hive and wait for the connection - tc := &testConnect{ - connectf: func(c string) error { - s.Connect(adapters.NewNodeIdFromHex(c)) - return nil - }, - ticker: make(chan time.Time), - } - pp.newTicker = func() hiveTicker { return tc } + tick := make(chan time.Time) + pp.tick = tick pp.Start(s.Server) defer pp.Stop() - tc.ticker <- time.Now() + tick <- time.Now() // retrieve and broadcast ord := raddr.Over()[0] / 32 o := 0