1
|
|
|
package postgres |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"database/sql" |
6
|
|
|
"time" |
7
|
|
|
|
8
|
|
|
"github.com/cenkalti/backoff/v4" |
9
|
|
|
|
10
|
|
|
"github.com/Masterminds/squirrel" |
11
|
|
|
|
12
|
|
|
_ "github.com/jackc/pgx/v5/stdlib" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
// Postgres - Structure for Postresql instance |
16
|
|
|
type Postgres struct { |
17
|
|
|
DB *sql.DB |
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
|
|
|
} |
28
|
|
|
|
29
|
|
|
// New - Creates new postgresql db instance |
30
|
|
|
func New(uri string, opts ...Option) (*Postgres, error) { |
31
|
|
|
pg := &Postgres{ |
32
|
|
|
maxOpenConnections: _defaultMaxOpenConnections, |
33
|
|
|
maxIdleConnections: _defaultMaxIdleConnections, |
34
|
|
|
maxDataPerWrite: _defaultMaxDataPerWrite, |
35
|
|
|
maxRetries: _defaultMaxRetries, |
36
|
|
|
watchBufferSize: _defaultWatchBufferSize, |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Custom options |
40
|
|
|
for _, opt := range opts { |
41
|
|
|
opt(pg) |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
pg.Builder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar) |
45
|
|
|
|
46
|
|
|
db, err := sql.Open("pgx", uri) |
47
|
|
|
if err != nil { |
48
|
|
|
return nil, err |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if pg.maxOpenConnections != 0 { |
52
|
|
|
db.SetMaxOpenConns(pg.maxOpenConnections) |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if pg.maxIdleConnections != 0 { |
56
|
|
|
db.SetMaxIdleConns(pg.maxIdleConnections) |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if pg.maxConnectionLifeTime != 0 { |
60
|
|
|
db.SetConnMaxLifetime(pg.maxConnectionLifeTime) |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if pg.maxConnectionIdleTime != 0 { |
64
|
|
|
db.SetConnMaxIdleTime(pg.maxConnectionIdleTime) |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
policy := backoff.NewExponentialBackOff() |
68
|
|
|
policy.MaxElapsedTime = 1 * time.Minute |
69
|
|
|
err = backoff.Retry(func() error { |
70
|
|
|
err = db.PingContext(context.Background()) |
71
|
|
|
if err != nil { |
72
|
|
|
return err |
73
|
|
|
} |
74
|
|
|
return nil |
75
|
|
|
}, policy) |
76
|
|
|
if err != nil { |
77
|
|
|
return nil, err |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
pg.DB = db |
81
|
|
|
return pg, nil |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
func (p *Postgres) GetMaxDataPerWrite() int { |
85
|
|
|
return p.maxDataPerWrite |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
func (p *Postgres) GetMaxRetries() int { |
89
|
|
|
return p.maxRetries |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
func (p *Postgres) GetWatchBufferSize() int { |
93
|
|
|
return p.watchBufferSize |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// GetEngineType - Get the engine type which is postgresql in string |
97
|
|
|
func (p *Postgres) GetEngineType() string { |
98
|
|
|
return "postgres" |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Close - Close postgresql instance |
102
|
|
|
func (p *Postgres) Close() error { |
103
|
|
|
if p.DB != nil { |
104
|
|
|
return p.DB.Close() |
105
|
|
|
} |
106
|
|
|
return nil |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// IsReady - Check if database is ready |
110
|
|
|
func (p *Postgres) IsReady(ctx context.Context) (bool, error) { |
111
|
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) |
112
|
|
|
defer cancel() |
113
|
|
|
if err := p.DB.PingContext(ctx); err != nil { |
114
|
|
|
return false, err |
115
|
|
|
} |
116
|
|
|
return true, nil |
117
|
|
|
} |
118
|
|
|
|