Passed
Pull Request — master (#1232)
by Tolga
02:24
created

postgres.New   B

Complexity

Conditions 7

Size

Total Lines 57
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 37
nop 2
dl 0
loc 57
rs 7.592
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
package postgres
2
3
import (
4
	"context"
5
	"time"
6
7
	"github.com/jackc/pgx/v5"
8
	"github.com/jackc/pgx/v5/pgxpool"
9
10
	"github.com/Masterminds/squirrel"
11
)
12
13
// Postgres - Structure for Postresql instance
14
type Postgres struct {
15
	ReadPool  *pgxpool.Pool
16
	WritePool *pgxpool.Pool
17
18
	Builder squirrel.StatementBuilderType
19
	// options
20
	maxDataPerWrite       int
21
	maxRetries            int
22
	watchBufferSize       int
23
	maxConnectionLifeTime time.Duration
24
	maxConnectionIdleTime time.Duration
25
	maxOpenConnections    int
26
	maxIdleConnections    int
27
	simpleMode            bool
28
}
29
30
// New - Creates new postgresql db instance
31
func New(uri string, opts ...Option) (*Postgres, error) {
32
	pg := &Postgres{
33
		maxOpenConnections: _defaultMaxOpenConnections,
34
		maxIdleConnections: _defaultMaxIdleConnections,
35
		maxDataPerWrite:    _defaultMaxDataPerWrite,
36
		maxRetries:         _defaultMaxRetries,
37
		watchBufferSize:    _defaultWatchBufferSize,
38
		simpleMode:         _defaultSimpleMode,
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
	if pg.simpleMode {
59
		writeConfig.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeSimpleProtocol
60
		readConfig.ConnConfig.DefaultQueryExecMode = pgx.QueryExecModeSimpleProtocol
61
	}
62
63
	writeConfig.MinConns = int32(pg.maxIdleConnections)
64
	readConfig.MinConns = int32(pg.maxIdleConnections)
65
66
	writeConfig.MaxConns = int32(pg.maxOpenConnections)
67
	readConfig.MaxConns = int32(pg.maxOpenConnections)
68
69
	writeConfig.MaxConnIdleTime = pg.maxConnectionIdleTime
70
	readConfig.MaxConnIdleTime = pg.maxConnectionIdleTime
71
72
	writeConfig.MaxConnLifetime = pg.maxConnectionLifeTime
73
	readConfig.MaxConnLifetime = pg.maxConnectionLifeTime
74
75
	initContext, cancelInit := context.WithTimeout(context.Background(), 5*time.Second)
76
	defer cancelInit()
77
78
	pg.WritePool, err = pgxpool.NewWithConfig(initContext, writeConfig)
79
	if err != nil {
80
		return nil, err
81
	}
82
	pg.ReadPool, err = pgxpool.NewWithConfig(initContext, readConfig)
83
	if err != nil {
84
		return nil, err
85
	}
86
87
	return pg, nil
88
}
89
90
func (p *Postgres) GetMaxDataPerWrite() int {
91
	return p.maxDataPerWrite
92
}
93
94
func (p *Postgres) GetMaxRetries() int {
95
	return p.maxRetries
96
}
97
98
func (p *Postgres) GetWatchBufferSize() int {
99
	return p.watchBufferSize
100
}
101
102
// GetEngineType - Get the engine type which is postgresql in string
103
func (p *Postgres) GetEngineType() string {
104
	return "postgres"
105
}
106
107
// Close - Close postgresql instance
108
func (p *Postgres) Close() error {
109
	p.ReadPool.Close()
110
	p.WritePool.Close()
111
	return nil
112
}
113
114
// IsReady - Check if database is ready
115
func (p *Postgres) IsReady(ctx context.Context) (bool, error) {
116
	ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
117
	defer cancel()
118
	if err := p.ReadPool.Ping(ctx); err != nil {
119
		return false, err
120
	}
121
	return true, nil
122
}
123