Make message buffer size configurable

This commit is contained in:
Jack Christensen 2013-07-26 15:10:19 -05:00
parent 3873a83a0a
commit c36c0875c9

View File

@ -24,6 +24,7 @@ type ConnectionParameters struct {
Database string Database string
User string User string
Password string Password string
MsgBufSize int // Size of work buffer used for transcoding messages. For optimal performance, it should be large enough to store a single row from any result set. Default: 1024
} }
// Connection is a PostgreSQL connection handle. It is not safe for concurrent usage. // Connection is a PostgreSQL connection handle. It is not safe for concurrent usage.
@ -33,6 +34,7 @@ type Connection struct {
conn net.Conn // the underlying TCP or unix domain socket connection conn net.Conn // the underlying TCP or unix domain socket connection
writer *bufio.Writer // buffered writer to avoid sending tiny packets writer *bufio.Writer // buffered writer to avoid sending tiny packets
buf *bytes.Buffer // work buffer to avoid constant alloc and dealloc buf *bytes.Buffer // work buffer to avoid constant alloc and dealloc
bufSize int // desired size of buf
Pid int32 // backend pid Pid int32 // backend pid
SecretKey int32 // key to use to send a cancel query message to the server SecretKey int32 // key to use to send a cancel query message to the server
RuntimeParams map[string]string // parameters that have been reported by the server RuntimeParams map[string]string // parameters that have been reported by the server
@ -83,9 +85,6 @@ func (e ProtocolError) Error() string {
return string(e) return string(e)
} }
// sharedBufferSize is the default number of bytes of work buffer per connection
const sharedBufferSize = 1024
// Connect establishes a connection with a PostgreSQL server using parameters. One // Connect establishes a connection with a PostgreSQL server using parameters. One
// of parameters.Socket or parameters.Host must be specified. parameters.User must // of parameters.Socket or parameters.Host must be specified. parameters.User must
// also the included. Other parameters fields are optional. // also the included. Other parameters fields are optional.
@ -96,6 +95,9 @@ func Connect(parameters ConnectionParameters) (c *Connection, err error) {
if c.parameters.Port == 0 { if c.parameters.Port == 0 {
c.parameters.Port = 5432 c.parameters.Port = 5432
} }
if c.parameters.MsgBufSize == 0 {
c.parameters.MsgBufSize = 1024
}
if c.parameters.Socket != "" { if c.parameters.Socket != "" {
c.conn, err = net.Dial("unix", c.parameters.Socket) c.conn, err = net.Dial("unix", c.parameters.Socket)
@ -115,7 +117,8 @@ func Connect(parameters ConnectionParameters) (c *Connection, err error) {
}() }()
c.writer = bufio.NewWriter(c.conn) c.writer = bufio.NewWriter(c.conn)
c.buf = bytes.NewBuffer(make([]byte, 0, sharedBufferSize)) c.bufSize = c.parameters.MsgBufSize
c.buf = bytes.NewBuffer(make([]byte, 0, c.bufSize))
c.RuntimeParams = make(map[string]string) c.RuntimeParams = make(map[string]string)
c.preparedStatements = make(map[string]*preparedStatement) c.preparedStatements = make(map[string]*preparedStatement)
@ -819,8 +822,8 @@ func (c *Connection) txPasswordMessage(password string) (err error) {
// old one can get GC'ed // old one can get GC'ed
func (c *Connection) getBuf() *bytes.Buffer { func (c *Connection) getBuf() *bytes.Buffer {
c.buf.Reset() c.buf.Reset()
if cap(c.buf.Bytes()) > sharedBufferSize { if cap(c.buf.Bytes()) > c.bufSize {
c.buf = bytes.NewBuffer(make([]byte, 0, sharedBufferSize)) c.buf = bytes.NewBuffer(make([]byte, 0, c.bufSize))
} }
return c.buf return c.buf
} }