-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
131 lines (116 loc) · 3.36 KB
/
Copy pathoptions.go
File metadata and controls
131 lines (116 loc) · 3.36 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
package dbresolver
import (
"database/sql"
"fmt"
"time"
)
// LoadBalancerPolicy define the loadbalancer policy data type
type LoadBalancerPolicy string
// Supported Loadbalancer policy
const (
RoundRobinLB LoadBalancerPolicy = "ROUND_ROBIN"
RandomLB LoadBalancerPolicy = "RANDOM"
)
// Option define the option property
type Option struct {
PrimaryDBs []*sql.DB
ReplicaDBs []*sql.DB
StmtLB StmtLoadBalancer
DBLB DBLoadBalancer
QueryTypeChecker QueryTypeChecker
QueryRouter QueryRouter
CCConfig *CausalConsistencyConfig
}
// OptionFunc used for option chaining
type OptionFunc func(opt *Option)
// WithPrimaryDBs add primaryDBs to the resolver
func WithPrimaryDBs(primaryDBs ...*sql.DB) OptionFunc {
return func(opt *Option) {
opt.PrimaryDBs = primaryDBs
}
}
// WithReplicaDBs add replica DBs to the resolver
func WithReplicaDBs(replicaDBs ...*sql.DB) OptionFunc {
return func(opt *Option) {
opt.ReplicaDBs = replicaDBs
}
}
// WithQueryTypeChecker sets the query type checker instance.
func WithQueryTypeChecker(checker QueryTypeChecker) OptionFunc {
return func(opt *Option) {
opt.QueryTypeChecker = checker
}
}
// WithLoadBalancer configure the loadbalancer for the resolver
func WithLoadBalancer(lb LoadBalancerPolicy) OptionFunc {
return func(opt *Option) {
switch lb {
case RoundRobinLB:
opt.DBLB = &RoundRobinLoadBalancer[*sql.DB]{}
opt.StmtLB = &RoundRobinLoadBalancer[*sql.Stmt]{}
case RandomLB:
opt.DBLB = &RandomLoadBalancer[*sql.DB]{
randInt: make(chan int, 1),
}
opt.StmtLB = &RandomLoadBalancer[*sql.Stmt]{
randInt: make(chan int, 1),
}
default:
panic(fmt.Sprintf("LoadBalancer: %s is not supported", lb))
}
}
}
func defaultOption() *Option {
return &Option{
DBLB: &RoundRobinLoadBalancer[*sql.DB]{},
StmtLB: &RoundRobinLoadBalancer[*sql.Stmt]{},
QueryTypeChecker: NewDefaultQueryTypeChecker(),
CCConfig: DefaultCausalConsistencyConfig(),
}
}
// WithCausalConsistency enables and configures LSN-based causal consistency
func WithCausalConsistency(router QueryRouter) OptionFunc {
return func(opt *Option) {
if router != nil {
opt.QueryRouter = router
}
}
}
// WithCausalConsistencyLevel sets a specific causal consistency level
func WithCausalConsistencyLevel(level CausalConsistencyLevel) OptionFunc {
return func(opt *Option) {
if opt.CCConfig == nil {
opt.CCConfig = DefaultCausalConsistencyConfig()
}
opt.CCConfig.Level = level
opt.CCConfig.Enabled = true
}
}
// WithLSNQueryTimeout sets the timeout for LSN queries
func WithLSNQueryTimeout(timeout time.Duration) OptionFunc {
return func(opt *Option) {
if opt.CCConfig == nil {
opt.CCConfig = DefaultCausalConsistencyConfig()
}
opt.CCConfig.Timeout = timeout
opt.CCConfig.Enabled = true
}
}
// WithMasterFallback configures whether to fallback to master when LSN requirements can't be met
func WithMasterFallback(fallback bool) OptionFunc {
return func(opt *Option) {
if opt.CCConfig == nil {
opt.CCConfig = DefaultCausalConsistencyConfig()
}
opt.CCConfig.FallbackToMaster = fallback
opt.CCConfig.Enabled = true
}
}
// WithCausalConsistencyConfig sets the complete causal consistency configuration
func WithCausalConsistencyConfig(config *CausalConsistencyConfig) OptionFunc {
return func(opt *Option) {
if config != nil {
opt.CCConfig = config
}
}
}