mirror of https://github.com/jackc/pgx.git
ConnectConfig copies config
parent
759e47dba3
commit
9201cc0341
15
conn.go
15
conn.go
|
@ -113,6 +113,10 @@ func Connect(ctx context.Context, connString string) (*Conn, error) {
|
||||||
// ConnectConfig establishes a connection with a PostgreSQL server with a configuration struct.
|
// ConnectConfig establishes a connection with a PostgreSQL server with a configuration struct.
|
||||||
// connConfig must have been created by ParseConfig.
|
// connConfig must have been created by ParseConfig.
|
||||||
func ConnectConfig(ctx context.Context, connConfig *ConnConfig) (*Conn, error) {
|
func ConnectConfig(ctx context.Context, connConfig *ConnConfig) (*Conn, error) {
|
||||||
|
// In general this improves safety. In particular avoid the config.Config.OnNotification mutation from affecting other
|
||||||
|
// connections with the same config. See https://github.com/jackc/pgx/issues/618.
|
||||||
|
connConfig = connConfig.Copy()
|
||||||
|
|
||||||
return connect(ctx, connConfig)
|
return connect(ctx, connConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,23 +192,16 @@ func ParseConfig(connString string) (*ConnConfig, error) {
|
||||||
return connConfig, nil
|
return connConfig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// connect connects to a database. connect takes ownership of config. The caller must not use or access it again.
|
||||||
func connect(ctx context.Context, config *ConnConfig) (c *Conn, err error) {
|
func connect(ctx context.Context, config *ConnConfig) (c *Conn, err error) {
|
||||||
// Default values are set in ParseConfig. Enforce initial creation by ParseConfig rather than setting defaults from
|
// Default values are set in ParseConfig. Enforce initial creation by ParseConfig rather than setting defaults from
|
||||||
// zero values.
|
// zero values.
|
||||||
if !config.createdByParseConfig {
|
if !config.createdByParseConfig {
|
||||||
panic("config must be created by ParseConfig")
|
panic("config must be created by ParseConfig")
|
||||||
}
|
}
|
||||||
originalConfig := config
|
|
||||||
|
|
||||||
// This isn't really a deep copy. But it is enough to avoid the config.Config.OnNotification mutation from affecting
|
|
||||||
// other connections with the same config. See https://github.com/jackc/pgx/issues/618.
|
|
||||||
{
|
|
||||||
configCopy := *config
|
|
||||||
config = &configCopy
|
|
||||||
}
|
|
||||||
|
|
||||||
c = &Conn{
|
c = &Conn{
|
||||||
config: originalConfig,
|
config: config,
|
||||||
typeMap: pgtype.NewMap(),
|
typeMap: pgtype.NewMap(),
|
||||||
logLevel: config.LogLevel,
|
logLevel: config.LogLevel,
|
||||||
logger: config.Logger,
|
logger: config.Logger,
|
||||||
|
|
Loading…
Reference in New Issue