Conditions | 6 |
Total Lines | 57 |
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 |
||
32 | func New(uri string, opts ...Option) (*Postgres, error) { |
||
33 | pg := &Postgres{ |
||
34 | maxOpenConnections: _defaultMaxOpenConnections, |
||
35 | maxIdleConnections: _defaultMaxIdleConnections, |
||
36 | maxDataPerWrite: _defaultMaxDataPerWrite, |
||
37 | maxRetries: _defaultMaxRetries, |
||
38 | watchBufferSize: _defaultWatchBufferSize, |
||
39 | } |
||
40 | |||
41 | // Custom options |
||
42 | for _, opt := range opts { |
||
43 | opt(pg) |
||
44 | } |
||
45 | |||
46 | pg.Builder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar) |
||
47 | |||
48 | writeConfig, err := pgxpool.ParseConfig(uri) |
||
49 | if err != nil { |
||
50 | return nil, err |
||
51 | } |
||
52 | |||
53 | readConfig, err := pgxpool.ParseConfig(uri) |
||
54 | if err != nil { |
||
55 | return nil, err |
||
56 | } |
||
57 | |||
58 | setDefaultQueryExecMode(writeConfig.ConnConfig) |
||
59 | setDefaultQueryExecMode(readConfig.ConnConfig) |
||
60 | |||
61 | writeConfig.MinConns = int32(pg.maxIdleConnections) |
||
62 | readConfig.MinConns = int32(pg.maxIdleConnections) |
||
63 | |||
64 | writeConfig.MaxConns = int32(pg.maxOpenConnections) |
||
65 | readConfig.MaxConns = int32(pg.maxOpenConnections) |
||
66 | |||
67 | writeConfig.MaxConnIdleTime = pg.maxConnectionIdleTime |
||
68 | readConfig.MaxConnIdleTime = pg.maxConnectionIdleTime |
||
69 | |||
70 | writeConfig.MaxConnLifetime = pg.maxConnectionLifeTime |
||
71 | readConfig.MaxConnLifetime = pg.maxConnectionLifeTime |
||
72 | |||
73 | writeConfig.MaxConnLifetimeJitter = time.Duration(0.2 * float64(pg.maxConnectionLifeTime)) |
||
74 | readConfig.MaxConnLifetimeJitter = time.Duration(0.2 * float64(pg.maxConnectionLifeTime)) |
||
75 | |||
76 | initialContext, cancelInit := context.WithTimeout(context.Background(), 5*time.Second) |
||
77 | defer cancelInit() |
||
78 | |||
79 | pg.WritePool, err = pgxpool.NewWithConfig(initialContext, writeConfig) |
||
80 | if err != nil { |
||
81 | return nil, err |
||
82 | } |
||
83 | pg.ReadPool, err = pgxpool.NewWithConfig(initialContext, readConfig) |
||
84 | if err != nil { |
||
85 | return nil, err |
||
86 | } |
||
87 | |||
88 | return pg, nil |
||
89 | } |
||
148 |