-
Notifications
You must be signed in to change notification settings - Fork 8
/
db.go
73 lines (61 loc) · 1.48 KB
/
db.go
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
package db
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
type Client struct {
*sqlx.DB
queryTimeOut time.Duration
}
// NewClient creates a new sqlx database client
func New(cfg Config) (*Client, error) {
db, err := sqlx.Connect(cfg.Driver, cfg.URL)
if err != nil {
return nil, err
}
db.SetMaxIdleConns(cfg.MaxIdleConns)
db.SetMaxOpenConns(cfg.MaxOpenConns)
db.SetConnMaxLifetime(cfg.ConnMaxLifeTime)
return &Client{DB: db, queryTimeOut: cfg.MaxQueryTimeout}, err
}
func (c Client) WithTimeout(ctx context.Context, op func(ctx context.Context) error) (err error) {
ctxWithTimeout, cancel := context.WithTimeout(ctx, c.queryTimeOut)
defer cancel()
return op(ctxWithTimeout)
}
func (c Client) WithTxn(ctx context.Context, txnOptions sql.TxOptions, txFunc func(*sqlx.Tx) error) (err error) {
txn, err := c.BeginTxx(ctx, &txnOptions)
if err != nil {
return err
}
defer func() {
if p := recover(); p != nil {
switch p := p.(type) {
case error:
err = p
default:
err = errors.Errorf("%s", p)
}
err = txn.Rollback()
panic(p)
} else if err != nil {
if rlbErr := txn.Rollback(); err != nil {
err = fmt.Errorf("rollback error: %s while executing: %w", rlbErr, err)
} else {
err = fmt.Errorf("rollback: %w", err)
}
} else {
err = txn.Commit()
}
}()
err = txFunc(txn)
return err
}
// Close closes the database connection
func (c *Client) Close() error {
return c.DB.Close()
}