diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-04-15 18:01:22 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-04-15 18:01:22 +0800 |
commit | 6ceb253f743ec0d2bdd9a676c7f365de2201470c (patch) | |
tree | d856a141df93719d98100c2cdd4bf2a08bcd7b2c /whisper/peer.go | |
parent | 46ea193a49f60bb54cd5fc083adcc6fdf58dbdaf (diff) | |
download | go-tangerine-6ceb253f743ec0d2bdd9a676c7f365de2201470c.tar.gz go-tangerine-6ceb253f743ec0d2bdd9a676c7f365de2201470c.tar.zst go-tangerine-6ceb253f743ec0d2bdd9a676c7f365de2201470c.zip |
whisper: use async handshakes to handle blocking peers
Diffstat (limited to 'whisper/peer.go')
-rw-r--r-- | whisper/peer.go | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/whisper/peer.go b/whisper/peer.go index f077dbe70..8bf848855 100644 --- a/whisper/peer.go +++ b/whisper/peer.go @@ -53,10 +53,12 @@ func (self *peer) stop() { // handshake sends the protocol initiation status message to the remote peer and // verifies the remote status too. func (self *peer) handshake() error { - // Send own status message, fetch remote one - if err := p2p.SendItems(self.ws, statusCode, protocolVersion); err != nil { - return err - } + // Send the handshake status message asynchronously + errc := make(chan error, 1) + go func() { + errc <- p2p.SendItems(self.ws, statusCode, protocolVersion) + }() + // Fetch the remote status packet and verify protocol match packet, err := self.ws.ReadMsg() if err != nil { return err @@ -64,7 +66,6 @@ func (self *peer) handshake() error { if packet.Code != statusCode { return fmt.Errorf("peer sent %x before status packet", packet.Code) } - // Decode the rest of the status packet and verify protocol match s := rlp.NewStream(packet.Payload) if _, err := s.List(); err != nil { return fmt.Errorf("bad status message: %v", err) @@ -76,7 +77,11 @@ func (self *peer) handshake() error { if peerVersion != protocolVersion { return fmt.Errorf("protocol version mismatch %d != %d", peerVersion, protocolVersion) } - return packet.Discard() // ignore anything after protocol version + // Wait until out own status is consumed too + if err := <-errc; err != nil { + return fmt.Errorf("failed to send status packet: %v", err) + } + return nil } // update executes periodic operations on the peer, including message transmission |