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