Use pgproto3 for startup message

This commit is contained in:
Jack Christensen 2017-05-29 09:18:41 -05:00
parent 4ca7ad1207
commit 4ee21a15de
2 changed files with 9 additions and 39 deletions

20
conn.go
View File

@ -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')

View File

@ -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