Skip to content

Commit

Permalink
pgwire: conn holds writeBuffer by value, not reference
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanbenschoten committed Sep 25, 2018
1 parent b8f7eba commit ab71616
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
10 changes: 5 additions & 5 deletions pkg/sql/pgwire/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type conn struct {
}

readBuf pgwirebase.ReadBuffer
msgBuilder *writeBuffer
msgBuilder writeBuffer
}

// serveConn creates a conn that will serve the netConn. It returns once the
Expand Down Expand Up @@ -173,12 +173,12 @@ func newConn(
execCfg: execCfg,
metrics: metrics,
rd: *bufio.NewReader(netConn),
msgBuilder: newWriteBuffer(metrics.BytesOutCount),
}
c.stmtBuf.Init()
c.writerState.fi.buf = &c.writerState.buf
c.writerState.fi.lastFlushed = -1
c.writerState.fi.cmdStarts = make(map[sql.CmdPos]int)
c.msgBuilder.init(metrics.BytesOutCount)

return c
}
Expand Down Expand Up @@ -377,7 +377,7 @@ Loop:
// and flushing the buffer.
if ctxCanceled || draining() {
_ /* err */ = writeErr(
newAdminShutdownErr(err), c.msgBuilder, &c.writerState.buf)
newAdminShutdownErr(err), &c.msgBuilder, &c.writerState.buf)
_ /* n */, _ /* err */ = c.writerState.buf.WriteTo(c.conn)

// Swallow whatever error we might have gotten from the writer. If we're
Expand Down Expand Up @@ -900,7 +900,7 @@ func (c *conn) bufferCommandComplete(tag []byte) {
}

func (c *conn) bufferErr(err error) {
if err := writeErr(err, c.msgBuilder, &c.writerState.buf); err != nil {
if err := writeErr(err, &c.msgBuilder, &c.writerState.buf); err != nil {
panic(fmt.Sprintf("unexpected err from buffer: %s", err))
}
}
Expand Down Expand Up @@ -1224,7 +1224,7 @@ func (r *pgwireReader) ReadByte() (byte, error) {
func (c *conn) handleAuthentication(ctx context.Context, insecure bool) error {

sendError := func(err error) error {
_ /* err */ = writeErr(err, c.msgBuilder, c.conn)
_ /* err */ = writeErr(err, &c.msgBuilder, c.conn)
return err
}

Expand Down
12 changes: 8 additions & 4 deletions pkg/sql/pgwire/write_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ type writeBuffer struct {
}

func newWriteBuffer(bytecount *metric.Counter) *writeBuffer {
b := &writeBuffer{
bytecount: bytecount,
}
b.textFormatter = tree.MakeFmtCtx(&b.variablePutbuf, tree.FmtPgwireText)
b := new(writeBuffer)
b.init(bytecount)
return b
}

// init exists to avoid the allocation imposed by newWriteBuffer.
func (b *writeBuffer) init(bytecount *metric.Counter) {
b.bytecount = bytecount
b.textFormatter = tree.MakeFmtCtx(&b.variablePutbuf, tree.FmtPgwireText)
}

// Write implements the io.Write interface.
func (b *writeBuffer) Write(p []byte) (int, error) {
b.write(p)
Expand Down

0 comments on commit ab71616

Please sign in to comment.