-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
options.go
77 lines (67 loc) · 1.93 KB
/
options.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
package dbresolver
import (
"database/sql"
"fmt"
)
// 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
}
// 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.
// The default one just checks for the presence of the string "RETURNING" in the uppercase query.
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: &DefaultQueryTypeChecker{},
}
}