Ìmprove handling of multiple rapid file changes

This commit is contained in:
2025-09-20 15:30:08 +02:00
parent 8c432bb834
commit f8f36b98db
6 changed files with 106 additions and 79 deletions

View File

@@ -13,6 +13,7 @@ import (
"fyne.io/systray"
"git.blackforestbytes.com/BlackForestBytes/goext/dataext"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"git.blackforestbytes.com/BlackForestBytes/goext/mathext"
"git.blackforestbytes.com/BlackForestBytes/goext/syncext"
"git.blackforestbytes.com/BlackForestBytes/goext/termext"
"git.blackforestbytes.com/BlackForestBytes/goext/timeext"
@@ -30,7 +31,8 @@ type Application struct {
config Config
trayReady *syncext.AtomicBool
uploadRunning *syncext.AtomicBool
uploadWaiting *syncext.AtomicBool
uploadActive *syncext.AtomicBool
syncLoopRunning *syncext.AtomicBool
keepassRunning *syncext.AtomicBool
@@ -48,6 +50,8 @@ type Application struct {
currSysTrayTooltip string
uploadDCI *dataext.DelayedCombiningInvoker
trayItemChecksum *systray.MenuItem
trayItemETag *systray.MenuItem
trayItemLastModified *systray.MenuItem
@@ -60,7 +64,8 @@ func NewApplication() *Application {
logLock: sync.Mutex{},
logList: make([]LogMessage, 0, 1024),
logBroadcaster: dataext.NewPubSub[string, LogMessage](128),
uploadRunning: syncext.NewAtomicBool(false),
uploadWaiting: syncext.NewAtomicBool(false),
uploadActive: syncext.NewAtomicBool(false),
trayReady: syncext.NewAtomicBool(false),
syncLoopRunning: syncext.NewAtomicBool(false),
keepassRunning: syncext.NewAtomicBool(false),
@@ -120,6 +125,12 @@ func (app *Application) Run() {
}
}
debounce := timeext.FromMilliseconds(app.config.Debounce)
app.uploadDCI = dataext.NewDelayedCombiningInvoker(app.runDBUpload, debounce, mathext.Max(45*time.Second, debounce*3))
app.uploadDCI.RegisterOnRequest(func(_ int, _ bool) { app.uploadWaiting.Set(app.uploadDCI.HasPendingRequests()) })
app.uploadDCI.RegisterOnExecutionDone(func() { app.uploadWaiting.Set(app.uploadDCI.HasPendingRequests()) })
go func() {
app.syncLoopRunning.Set(true)
defer app.syncLoopRunning.Set(false)
@@ -227,9 +238,14 @@ func (app *Application) stopBackgroundRoutines() {
app.trayReady.Wait(false)
app.LogDebug("Stopped systray.")
if app.uploadRunning.Get() {
if app.uploadWaiting.Get() {
app.LogInfo("Triggering pending upload immediately...")
app.uploadDCI.ExecuteNow()
}
if app.uploadActive.Get() {
app.LogInfo("Waiting for active upload...")
app.uploadRunning.Wait(false)
app.uploadActive.Wait(false)
app.LogInfo("Upload finished.")
}

View File

@@ -11,7 +11,6 @@ import (
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
"git.blackforestbytes.com/BlackForestBytes/goext/timeext"
"mikescher.com/kpsync/assets"
)
@@ -225,21 +224,16 @@ func (app *Application) runKeepass(fallback bool) {
}
func (app *Application) onDBFileChanged() {
app.masterLock.Lock()
app.uploadRunning.Wait(false)
app.uploadRunning.Set(true)
app.masterLock.Unlock()
func (app *Application) runDBUpload() {
app.uploadWaiting.Set(false)
defer app.uploadRunning.Set(false)
app.uploadActive.Set(true)
defer app.uploadActive.Set(false)
fin1 := app.setTrayState("Uploading database", assets.IconUpload)
defer fin1()
app.LogInfo("Database file was modified")
app.LogInfo(fmt.Sprintf("Sleeping for %d ms", app.config.Debounce))
time.Sleep(timeext.FromMilliseconds(app.config.Debounce))
app.LogInfo("Starting upload-check")
state := app.readState()
localCS, err := app.calcLocalChecksum()
@@ -373,12 +367,12 @@ func (app *Application) doDBUpload(state *State, stateClear func(), allowConflic
func (app *Application) runFinalSync() {
app.masterLock.Lock()
app.uploadRunning.Wait(false)
app.uploadRunning.Set(true)
app.uploadDCI.CancelPendingRequests()
app.uploadActive.Wait(false)
app.uploadActive.Set(true)
defer app.uploadActive.Set(false)
app.masterLock.Unlock()
defer app.uploadRunning.Set(false)
fin1 := app.setTrayState("Uploading database", assets.IconUpload)
defer fin1()
@@ -411,12 +405,12 @@ func (app *Application) runFinalSync() {
func (app *Application) runExplicitSync(force bool) {
app.masterLock.Lock()
app.uploadRunning.Wait(false)
app.uploadRunning.Set(true)
app.uploadDCI.CancelPendingRequests()
app.uploadActive.Wait(false)
app.uploadActive.Set(true)
defer app.uploadActive.Set(false)
app.masterLock.Unlock()
defer app.uploadRunning.Set(false)
state := app.readState()
if !force {

View File

@@ -6,6 +6,7 @@ import (
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
"github.com/fsnotify/fsnotify"
"mikescher.com/kpsync/assets"
)
func (app *Application) runSyncWatcher() error {
@@ -61,7 +62,10 @@ func (app *Application) runSyncWatcher() error {
continue
}
app.onDBFileChanged()
app.uploadWaiting.Set(true)
app.setTrayStateDirect("Uploading database (waiting)", assets.IconUpload)
app.LogInfo(fmt.Sprintf("Database file was modified - requesting upload (currently %d pending requests)", app.uploadDCI.CountPendingRequests()))
app.uploadDCI.Request()
case err := <-watcher.Errors:
app.LogError("Filewatcher reported an error", err)