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
// 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
}

View file

@ -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