From b29b7fedd0ea27012a41db17c524a83800075026 Mon Sep 17 00:00:00 2001 From: zelig Date: Wed, 21 Jan 2015 22:03:28 +0000 Subject: [PATCH] remove peer.writeMsg - remove it, its redundant - remove duplicate code in peer.writeProtoMsg, now falling back to proto.WriteMsg - remove peer.writeMu, no need for mutex - no timer set in p.WriteMsg, - write will be async for the protocol anyway, so no write timeout argument anywhere, only constant set for conn writeDeadline --- p2p/messenger.go | 30 +++--------------------------- p2p/peer.go | 5 +---- 2 files changed, 4 insertions(+), 31 deletions(-) diff --git a/p2p/messenger.go b/p2p/messenger.go index 56cd0c9da0..cc56233037 100644 --- a/p2p/messenger.go +++ b/p2p/messenger.go @@ -143,27 +143,7 @@ func (p *Peer) writeProtoMsg(protoName string, msg Msg) error { if !ok { return fmt.Errorf("protocol %s not handled by peer", protoName) } - if msg.Code >= proto.maxcode { - return newPeerError(errInvalidMsgCode, "code %x is out of range for protocol %q", msg.Code, protoName) - } - msg.Code += proto.offset - return p.writeMsg(msg, msgWriteTimeout) -} - -/* -this function is not needed write will be done directly by the msgReadWriter -with the connection attached -if the interface is channel, then no write locking is needed for synchronous write -*/ -// writeMsg writes a message to the connection. -func (p *Peer) writeMsg(msg Msg, timeout time.Duration) error { - p.writeMu.Lock() - defer p.writeMu.Unlock() - p.conn.SetWriteDeadline(time.Now().Add(timeout)) - if err := writeMsg(p.bufconn, msg); err != nil { - return newPeerError(errWrite, "%v", err) - } - return p.bufconn.Flush() + return proto.WriteMsg(msg) } // proto will embed the same writer channel as given to the readwriter @@ -180,12 +160,8 @@ func (rw *proto) WriteMsg(msg Msg) error { return newPeerError(errInvalidMsgCode, "not handled") } msg.Code += rw.offset - select { - case rw.out <- msg: - return nil - case <-time.After(msgWriteTimeout): - return newPeerError(errWrite, "messenger timeout") - } + rw.out <- msg + return nil } func (rw *proto) EncodeMsg(code uint64, data ...interface{}) error { diff --git a/p2p/peer.go b/p2p/peer.go index a9a4cea591..f63bba1ba6 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -62,9 +62,6 @@ type Peer struct { listenAddr *peerAddr // what remote peer is listening on dialAddr *peerAddr // non-nil if dialing - // The mutex protects the connection - // so only one protocol can write at a time. - writeMu sync.Mutex conn net.Conn bufconn *bufio.ReadWriter outgoingMsgC chan Msg @@ -284,7 +281,7 @@ loop: done := make(chan struct{}) go func() { p.conn.SetDeadline(time.Now().Add(disconnectGracePeriod)) - p.writeMsg(NewMsg(discMsg, reason), disconnectGracePeriod) + p.writeProtoMsg("", NewMsg(discMsg, reason)) io.Copy(ioutil.Discard, p.conn) close(done) }()