|
| 1 | +// Copyright © 2022 by PACE Telematics GmbH. All rights reserved. |
| 2 | +// Created at 2022/03/09 by Sascha Voth |
| 3 | + |
| 4 | +package postgres |
| 5 | + |
| 6 | +import ( |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type ConfigOption func(cfg *Config) |
| 11 | + |
| 12 | +// WithPort - customize the db port |
| 13 | +func WithPort(port int) ConfigOption { |
| 14 | + return func(cfg *Config) { |
| 15 | + cfg.Port = port |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// WithHost - customise the db host |
| 20 | +func WithHost(host string) ConfigOption { |
| 21 | + return func(cfg *Config) { |
| 22 | + cfg.Host = host |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// WithPassword - customise the db password |
| 27 | +func WithPassword(password string) ConfigOption { |
| 28 | + return func(cfg *Config) { |
| 29 | + cfg.Password = password |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// WithUser - customise the db user |
| 34 | +func WithUser(user string) ConfigOption { |
| 35 | + return func(cfg *Config) { |
| 36 | + cfg.User = user |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// WithDatabase - customise the db name |
| 41 | +func WithDatabase(database string) ConfigOption { |
| 42 | + return func(cfg *Config) { |
| 43 | + cfg.Database = database |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// WithApplicationName -ApplicationName is the application name. Used in logs on Pg side. |
| 48 | +// Only available from pg-9.0. |
| 49 | +func WithApplicationName(applicationName string) ConfigOption { |
| 50 | + return func(cfg *Config) { |
| 51 | + cfg.ApplicationName = applicationName |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// WithMaxRetries - Maximum number of retries before giving up. |
| 56 | +func WithMaxRetries(maxRetries int) ConfigOption { |
| 57 | + return func(cfg *Config) { |
| 58 | + cfg.MaxRetries = maxRetries |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// WithRetryStatementTimeout - Whether to retry queries cancelled because of statement_timeout. |
| 63 | +func WithRetryStatementTimeout(retryStatementTimeout bool) ConfigOption { |
| 64 | + return func(cfg *Config) { |
| 65 | + cfg.RetryStatementTimeout = retryStatementTimeout |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// WithMinRetryBackoff - Minimum backoff between each retry. |
| 70 | +// -1 disables backoff. |
| 71 | +func WithMinRetryBackoff(minRetryBackoff time.Duration) ConfigOption { |
| 72 | + return func(cfg *Config) { |
| 73 | + cfg.MinRetryBackoff = minRetryBackoff |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// WithMaxRetryBackoff - Maximum backoff between each retry. |
| 78 | +// -1 disables backoff. |
| 79 | +func WithMaxRetryBackoff(maxRetryBackoff time.Duration) ConfigOption { |
| 80 | + return func(cfg *Config) { |
| 81 | + cfg.MaxRetryBackoff = maxRetryBackoff |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// WithDialTimeout - Dial timeout for establishing new connections. |
| 86 | +func WithDialTimeout(dialTimeout time.Duration) ConfigOption { |
| 87 | + return func(cfg *Config) { |
| 88 | + cfg.DialTimeout = dialTimeout |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// WithReadTimeout - Timeout for socket reads. If reached, commands will fail |
| 93 | +// with a timeout instead of blocking. |
| 94 | +func WithReadTimeout(readTimeout time.Duration) ConfigOption { |
| 95 | + return func(cfg *Config) { |
| 96 | + cfg.ReadTimeout = readTimeout |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// WithWriteTimeout - Timeout for socket writes. If reached, commands will fail |
| 101 | +// with a timeout instead of blocking. |
| 102 | +func WithWriteTimeout(writeTimeout time.Duration) ConfigOption { |
| 103 | + return func(cfg *Config) { |
| 104 | + cfg.WriteTimeout = writeTimeout |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// WithPoolSize - Maximum number of socket connections. |
| 109 | +func WithPoolSize(poolSize int) ConfigOption { |
| 110 | + return func(cfg *Config) { |
| 111 | + cfg.PoolSize = poolSize |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// WithMinIdleConns - Minimum number of idle connections which is useful when establishing |
| 116 | +// new connection is slow. |
| 117 | +func WithMinIdleConns(minIdleConns int) ConfigOption { |
| 118 | + return func(cfg *Config) { |
| 119 | + cfg.MinIdleConns = minIdleConns |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// WithMaxConnAge - Connection age at which client retires (closes) the connection. |
| 124 | +// It is useful with proxies like PgBouncer and HAProxy. |
| 125 | +func WithMaxConnAge(maxConnAge time.Duration) ConfigOption { |
| 126 | + return func(cfg *Config) { |
| 127 | + cfg.MaxConnAge = maxConnAge |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// WithPoolTimeout - Time for which client waits for free connection if all |
| 132 | +// connections are busy before returning an error. |
| 133 | +func WithPoolTimeout(poolTimeout time.Duration) ConfigOption { |
| 134 | + return func(cfg *Config) { |
| 135 | + cfg.PoolTimeout = poolTimeout |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// WithIdleTimeout - Amount of time after which client closes idle connections. |
| 140 | +// Should be less than server's timeout. |
| 141 | +// -1 disables idle timeout check. |
| 142 | +func WithIdleTimeout(idleTimeout time.Duration) ConfigOption { |
| 143 | + return func(cfg *Config) { |
| 144 | + cfg.IdleTimeout = idleTimeout |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// WithIdleCheckFrequency - Frequency of idle checks made by idle connection's reaper. |
| 149 | +// -1 disables idle connection's reaper, |
| 150 | +// but idle connections are still discarded by the client |
| 151 | +// if IdleTimeout is set. |
| 152 | +func WithIdleCheckFrequency(idleCheckFrequency time.Duration) ConfigOption { |
| 153 | + return func(cfg *Config) { |
| 154 | + cfg.IdleCheckFrequency = idleCheckFrequency |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// WithHealthCheckTableName - Name of the Table that is created to try if database is writeable |
| 159 | +func WithHealthCheckTableName(healthCheckTableName string) ConfigOption { |
| 160 | + return func(cfg *Config) { |
| 161 | + cfg.HealthCheckTableName = healthCheckTableName |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// WithHealthCheckResultTTL - Amount of time to cache the last health check result |
| 166 | +func WithHealthCheckResultTTL(healthCheckResultTTL time.Duration) ConfigOption { |
| 167 | + return func(cfg *Config) { |
| 168 | + cfg.HealthCheckResultTTL = healthCheckResultTTL |
| 169 | + } |
| 170 | +} |
0 commit comments