forked from go-gorm/dbresolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
63 lines (56 loc) · 1.27 KB
/
resolver.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
package dbresolver
import (
"gorm.io/gorm"
)
type resolver struct {
sources []gorm.ConnPool
replicas []gorm.ConnPool
policy Policy
dbResolver *DBResolver
traceResolverMode bool
}
func (r *resolver) resolve(stmt *gorm.Statement, op Operation) (connPool gorm.ConnPool) {
if op == Read {
if len(r.replicas) == 1 {
connPool = r.replicas[0]
} else {
connPool = r.policy.Resolve(r.replicas)
}
if r.traceResolverMode {
markStmtResolverMode(stmt, ResolverModeReplica)
}
} else if len(r.sources) == 1 {
connPool = r.sources[0]
if r.traceResolverMode {
markStmtResolverMode(stmt, ResolverModeSource)
}
} else {
connPool = r.policy.Resolve(r.sources)
if r.traceResolverMode {
markStmtResolverMode(stmt, ResolverModeSource)
}
}
if stmt.DB.PrepareStmt {
if preparedStmt, ok := r.dbResolver.prepareStmtStore[connPool]; ok {
return &gorm.PreparedStmtDB{
ConnPool: connPool,
Mux: preparedStmt.Mux,
Stmts: preparedStmt.Stmts,
}
}
}
return
}
func (r *resolver) call(fc func(connPool gorm.ConnPool) error) error {
for _, s := range r.sources {
if err := fc(s); err != nil {
return err
}
}
for _, re := range r.replicas {
if err := fc(re); err != nil {
return err
}
}
return nil
}