Conditions | 5 |
Total Lines | 66 |
Code Lines | 37 |
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:
1 | package postgres |
||
37 | func New(uri string, opts ...Option) (*Postgres, error) { |
||
38 | pg := &Postgres{ |
||
39 | maxOpenConnections: _defaultMaxOpenConnections, |
||
40 | maxIdleConnections: _defaultMaxIdleConnections, |
||
41 | maxDataPerWrite: _defaultMaxDataPerWrite, |
||
42 | maxRetries: _defaultMaxRetries, |
||
43 | watchBufferSize: _defaultWatchBufferSize, |
||
44 | } |
||
45 | |||
46 | // Custom options |
||
47 | for _, opt := range opts { |
||
48 | opt(pg) |
||
49 | } |
||
50 | |||
51 | pg.Builder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar) |
||
52 | |||
53 | writeConfig, err := pgxpool.ParseConfig(uri) |
||
54 | if err != nil { |
||
55 | return nil, err |
||
56 | } |
||
57 | |||
58 | readConfig, err := pgxpool.ParseConfig(uri) |
||
59 | if err != nil { |
||
60 | return nil, err |
||
61 | } |
||
62 | |||
63 | // Set the default execution mode for queries using the write and read configurations. |
||
64 | setDefaultQueryExecMode(writeConfig.ConnConfig) |
||
65 | setDefaultQueryExecMode(readConfig.ConnConfig) |
||
66 | |||
67 | // Set the plan cache mode for both write and read configurations to optimize query planning. |
||
68 | setPlanCacheMode(writeConfig.ConnConfig) |
||
69 | setPlanCacheMode(readConfig.ConnConfig) |
||
70 | |||
71 | // Set the minimum number of idle connections in the pool for both write and read configurations. |
||
72 | writeConfig.MinConns = int32(pg.maxIdleConnections) |
||
73 | readConfig.MinConns = int32(pg.maxIdleConnections) |
||
74 | |||
75 | // Set the maximum number of active connections in the pool for both write and read configurations. |
||
76 | writeConfig.MaxConns = int32(pg.maxOpenConnections) |
||
77 | readConfig.MaxConns = int32(pg.maxOpenConnections) |
||
78 | |||
79 | // Set the maximum amount of time a connection may be idle before being closed for both configurations. |
||
80 | writeConfig.MaxConnIdleTime = pg.maxConnectionIdleTime |
||
81 | readConfig.MaxConnIdleTime = pg.maxConnectionIdleTime |
||
82 | |||
83 | // Set the maximum lifetime of a connection in the pool for both configurations. |
||
84 | writeConfig.MaxConnLifetime = pg.maxConnectionLifeTime |
||
85 | readConfig.MaxConnLifetime = pg.maxConnectionLifeTime |
||
86 | |||
87 | // Set a jitter to the maximum connection lifetime to prevent all connections from expiring at the same time. |
||
88 | writeConfig.MaxConnLifetimeJitter = time.Duration(0.2 * float64(pg.maxConnectionLifeTime)) |
||
89 | readConfig.MaxConnLifetimeJitter = time.Duration(0.2 * float64(pg.maxConnectionLifeTime)) |
||
90 | |||
91 | // Create connection pools for both writing and reading operations using the configured settings. |
||
92 | pg.WritePool, pg.ReadPool, err = createPools( |
||
93 | context.Background(), // Context used to control the lifecycle of the pools. |
||
94 | writeConfig, // Configuration settings for the write pool. |
||
95 | readConfig, // Configuration settings for the read pool. |
||
96 | ) |
||
97 | // Handle errors during the creation of the connection pools. |
||
98 | if err != nil { |
||
99 | return nil, err |
||
100 | } |
||
101 | |||
102 | return pg, nil |
||
103 | } |
||
234 |