-
Notifications
You must be signed in to change notification settings - Fork 2
/
database_lock.go
155 lines (128 loc) · 3.31 KB
/
database_lock.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Package cachelock
//
// Laravel yyds!
package cachelock
import (
"context"
"time"
"gorm.io/gorm"
"github.com/duc-cnzj/mars/v4/internal/contracts"
"github.com/duc-cnzj/mars/v4/internal/mlog"
"github.com/duc-cnzj/mars/v4/internal/models"
"github.com/duc-cnzj/mars/v4/internal/utils"
"github.com/duc-cnzj/mars/v4/internal/utils/rand"
"github.com/duc-cnzj/mars/v4/internal/utils/recovery"
)
type timer interface {
Unix() int64
}
type realTimers struct{}
func (r *realTimers) Unix() int64 {
return time.Now().Unix()
}
type databaseLock struct {
lottery [2]int
timer timer
owner string
dbFunc func() *gorm.DB
}
func NewDatabaseLock(lottery [2]int, dbFunc func() *gorm.DB) contracts.Locker {
return &databaseLock{lottery: lottery, dbFunc: dbFunc, owner: utils.RandomString(40), timer: &realTimers{}}
}
func (d *databaseLock) RenewalAcquire(key string, seconds int64, renewalSeconds int64) (func(), bool) {
if d.Acquire(key, seconds) {
ctx, cancelFunc := context.WithCancel(context.TODO())
go func() {
defer recovery.HandlePanic("[lock]: key: " + key)
ticker := time.NewTicker(time.Second * time.Duration(renewalSeconds))
defer ticker.Stop()
for {
select {
case <-ctx.Done():
mlog.Debug("[lock]: canceled: " + key)
return
case <-ticker.C:
if !d.renewalExistKey(key, seconds) {
mlog.Warning("[lock]: err renewal lock: " + key)
return
}
}
}
}()
return func() {
cancelFunc()
d.Release(key)
}, true
}
return nil, false
}
func (d *databaseLock) ID() string {
return d.owner
}
func (d *databaseLock) Type() string {
return "database"
}
func (d *databaseLock) Acquire(key string, seconds int64) bool {
var (
acquired bool
unix = d.timer.Unix()
expiration = unix + seconds
)
db := d.dbFunc().Create(&models.CacheLock{
Key: key,
Owner: d.owner,
Expiration: expiration,
})
if db.Error == nil {
acquired = true
}
if !acquired {
updates := d.dbFunc().Model(&models.CacheLock{}).Where("`key` = ? AND `expiration` <= ?", key, unix).Updates(map[string]any{
"owner": d.owner,
"expiration": expiration,
})
if updates.RowsAffected >= 1 {
acquired = true
}
}
if rand.Intn(d.lottery[1]) < d.lottery[0] {
d.dbFunc().Where("`expiration` < ?", unix-60).Delete(&models.CacheLock{})
}
return acquired
}
func (d *databaseLock) renewalExistKey(key string, seconds int64) bool {
var (
acquired bool
unix = d.timer.Unix()
expiration = unix + seconds
)
cl := &models.CacheLock{}
first := d.dbFunc().Where("`key` = ?", key).First(cl)
if first.Error != nil {
return acquired
}
updates := d.dbFunc().Model(&models.CacheLock{}).Where("`key` = ? AND `owner` = ?", key, d.owner).Updates(map[string]any{
"owner": d.owner,
"expiration": expiration,
})
if updates.RowsAffected >= 1 {
acquired = true
}
return acquired
}
func (d *databaseLock) Release(key string) bool {
if d.Owner(key) == d.owner {
d.dbFunc().Where("`key` = ? AND `owner` = ?", key, d.owner).Delete(&models.CacheLock{})
return true
}
return false
}
func (d *databaseLock) ForceRelease(key string) bool {
d.dbFunc().Where("`key` = ?", key).Delete(&models.CacheLock{})
return true
}
func (d *databaseLock) Owner(key string) string {
cl := &models.CacheLock{}
d.dbFunc().Where("`key` = ?", key).First(cl)
return cl.Owner
}