mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +01:00
🎨 Improve kernel stability by eliminating some data races https://github.com/siyuan-note/siyuan/issues/9842
This commit is contained in:
parent
8c5c62670e
commit
bea32e96d5
17 changed files with 158 additions and 117 deletions
|
|
@ -26,6 +26,7 @@ import (
|
|||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/88250/gulu"
|
||||
|
|
@ -55,8 +56,8 @@ func SyncDataDownload() {
|
|||
return
|
||||
}
|
||||
|
||||
syncLock.Lock()
|
||||
defer syncLock.Unlock()
|
||||
lockSync()
|
||||
defer unlockSync()
|
||||
|
||||
now := util.CurrentTimeMillis()
|
||||
Conf.Sync.Synced = now
|
||||
|
|
@ -82,8 +83,8 @@ func SyncDataUpload() {
|
|||
return
|
||||
}
|
||||
|
||||
syncLock.Lock()
|
||||
defer syncLock.Unlock()
|
||||
lockSync()
|
||||
defer unlockSync()
|
||||
|
||||
now := util.CurrentTimeMillis()
|
||||
Conf.Sync.Synced = now
|
||||
|
|
@ -98,9 +99,11 @@ func SyncDataUpload() {
|
|||
}
|
||||
|
||||
var (
|
||||
syncSameCount = 0
|
||||
syncSameCount = atomic.Int32{}
|
||||
autoSyncErrCount = 0
|
||||
fixSyncInterval = 5 * time.Minute
|
||||
|
||||
syncPlanTimeLock = sync.Mutex{}
|
||||
syncPlanTime = time.Now().Add(fixSyncInterval)
|
||||
|
||||
BootSyncSucc = -1 // -1:未执行,0:执行成功,1:执行失败
|
||||
|
|
@ -108,9 +111,12 @@ var (
|
|||
)
|
||||
|
||||
func SyncDataJob() {
|
||||
syncPlanTimeLock.Lock()
|
||||
if time.Now().Before(syncPlanTime) {
|
||||
syncPlanTimeLock.Unlock()
|
||||
return
|
||||
}
|
||||
syncPlanTimeLock.Unlock()
|
||||
|
||||
SyncData(false)
|
||||
}
|
||||
|
|
@ -132,8 +138,8 @@ func BootSyncData() {
|
|||
return
|
||||
}
|
||||
|
||||
syncLock.Lock()
|
||||
defer syncLock.Unlock()
|
||||
lockSync()
|
||||
defer unlockSync()
|
||||
|
||||
util.IncBootProgress(3, "Syncing data from the cloud...")
|
||||
BootSyncSucc = 0
|
||||
|
|
@ -155,6 +161,16 @@ func SyncData(byHand bool) {
|
|||
syncData(false, byHand)
|
||||
}
|
||||
|
||||
func lockSync() {
|
||||
syncLock.Lock()
|
||||
isSyncing = true
|
||||
}
|
||||
|
||||
func unlockSync() {
|
||||
isSyncing = false
|
||||
syncLock.Unlock()
|
||||
}
|
||||
|
||||
func syncData(exit, byHand bool) {
|
||||
defer logging.Recover()
|
||||
|
||||
|
|
@ -168,8 +184,8 @@ func syncData(exit, byHand bool) {
|
|||
return
|
||||
}
|
||||
|
||||
syncLock.Lock()
|
||||
defer syncLock.Unlock()
|
||||
lockSync()
|
||||
defer unlockSync()
|
||||
|
||||
if exit {
|
||||
ExitSyncSucc = 0
|
||||
|
|
@ -240,7 +256,7 @@ func checkSync(boot, exit, byHand bool) bool {
|
|||
}
|
||||
}
|
||||
|
||||
if gulu.IsMutexLocked(&syncLock) {
|
||||
if isSyncing {
|
||||
logging.LogWarnf("sync is in progress")
|
||||
planSyncAfter(fixSyncInterval)
|
||||
return false
|
||||
|
|
@ -266,7 +282,7 @@ func incReindex(upserts, removes []string) (upsertRootIDs, removeRootIDs []strin
|
|||
|
||||
luteEngine := util.NewLute()
|
||||
// 先执行 remove,否则移动文档时 upsert 会被忽略,导致未被索引
|
||||
bootProgressPart := 10 / float64(len(removes))
|
||||
bootProgressPart := int32(10 / float64(len(removes)))
|
||||
for _, removeFile := range removes {
|
||||
if !strings.HasSuffix(removeFile, ".sy") {
|
||||
continue
|
||||
|
|
@ -288,7 +304,7 @@ func incReindex(upserts, removes []string) (upsertRootIDs, removeRootIDs []strin
|
|||
msg = fmt.Sprintf(Conf.Language(35))
|
||||
util.PushStatusBar(msg)
|
||||
|
||||
bootProgressPart = 10 / float64(len(upserts))
|
||||
bootProgressPart = int32(10 / float64(len(upserts)))
|
||||
for _, upsertFile := range upserts {
|
||||
if !strings.HasSuffix(upsertFile, ".sy") {
|
||||
continue
|
||||
|
|
@ -413,7 +429,10 @@ func SetSyncProviderWebDAV(webdav *conf.WebDAV) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
var syncLock = sync.Mutex{}
|
||||
var (
|
||||
syncLock = sync.Mutex{}
|
||||
isSyncing bool
|
||||
)
|
||||
|
||||
func CreateCloudSyncDir(name string) (err error) {
|
||||
if conf.ProviderSiYuan != Conf.Sync.Provider {
|
||||
|
|
@ -587,12 +606,14 @@ func getIgnoreLines() (ret []string) {
|
|||
}
|
||||
|
||||
func IncSync() {
|
||||
syncSameCount = 0
|
||||
syncSameCount.Store(0)
|
||||
planSyncAfter(30 * time.Second)
|
||||
}
|
||||
|
||||
func planSyncAfter(d time.Duration) {
|
||||
syncPlanTimeLock.Lock()
|
||||
syncPlanTime = time.Now().Add(d)
|
||||
syncPlanTimeLock.Unlock()
|
||||
}
|
||||
|
||||
func isProviderOnline(byHand bool) (ret bool) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue