From 4ee21a15de306889a17140af02c060378ee3feee Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Mon, 29 May 2017 09:18:41 -0500 Subject: [PATCH] Use pgproto3 for startup message --- conn.go | 20 +++++++++----------- messages.go | 28 ---------------------------- 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/conn.go b/conn.go index 6b0cc0c5..2c4f4907 100644 --- a/conn.go +++ b/conn.go @@ -302,27 +302,30 @@ func (c *Conn) connect(config ConnConfig, network, address string, tlsConfig *tl return err } - msg := newStartupMessage() + startupMsg := pgproto3.StartupMessage{ + ProtocolVersion: pgproto3.ProtocolVersionNumber, + Parameters: make(map[string]string), + } // Default to disabling TLS renegotiation. // // Go does not support (https://github.com/golang/go/issues/5742) // PostgreSQL recommends disabling (http://www.postgresql.org/docs/9.4/static/runtime-config-connection.html#GUC-SSL-RENEGOTIATION-LIMIT) if tlsConfig != nil { - msg.options["ssl_renegotiation_limit"] = "0" + startupMsg.Parameters["ssl_renegotiation_limit"] = "0" } // Copy default run-time params for k, v := range config.RuntimeParams { - msg.options[k] = v + startupMsg.Parameters[k] = v } - msg.options["user"] = c.config.User + startupMsg.Parameters["user"] = c.config.User if c.config.Database != "" { - msg.options["database"] = c.config.Database + startupMsg.Parameters["database"] = c.config.Database } - if err = c.txStartupMessage(msg); err != nil { + if _, err := c.conn.Write(startupMsg.Encode(nil)); err != nil { return err } @@ -1272,11 +1275,6 @@ func (c *Conn) startTLS(tlsConfig *tls.Config) (err error) { return nil } -func (c *Conn) txStartupMessage(msg *startupMessage) error { - _, err := c.conn.Write(msg.Bytes()) - return err -} - func (c *Conn) txPasswordMessage(password string) (err error) { buf := c.wbuf buf = append(buf, 'p') diff --git a/messages.go b/messages.go index b96d25c3..f06f8b41 100644 --- a/messages.go +++ b/messages.go @@ -1,43 +1,15 @@ package pgx import ( - "encoding/binary" - "github.com/jackc/pgx/pgtype" ) -const ( - protocolVersionNumber = 196608 // 3.0 -) - const ( copyData = 'd' copyFail = 'f' copyDone = 'c' ) -type startupMessage struct { - options map[string]string -} - -func newStartupMessage() *startupMessage { - return &startupMessage{map[string]string{}} -} - -func (s *startupMessage) Bytes() (buf []byte) { - buf = make([]byte, 8, 128) - binary.BigEndian.PutUint32(buf[4:8], uint32(protocolVersionNumber)) - for key, value := range s.options { - buf = append(buf, key...) - buf = append(buf, 0) - buf = append(buf, value...) - buf = append(buf, 0) - } - buf = append(buf, ("\000")...) - binary.BigEndian.PutUint32(buf[0:4], uint32(len(buf))) - return buf -} - type FieldDescription struct { Name string Table pgtype.Oid