-
Notifications
You must be signed in to change notification settings - Fork 121
/
conf.go
82 lines (65 loc) · 2.13 KB
/
conf.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
74
75
76
77
78
79
80
81
82
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"errors"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/jmoiron/sqlx"
)
const (
defaultRetryMaxAttempts = 3
)
// ConnPoolConf holds common SQL connection pool settings.
type ConnPoolConf struct {
MaxLifetime time.Duration `yaml:"maxLifeTime"`
MaxIdleTime time.Duration `yaml:"maxIdleTime"`
MaxOpen uint `yaml:"maxOpen"`
MaxIdle uint `yaml:"maxIdle"`
}
func (cc *ConnPoolConf) Configure(db *sqlx.DB) {
if cc == nil {
return
}
db.SetConnMaxLifetime(cc.MaxLifetime)
db.SetConnMaxIdleTime(cc.MaxIdleTime)
db.SetMaxIdleConns(int(cc.MaxIdle))
db.SetMaxOpenConns(int(cc.MaxOpen))
}
// ConnRetryConf holds common retry settings for establishing a database connection.
type ConnRetryConf struct {
// MaxAttempts is the maximum number of retries to attempt before giving up.
MaxAttempts uint64 `yaml:"maxAttempts"`
// InitialInterval is the initial wait period between retry attempts. Subsequent attempts will be longer depending on the attempt number.
InitialInterval time.Duration `yaml:"initialInterval"`
// MaxInterval is the maximum amount of time to wait between retry attempts.
MaxInterval time.Duration `yaml:"maxInterval"`
}
func (rc *ConnRetryConf) Validate() (outErr error) {
if rc == nil {
return nil
}
if rc.InitialInterval < 0 {
outErr = errors.Join(outErr, errors.New("retry.initialInterval must be a positive value"))
}
if rc.MaxInterval < 0 {
outErr = errors.Join(outErr, errors.New("retry.maxInterval must be a positive value"))
}
if rc.MaxInterval < rc.InitialInterval {
outErr = errors.Join(outErr, errors.New("retry.maxInterval must be larger than retry.initialInterval"))
}
return outErr
}
func (rc *ConnRetryConf) BackoffConf() backoff.BackOff {
if rc == nil {
return backoff.WithMaxRetries(backoff.NewExponentialBackOff(), defaultRetryMaxAttempts)
}
b := backoff.NewExponentialBackOff()
if rc.MaxInterval > 0 {
b.MaxInterval = rc.MaxInterval
}
if rc.InitialInterval > 0 {
b.InitialInterval = rc.InitialInterval
}
return backoff.WithMaxRetries(b, rc.MaxAttempts)
}