/
dbresolver.go
45 lines (38 loc) · 1.04 KB
/
dbresolver.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
package dbresolver
import (
"github.com/kyaxcorp/go-core/core/helpers/_context"
"github.com/kyaxcorp/go-core/core/helpers/sync/_bool"
"github.com/kyaxcorp/go-core/core/helpers/sync/_int"
"gorm.io/gorm"
"sync"
)
const (
Write Operation = "write"
Read Operation = "read"
)
func (dr *DBResolver) Name() string {
return "gorm:db_resolver"
}
// Initialize this is how gorm initializes our code...
func (dr *DBResolver) Initialize(db *gorm.DB) error {
dr.DB = db
// Set the context
dr.ctx = db.Statement.Context
dr.nrOfActiveMasters = _int.NewVal(0)
dr.nrOfInactiveMasters = _int.NewVal(0)
//dr.isMonitoringActive = _bool.NewVal(false)
dr.isMonitoringActive = _bool.NewValContext(false, dr.ctx)
dr.activeMastersLock = &sync.RWMutex{}
// Register callbacks
dr.registerCallbacks(db)
// Compile
return dr.compile()
}
func (dr *DBResolver) IsTerminating() bool {
// We simply create a temporary context to not be blocked by using directly
tmpContext := _context.WithCancel(dr.ctx)
if tmpContext.IsDone() {
return true
}
return false
}