-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres.go
More file actions
132 lines (112 loc) · 2.81 KB
/
postgres.go
File metadata and controls
132 lines (112 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Package postgres implements postgres connection.
package postgres
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
const (
_defaultMaxPoolSize = 1
_defaultConnAttempts = 10
_defaultConnTimeout = 3 * time.Second
)
// Postgres -.
type Postgres struct {
maxPoolSize int
connAttempts int
connTimeout time.Duration
Builder squirrel.StatementBuilderType
pool *pgxpool.Pool
logger Logger
connected time.Time
}
// New -.
func New(url string, opts ...Option) (*Postgres, error) {
pg := &Postgres{
maxPoolSize: _defaultMaxPoolSize,
connAttempts: _defaultConnAttempts,
connTimeout: _defaultConnTimeout,
}
for _, opt := range opts {
opt(pg)
}
// builder
pg.Builder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)
// pool
poolConfig, err := pgxpool.ParseConfig(url)
if err != nil {
return nil, err
}
poolConfig.MaxConns = int32(pg.maxPoolSize)
for pg.connAttempts > 0 {
pg.pool, err = pgxpool.ConnectConfig(context.Background(), poolConfig)
if err == nil {
pg.connected = time.Now()
return pg, nil
}
pg.Warnf("Postgres trying connect, attempts left: %d\n", pg.connAttempts)
pg.connAttempts--
time.Sleep(pg.connTimeout)
}
return nil, errors.New("Postgres: connection attempts exceeded")
}
// Close -.
func (p *Postgres) Close() {
if p.pool != nil {
p.pool.Close()
totalTime := time.Since(p.connected).String()
p.Infof("Postgres: closed, total connection time: %s\n", totalTime)
}
}
// Pool -.
func (p *Postgres) Pool() *pgxpool.Pool {
return p.pool
}
// BeginTransaction -.
func (p *Postgres) BeginTransaction() (pgx.Tx, error) {
tx, err := p.Pool().Begin(context.Background())
if err != nil {
return nil, fmt.Errorf("Postgres: error on begin transaction: %s", err)
}
return tx, nil
}
// EndTransaction -.
func (p *Postgres) EndTransaction(tx pgx.Tx, err error) {
if err != nil {
rollErr := tx.Rollback(context.Background())
if rollErr != nil {
p.Errorf("Postgres: error on rollback transaction: %s\n", rollErr)
}
} else {
commitErr := tx.Commit(context.Background())
if commitErr != nil {
p.Errorf("Postgres: error on commit transaction: %s\n", commitErr)
}
}
}
func (p *Postgres) Infof(format string, args ...interface{}) {
if p.logger != nil {
p.logger.Infof(strings.ReplaceAll(format, "\n", ""), args...)
} else {
fmt.Printf(format, args...)
}
}
func (p *Postgres) Warnf(format string, args ...interface{}) {
if p.logger != nil {
p.logger.Warnf(strings.ReplaceAll(format, "\n", ""), args...)
} else {
fmt.Printf(format, args...)
}
}
func (p *Postgres) Errorf(format string, args ...interface{}) {
if p.logger != nil {
p.logger.Errorf(strings.ReplaceAll(format, "\n", ""), args...)
} else {
fmt.Printf(format, args...)
}
}