lib/protocol: Refactor to pass only relevant argument to writeMessage (#5681)

This commit is contained in:
Simon Frei 2019-05-02 14:09:19 +02:00 committed by GitHub
parent fe4daf242b
commit 9f358ecae0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -343,7 +343,7 @@ func (c *rawConnection) ClusterConfig(config ClusterConfig) {
return return
default: default:
} }
if err := c.writeMessage(asyncMessage{&config, nil}); err != nil { if err := c.writeMessage(&config); err != nil {
c.internalClose(err) c.internalClose(err)
} }
close(c.sentClusterConfig) close(c.sentClusterConfig)
@ -690,7 +690,7 @@ func (c *rawConnection) writerLoop() error {
for { for {
select { select {
case hm := <-c.outbox: case hm := <-c.outbox:
err := c.writeMessage(hm) err := c.writeMessage(hm.msg)
if hm.done != nil { if hm.done != nil {
close(hm.done) close(hm.done)
} }
@ -704,17 +704,17 @@ func (c *rawConnection) writerLoop() error {
} }
} }
func (c *rawConnection) writeMessage(hm asyncMessage) error { func (c *rawConnection) writeMessage(msg message) error {
if c.shouldCompressMessage(hm.msg) { if c.shouldCompressMessage(msg) {
return c.writeCompressedMessage(hm) return c.writeCompressedMessage(msg)
} }
return c.writeUncompressedMessage(hm) return c.writeUncompressedMessage(msg)
} }
func (c *rawConnection) writeCompressedMessage(hm asyncMessage) error { func (c *rawConnection) writeCompressedMessage(msg message) error {
size := hm.msg.ProtoSize() size := msg.ProtoSize()
buf := BufferPool.Get(size) buf := BufferPool.Get(size)
if _, err := hm.msg.MarshalTo(buf); err != nil { if _, err := msg.MarshalTo(buf); err != nil {
return fmt.Errorf("marshalling message: %v", err) return fmt.Errorf("marshalling message: %v", err)
} }
@ -724,7 +724,7 @@ func (c *rawConnection) writeCompressedMessage(hm asyncMessage) error {
} }
hdr := Header{ hdr := Header{
Type: c.typeOf(hm.msg), Type: c.typeOf(msg),
Compression: MessageCompressionLZ4, Compression: MessageCompressionLZ4,
} }
hdrSize := hdr.ProtoSize() hdrSize := hdr.ProtoSize()
@ -757,11 +757,11 @@ func (c *rawConnection) writeCompressedMessage(hm asyncMessage) error {
return nil return nil
} }
func (c *rawConnection) writeUncompressedMessage(hm asyncMessage) error { func (c *rawConnection) writeUncompressedMessage(msg message) error {
size := hm.msg.ProtoSize() size := msg.ProtoSize()
hdr := Header{ hdr := Header{
Type: c.typeOf(hm.msg), Type: c.typeOf(msg),
} }
hdrSize := hdr.ProtoSize() hdrSize := hdr.ProtoSize()
if hdrSize > 1<<16-1 { if hdrSize > 1<<16-1 {
@ -780,7 +780,7 @@ func (c *rawConnection) writeUncompressedMessage(hm asyncMessage) error {
// Message length // Message length
binary.BigEndian.PutUint32(buf[2+hdrSize:], uint32(size)) binary.BigEndian.PutUint32(buf[2+hdrSize:], uint32(size))
// Message // Message
if _, err := hm.msg.MarshalTo(buf[2+hdrSize+4:]); err != nil { if _, err := msg.MarshalTo(buf[2+hdrSize+4:]); err != nil {
return fmt.Errorf("marshalling message: %v", err) return fmt.Errorf("marshalling message: %v", err)
} }