Skip to content

Commit 898ea6e

Browse files
committed
Gossip implementation
This commit adds the gossip code itself, which is the code that connects between the comm layer, the pull process, and the discovery layer Change-Id: I9650790c61f318837fb7a68072386dfea09fc54f Signed-off-by: Yacov Manevich <[email protected]>
1 parent a293bc9 commit 898ea6e

File tree

9 files changed

+1205
-22
lines changed

9 files changed

+1205
-22
lines changed

gossip/comm/comm_test.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,18 @@ func TestHandshake(t *testing.T) {
106106
m := <-comm1.Accept(acceptAll)
107107
rcvChan <- m.GetGossipMessage()
108108
}()
109-
stream.Send(msg2Send)
109+
go stream.Send(msg2Send)
110110
time.Sleep(time.Second)
111111
assert.Equal(t, 1, len(rcvChan))
112-
receivedMsg := <-rcvChan
112+
var receivedMsg *proto.GossipMessage
113+
select {
114+
case receivedMsg = <-rcvChan:
115+
break
116+
case <- time.NewTicker(time.Duration(time.Second * 2)).C:
117+
assert.Fail(t, "Timed out waiting for received message")
118+
break
119+
}
120+
113121
assert.Equal(t, nonce, receivedMsg.Nonce)
114122

115123
// negative path, nothing should be read from the channel because the signature is wrong

gossip/gossip/algo/pull.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ func init() {
4747
rand.Seed(42)
4848
}
4949

50-
var digestWaitTime = time.Duration(4) * time.Second
51-
var requestWaitTime = time.Duration(4) * time.Second
52-
var responseWaitTime = time.Duration(7) * time.Second
50+
var digestWaitTime = time.Duration(1) * time.Second
51+
var requestWaitTime = time.Duration(1) * time.Second
52+
var responseWaitTime = time.Duration(2) * time.Second
5353

5454
// SetDigestWaitTime sets the digest wait time
5555
func SetDigestWaitTime(time time.Duration) {
@@ -127,8 +127,10 @@ func NewPullEngine(participant PullAdapter, sleepTime time.Duration) *PullEngine
127127
go func() {
128128
for !engine.toDie() {
129129
time.Sleep(sleepTime)
130+
if engine.toDie() {
131+
return
132+
}
130133
engine.initiatePull()
131-
132134
}
133135
}()
134136

@@ -278,7 +280,6 @@ func (engine *PullEngine) OnReq(items []uint64, nonce uint64, context interface{
278280
return
279281
}
280282
engine.lock.Lock()
281-
defer engine.lock.Unlock()
282283

283284
var items2Send []uint64
284285
for _, item := range items {
@@ -287,7 +288,9 @@ func (engine *PullEngine) OnReq(items []uint64, nonce uint64, context interface{
287288
}
288289
}
289290

290-
engine.SendRes(items2Send, context, nonce)
291+
engine.lock.Unlock()
292+
293+
go engine.SendRes(items2Send, context, nonce)
291294
}
292295

293296
// OnRes notifies the engine a response has arrived

gossip/gossip/batcher.go

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ func (p *batchingEmitterImpl) periodicEmit() {
6868
}
6969

7070
func (p *batchingEmitterImpl) emit() {
71+
if p.toDie() {
72+
return
73+
}
7174
if len(p.buff) == 0 {
7275
return
7376
}

gossip/gossip/batcher_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ limitations under the License.
1717
package gossip
1818

1919
import (
20-
"github.com/stretchr/testify/assert"
2120
"sync"
2221
"sync/atomic"
2322
"testing"
2423
"time"
24+
25+
"github.com/stretchr/testify/assert"
2526
)
2627

2728
func TestBatchingEmitterAddAndSize(t *testing.T) {

0 commit comments

Comments
 (0)