| Conditions | 13 |
| Total Lines | 110 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like postgres.newDB often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package postgres |
||
| 53 | func newDB(writerUri, readerUri string, opts ...Option) (*Postgres, error) { |
||
| 54 | pg := &Postgres{ |
||
| 55 | maxConns: _defaultMaxConns, |
||
| 56 | maxIdleConnections: _defaultMaxIdleConnections, |
||
| 57 | minConns: _defaultMinConns, |
||
| 58 | minIdleConns: _defaultMinIdleConns, |
||
| 59 | maxDataPerWrite: _defaultMaxDataPerWrite, |
||
| 60 | maxRetries: _defaultMaxRetries, |
||
| 61 | watchBufferSize: _defaultWatchBufferSize, |
||
| 62 | healthCheckPeriod: _defaultHealthCheckPeriod, |
||
| 63 | maxConnLifetimeJitter: _defaultMaxConnLifetimeJitter, |
||
| 64 | connectTimeout: _defaultConnectTimeout, |
||
| 65 | } |
||
| 66 | |||
| 67 | // Custom options |
||
| 68 | for _, opt := range opts { |
||
| 69 | opt(pg) |
||
| 70 | } |
||
| 71 | |||
| 72 | pg.Builder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar) |
||
| 73 | |||
| 74 | writeConfig, err := pgxpool.ParseConfig(writerUri) |
||
| 75 | if err != nil { |
||
| 76 | return nil, err |
||
| 77 | } |
||
| 78 | |||
| 79 | readConfig, err := pgxpool.ParseConfig(readerUri) |
||
| 80 | if err != nil { |
||
| 81 | return nil, err |
||
| 82 | } |
||
| 83 | |||
| 84 | // Set the default execution mode for queries using the write and read configurations. |
||
| 85 | setDefaultQueryExecMode(writeConfig.ConnConfig) |
||
| 86 | setDefaultQueryExecMode(readConfig.ConnConfig) |
||
| 87 | |||
| 88 | // Set the plan cache mode for both write and read configurations to optimize query planning. |
||
| 89 | setPlanCacheMode(writeConfig.ConnConfig) |
||
| 90 | setPlanCacheMode(readConfig.ConnConfig) |
||
| 91 | |||
| 92 | // Set the minimum number of connections in the pool for both write and read configurations. |
||
| 93 | // For backward compatibility: if MinConns is not set (0) but MaxIdleConnections is set, use MaxIdleConnections (old behavior). |
||
| 94 | minConns := pg.minConns |
||
| 95 | if minConns == 0 && pg.maxIdleConnections > 0 { |
||
| 96 | minConns = pg.maxIdleConnections |
||
| 97 | } |
||
| 98 | if minConns > 0 { |
||
| 99 | writeConfig.MinConns = int32(minConns) |
||
| 100 | readConfig.MinConns = int32(minConns) |
||
| 101 | } |
||
| 102 | |||
| 103 | // Set the minimum number of idle connections in the pool. |
||
| 104 | // Note: MinIdleConns was not set in the old code, so we only set it if explicitly configured. |
||
| 105 | if pg.minIdleConns > 0 { |
||
| 106 | writeConfig.MinIdleConns = int32(pg.minIdleConns) |
||
| 107 | readConfig.MinIdleConns = int32(pg.minIdleConns) |
||
| 108 | } |
||
| 109 | |||
| 110 | // Set the maximum number of connections in the pool for both write and read configurations. |
||
| 111 | // pgxpool default is 0 (unlimited), so only set if explicitly configured. |
||
| 112 | // Note: MaxOpenConnections is already mapped to MaxConns via options.go, so no backward compatibility needed here. |
||
| 113 | if pg.maxConns > 0 { |
||
| 114 | writeConfig.MaxConns = int32(pg.maxConns) |
||
| 115 | readConfig.MaxConns = int32(pg.maxConns) |
||
| 116 | } |
||
| 117 | |||
| 118 | // Set the maximum amount of time a connection may be idle before being closed for both configurations. |
||
| 119 | writeConfig.MaxConnIdleTime = pg.maxConnectionIdleTime |
||
| 120 | readConfig.MaxConnIdleTime = pg.maxConnectionIdleTime |
||
| 121 | |||
| 122 | // Set the maximum lifetime of a connection in the pool for both configurations. |
||
| 123 | writeConfig.MaxConnLifetime = pg.maxConnectionLifeTime |
||
| 124 | readConfig.MaxConnLifetime = pg.maxConnectionLifeTime |
||
| 125 | |||
| 126 | // Set a jitter to the maximum connection lifetime to prevent all connections from expiring at the same time. |
||
| 127 | if pg.maxConnLifetimeJitter > 0 { |
||
| 128 | writeConfig.MaxConnLifetimeJitter = pg.maxConnLifetimeJitter |
||
| 129 | readConfig.MaxConnLifetimeJitter = pg.maxConnLifetimeJitter |
||
| 130 | } else { |
||
| 131 | // Default to 20% of MaxConnLifetime if not explicitly set |
||
| 132 | writeConfig.MaxConnLifetimeJitter = time.Duration(0.2 * float64(pg.maxConnectionLifeTime)) |
||
| 133 | readConfig.MaxConnLifetimeJitter = time.Duration(0.2 * float64(pg.maxConnectionLifeTime)) |
||
| 134 | } |
||
| 135 | |||
| 136 | // Set the health check period for both configurations. |
||
| 137 | if pg.healthCheckPeriod > 0 { |
||
| 138 | writeConfig.HealthCheckPeriod = pg.healthCheckPeriod |
||
| 139 | readConfig.HealthCheckPeriod = pg.healthCheckPeriod |
||
| 140 | } |
||
| 141 | |||
| 142 | // Set the connect timeout for both configurations. |
||
| 143 | if pg.connectTimeout > 0 { |
||
| 144 | writeConfig.ConnConfig.ConnectTimeout = pg.connectTimeout |
||
| 145 | readConfig.ConnConfig.ConnectTimeout = pg.connectTimeout |
||
| 146 | } |
||
| 147 | |||
| 148 | writeConfig.ConnConfig.Tracer = otelpgx.NewTracer() |
||
| 149 | readConfig.ConnConfig.Tracer = otelpgx.NewTracer() |
||
| 150 | |||
| 151 | // Create connection pools for both writing and reading operations using the configured settings. |
||
| 152 | pg.WritePool, pg.ReadPool, err = createPools( |
||
| 153 | context.Background(), // Context used to control the lifecycle of the pools. |
||
| 154 | writeConfig, // Configuration settings for the write pool. |
||
| 155 | readConfig, // Configuration settings for the read pool. |
||
| 156 | ) |
||
| 157 | // Handle errors during the creation of the connection pools. |
||
| 158 | if err != nil { |
||
| 159 | return nil, err |
||
| 160 | } |
||
| 161 | |||
| 162 | return pg, nil |
||
| 163 | } |
||
| 305 |