Skip to content

Commit

Permalink
Merge pull request #1979 from jyellick/fix-concurrent-map-write
Browse files Browse the repository at this point in the history
Eliminate possible broadcaster concurrent map read/write
  • Loading branch information
srderson committed Jun 27, 2016
2 parents 8fbb610 + f0e32b6 commit 586376c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions consensus/obcpbft/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ func newBroadcaster(self uint64, N int, f int, c communicator) *broadcaster {
continue
}
chans[uint64(i)] = make(chan *sendRequest, queueSize)
}

// We do not start the go routines in the above loop to avoid concurrent map read/writes
for i := 0; i < N; i++ {
go b.drainer(uint64(i))
}

return b
}

Expand Down Expand Up @@ -103,16 +108,17 @@ func (b *broadcaster) drainerSend(dest uint64, send *sendRequest, successLastTim

func (b *broadcaster) drainer(dest uint64) {
successLastTime := false
destChan := b.msgChans[dest] // Avoid doing the map lookup every send

for {
select {
case send := <-b.msgChans[dest]:
case send := <-destChan:
successLastTime = b.drainerSend(dest, send, successLastTime)
case <-b.closedCh:
for {
// Drain the message channel to free calling waiters before we shut down
select {
case send := <-b.msgChans[dest]:
case send := <-destChan:
send.done <- false
b.closed.Done()
default:
Expand Down

0 comments on commit 586376c

Please sign in to comment.