NewWithConfig creates a new [Pool]. config must have been created by [ParseConfig].
(ctx context.Context, config *Config)
| 218 | |
| 219 | // NewWithConfig creates a new [Pool]. config must have been created by [ParseConfig]. |
| 220 | func NewWithConfig(ctx context.Context, config *Config) (*Pool, error) { |
| 221 | // Default values are set in ParseConfig. Enforce initial creation by ParseConfig rather than setting defaults from |
| 222 | // zero values. |
| 223 | if !config.createdByParseConfig { |
| 224 | panic("config must be created by ParseConfig") |
| 225 | } |
| 226 | |
| 227 | prepareConn := config.PrepareConn |
| 228 | if prepareConn == nil && config.BeforeAcquire != nil { |
| 229 | prepareConn = func(ctx context.Context, conn *pgx.Conn) (bool, error) { |
| 230 | return config.BeforeAcquire(ctx, conn), nil |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | p := &Pool{ |
| 235 | config: config, |
| 236 | beforeConnect: config.BeforeConnect, |
| 237 | afterConnect: config.AfterConnect, |
| 238 | prepareConn: prepareConn, |
| 239 | afterRelease: config.AfterRelease, |
| 240 | beforeClose: config.BeforeClose, |
| 241 | minConns: config.MinConns, |
| 242 | minIdleConns: config.MinIdleConns, |
| 243 | maxConns: config.MaxConns, |
| 244 | maxConnLifetime: config.MaxConnLifetime, |
| 245 | maxConnLifetimeJitter: config.MaxConnLifetimeJitter, |
| 246 | maxConnIdleTime: config.MaxConnIdleTime, |
| 247 | pingTimeout: config.PingTimeout, |
| 248 | healthCheckPeriod: config.HealthCheckPeriod, |
| 249 | healthCheckChan: make(chan struct{}, 1), |
| 250 | closeChan: make(chan struct{}), |
| 251 | } |
| 252 | |
| 253 | if t, ok := config.ConnConfig.Tracer.(AcquireTracer); ok { |
| 254 | p.acquireTracer = t |
| 255 | } |
| 256 | |
| 257 | if t, ok := config.ConnConfig.Tracer.(ReleaseTracer); ok { |
| 258 | p.releaseTracer = t |
| 259 | } |
| 260 | |
| 261 | if config.ShouldPing != nil { |
| 262 | p.shouldPing = config.ShouldPing |
| 263 | } else { |
| 264 | p.shouldPing = func(ctx context.Context, params ShouldPingParams) bool { |
| 265 | return params.IdleDuration > time.Second |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | var err error |
| 270 | p.p, err = puddle.NewPool( |
| 271 | &puddle.Config[*connResource]{ |
| 272 | Constructor: func(ctx context.Context) (*connResource, error) { |
| 273 | p.newConnsCount.Add(1) |
| 274 | connConfig := p.config.ConnConfig.Copy() |
| 275 | |
| 276 | // Connection will continue in background even if Acquire is canceled. Ensure that a connect won't hang forever. |
| 277 | if connConfig.ConnectTimeout <= 0 { |