Make protobuf client and request handler write protobuf length and data in one call to write to avoid race conditions

pull/269/head
Paul Dix 2014-02-23 11:46:06 -05:00
parent 1154adc8a5
commit 0892d341b3
2 changed files with 9 additions and 12 deletions

View File

@ -110,12 +110,12 @@ func (self *ProtobufClient) MakeRequest(request *protocol.Request, responseStrea
}
conn.SetWriteDeadline(time.Now().Add(self.writeTimeout))
err = binary.Write(conn, binary.LittleEndian, uint32(len(data)))
buff := bytes.NewBuffer(make([]byte, 0, len(data)+8))
binary.Write(buff, binary.LittleEndian, uint32(len(data)))
_, err = conn.Write(append(buff.Bytes(), data...))
if err == nil {
_, err = conn.Write(data)
if err == nil {
return nil
}
return nil
}
log.Error("ProtobufClient: error making request: %s", err)
// TODO: do something smarter here based on whatever the error is.

View File

@ -1,6 +1,7 @@
package coordinator
import (
"bytes"
"cluster"
log "code.google.com/p/log4go"
"common"
@ -122,13 +123,9 @@ func (self *ProtobufRequestHandler) WriteResponse(conn net.Conn, response *proto
return self.WriteResponse(conn, response)
}
err = binary.Write(conn, binary.LittleEndian, uint32(len(data)))
if err != nil {
log.Error("error writing response length: %s", err)
return err
}
_, err = conn.Write(data)
buff := bytes.NewBuffer(make([]byte, 0, len(data)+8))
binary.Write(buff, binary.LittleEndian, uint32(len(data)))
_, err = conn.Write(append(buff.Bytes(), data...))
if err != nil {
log.Error("error writing response: %s", err)
return err