forked from siyuan-note/dejavu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_lock.go
146 lines (128 loc) · 3.79 KB
/
sync_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
// DejaVu - Data snapshot and sync.
// Copyright (c) 2022-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package dejavu
import (
"errors"
"path/filepath"
"time"
"github.com/88250/gulu"
"github.com/siyuan-note/dejavu/cloud"
"github.com/siyuan-note/eventbus"
"github.com/siyuan-note/logging"
)
var (
ErrLockCloudFailed = errors.New("lock cloud repo failed")
ErrCloudLocked = errors.New("cloud repo is locked")
)
func (repo *Repo) unlockCloud(context map[string]interface{}) {
endRefreshLock <- true
var err error
for i := 0; i < 3; i++ {
eventbus.Publish(eventbus.EvtCloudUnlock, context)
err = repo.cloud.RemoveObject("lock-sync")
if nil == err {
return
}
}
if errors.Is(err, cloud.ErrCloudAuthFailed) {
return
}
logging.LogErrorf("unlock cloud repo failed: %s", err)
return
}
var endRefreshLock = make(chan bool)
func (repo *Repo) tryLockCloud(context map[string]interface{}) (err error) {
for i := 0; i < 3; i++ {
eventbus.Publish(eventbus.EvtCloudLock, context)
err = repo.lockCloud()
if nil != err {
if errors.Is(err, ErrCloudLocked) {
logging.LogInfof("cloud repo is locked, retry after 5s")
time.Sleep(5 * time.Second)
continue
}
return
}
// 锁定成功,定时刷新所
go func() {
for {
select {
case <-endRefreshLock:
return
case <-time.After(30 * time.Second):
if refershErr := repo.lockCloud0(); nil != refershErr {
logging.LogErrorf("refresh cloud repo lock failed: %s", refershErr)
}
}
}
}()
return
}
return
}
func (repo *Repo) lockCloud() (err error) {
data, err := repo.cloud.DownloadObject("lock-sync")
if errors.Is(err, cloud.ErrCloudObjectNotFound) {
err = repo.lockCloud0()
return
}
content := map[string]interface{}{}
err = gulu.JSON.UnmarshalJSON(data, &content)
if nil != err {
logging.LogErrorf("unmarshal lock sync failed: %s", err)
return
}
deviceID := content["deviceID"].(string)
t := int64(content["time"].(float64))
now := time.Now()
lockTime := time.UnixMilli(t)
if now.After(lockTime.Add(65*time.Second)) || deviceID == repo.DeviceID {
// 云端锁超时过期或者就是当前设备锁的,那么当前设备可以继续直接锁
err = repo.lockCloud0()
return
}
logging.LogWarnf("cloud repo is locked by device [%s] at [%s], will retry after 30s", content["deviceID"].(string), lockTime.Format("2006-01-02 15:04:05"))
err = ErrCloudLocked
return
}
func (repo *Repo) lockCloud0() (err error) {
lockSync := filepath.Join(repo.Path, "lock-sync")
content := map[string]interface{}{
"deviceID": repo.DeviceID,
"time": time.Now().UnixMilli(),
}
data, err := gulu.JSON.MarshalJSON(content)
if nil != err {
logging.LogErrorf("marshal lock sync failed: %s", err)
err = ErrLockCloudFailed
return
}
err = gulu.File.WriteFileSafer(lockSync, data, 0644)
if nil != err {
logging.LogErrorf("write lock sync failed: %s", err)
err = ErrCloudLocked
return
}
err = repo.cloud.UploadObject("lock-sync", true)
if nil != err {
if errors.Is(err, cloud.ErrSystemTimeIncorrect) || errors.Is(err, cloud.ErrCloudAuthFailed) {
return
}
logging.LogErrorf("upload lock sync failed: %s", err)
err = ErrLockCloudFailed
}
return
}