mirror of
https://github.com/jackc/pgx.git
synced 2025-05-30 19:22:19 +00:00
Use pgproto3 for startup message
This commit is contained in:
parent
4ca7ad1207
commit
4ee21a15de
20
conn.go
20
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')
|
||||
|
28
messages.go
28
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user