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