swarm/network: Remove hive keep alive toggle

Signed-off-by: Lewis Marshall <lewis@lmars.net>
This commit is contained in:
Lewis Marshall 2017-05-17 21:56:06 -07:00
parent be0335c889
commit 0b28cf8544
2 changed files with 13 additions and 59 deletions

View file

@ -82,12 +82,11 @@ type Hive struct {
store Store store Store
// bookkeeping // bookkeeping
lock sync.Mutex lock sync.Mutex
quit chan bool quit chan bool
toggle chan bool more chan bool
more chan bool
newTicker func() hiveTicker tick <-chan time.Time
} }
// Hive constructor embeds both arguments // Hive constructor embeds both arguments
@ -112,7 +111,6 @@ func (self *Hive) Start(server *p2p.Server) error {
return err return err
} }
} }
self.toggle = make(chan bool)
self.more = make(chan bool, 1) self.more = make(chan bool, 1)
self.quit = make(chan bool) self.quit = make(chan bool)
log.Debug("hive started") log.Debug("hive started")
@ -150,10 +148,9 @@ func (self *Hive) Start(server *p2p.Server) error {
log.Info(fmt.Sprintf("%v", self)) log.Info(fmt.Sprintf("%v", self))
select { select {
case self.toggle <- want:
log.Trace(fmt.Sprintf("keep hive alive: %v", want))
case <-self.quit: case <-self.quit:
return return
default:
} }
} }
}() }()
@ -250,29 +247,16 @@ func (t *timeTicker) Ch() <-chan time.Time {
// it goes to sleep mode if table is saturated // it goes to sleep mode if table is saturated
// it restarts if the table becomes non-full again due to disconnections // it restarts if the table becomes non-full again due to disconnections
func (self *Hive) keepAlive() { func (self *Hive) keepAlive() {
if self.newTicker == nil { if self.tick == nil {
self.newTicker = func() hiveTicker { ticker := time.NewTicker(self.KeepAliveInterval)
return &timeTicker{time.NewTicker(self.KeepAliveInterval)} defer ticker.Stop()
} self.tick = ticker.C
} }
ticker := self.newTicker()
tick := ticker.Ch()
for { for {
select { select {
case <-tick: case <-self.tick:
log.Debug("wake up: make hive alive") log.Debug("wake up: make hive alive")
self.wake() 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: case <-self.quit:
return return
} }

View file

@ -1,36 +1,12 @@
package network package network
import ( import (
"sync"
"testing" "testing"
"time" "time"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
p2ptest "github.com/ethereum/go-ethereum/p2p/testing" 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) { func newHiveTester(t *testing.T, params *HiveParams) (*bzzTester, *Hive) {
// setup // setup
addr := RandomAddr() // tested peers peer address addr := RandomAddr() // tested peers peer address
@ -56,17 +32,11 @@ func TestRegisterAndConnect(t *testing.T) {
pp.Register(ch) pp.Register(ch)
// start the hive and wait for the connection // start the hive and wait for the connection
tc := &testConnect{ tick := make(chan time.Time)
connectf: func(c string) error { pp.tick = tick
s.Connect(adapters.NewNodeIdFromHex(c))
return nil
},
ticker: make(chan time.Time),
}
pp.newTicker = func() hiveTicker { return tc }
pp.Start(s.Server) pp.Start(s.Server)
defer pp.Stop() defer pp.Stop()
tc.ticker <- time.Now() tick <- time.Now()
// retrieve and broadcast // retrieve and broadcast
ord := raddr.Over()[0] / 32 ord := raddr.Over()[0] / 32
o := 0 o := 0