MCPcopy Index your code
hub / github.com/Permify/permify / createPools

Function createPools

pkg/database/postgres/postgres.go:273–316  ·  view source on GitHub ↗

createPools initializes read and write connection pools with appropriate configurations and error handling.

(ctx context.Context, wConfig, rConfig *pgxpool.Config)

Source from the content-addressed store, hash-verified

271
272// createPools initializes read and write connection pools with appropriate configurations and error handling.
273func createPools(ctx context.Context, wConfig, rConfig *pgxpool.Config) (*pgxpool.Pool, *pgxpool.Pool, error) {
274 // Context with timeout for creating the pools
275 initCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
276 defer cancel()
277
278 // Create write pool
279 writePool, err := pgxpool.NewWithConfig(initCtx, wConfig)
280 if err != nil {
281 return nil, nil, fmt.Errorf("failed to create write pool: %w", err)
282 }
283
284 // Create read pool using the same configuration
285 readPool, err := pgxpool.NewWithConfig(initCtx, rConfig)
286 if err != nil {
287 writePool.Close() // Ensure write pool is closed on failure
288 return nil, nil, fmt.Errorf("failed to create read pool: %w", err)
289 }
290
291 // Set up retry policy for pinging pools
292 retryPolicy := backoff.NewExponentialBackOff()
293 retryPolicy.MaxElapsedTime = 1 * time.Minute
294
295 // Attempt to ping both pools to confirm connectivity
296 err = backoff.Retry(func() error {
297 pingCtx, pingCancel := context.WithTimeout(context.Background(), 2*time.Second)
298 defer pingCancel()
299
300 if err := writePool.Ping(pingCtx); err != nil {
301 return fmt.Errorf("write pool ping failed: %w", err)
302 }
303 if err := readPool.Ping(pingCtx); err != nil {
304 return fmt.Errorf("read pool ping failed: %w", err)
305 }
306 return nil
307 }, retryPolicy)
308 // Handle errors from pinging
309 if err != nil {
310 writePool.Close()
311 readPool.Close()
312 return nil, nil, fmt.Errorf("pinging pools failed: %w", err)
313 }
314
315 return writePool, readPool, nil
316}

Callers 1

newDBFunction · 0.85

Calls 1

CloseMethod · 0.65

Tested by

no test coverage detected