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
This commit is contained in:
zelig 2015-01-21 22:03:28 +00:00
parent 7a33109e85
commit b29b7fedd0
2 changed files with 4 additions and 31 deletions

View file

@ -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:
rw.out <- msg
return nil
case <-time.After(msgWriteTimeout):
return newPeerError(errWrite, "messenger timeout")
}
}
func (rw *proto) EncodeMsg(code uint64, data ...interface{}) error {

View file

@ -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)
}()