🎨 Improve kernel stability by eliminating some data races https://github.com/siyuan-note/siyuan/issues/9842

This commit is contained in:
Daniel 2023-12-08 13:05:50 +08:00
parent 8c5c62670e
commit bea32e96d5
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
17 changed files with 158 additions and 117 deletions

View file

@ -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) {