Change new get sync peer bits to a query channel.

Rather than using a dedicated channel for the sync peer request and reply,
use a single query channel that accepts a query type as well as a reply
channel.  This will allow other queries to be added in the future without
the various queries being racy.
This commit is contained in:
Dave Collins 2014-02-04 09:39:00 -06:00
parent aab3a6643c
commit d949072d6d

View file

@ -70,6 +70,12 @@ type txMsg struct {
peer *peer
}
// getSyncPeerMsg is a message type to be sent across the query channel for
// retrieving the current sync peer.
type getSyncPeerMsg struct {
reply chan *peer
}
// headerNode is used as a node in a list of headers that are linked together
// between checkpoints.
type headerNode struct {
@ -93,8 +99,7 @@ type blockManager struct {
processingReqs bool
syncPeer *peer
msgChan chan interface{}
syncPeerRequest chan bool
syncPeerResult chan *peer
query chan interface{}
wg sync.WaitGroup
quit chan bool
@ -904,9 +909,16 @@ out:
// bitch and whine.
}
// Return the current sync peer.
case <-b.syncPeerRequest:
b.syncPeerResult <- b.syncPeer
// Queries used for atomically retrieving internal state.
case qmsg := <-b.query:
switch msg := qmsg.(type) {
case getSyncPeerMsg:
msg.reply <- b.syncPeer
default:
bmgrLog.Warnf("Invalid query type in block "+
"handler query: %T", msg)
}
case <-b.quit:
break out
@ -1112,8 +1124,9 @@ func (b *blockManager) Stop() error {
// SyncPeer returns the current sync peer.
func (b *blockManager) SyncPeer() *peer {
b.syncPeerRequest <- true
return <-b.syncPeerResult
reply := make(chan *peer)
b.query <- getSyncPeerMsg{reply: reply}
return <-reply
}
// newBlockManager returns a new bitcoin block manager.
@ -1131,8 +1144,7 @@ func newBlockManager(s *server) (*blockManager, error) {
requestedBlocks: make(map[btcwire.ShaHash]bool),
lastBlockLogTime: time.Now(),
msgChan: make(chan interface{}, cfg.MaxPeers*3),
syncPeerRequest: make(chan bool, 1),
syncPeerResult: make(chan *peer, 1),
query: make(chan interface{}),
headerList: list.New(),
quit: make(chan bool),
}