lib/db: Clean up Timer and wait for logging before return in GC (#7720)

This commit is contained in:
greatroar 2021-05-31 09:50:21 +02:00 committed by GitHub
parent fcb19518c7
commit 95c9561e97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 13 deletions

View File

@ -693,20 +693,19 @@ func (db *Lowlevel) gcIndirect(ctx context.Context) (err error) {
var discardedBlocks, matchedBlocks, discardedVersions, matchedVersions int var discardedBlocks, matchedBlocks, discardedVersions, matchedVersions int
internalCtx, cancel := context.WithCancel(ctx) // Only print something if the process takes more than "a moment".
defer cancel() logWait := make(chan struct{})
go func() { logTimer := time.AfterFunc(10*time.Second, func() {
// Only print something if the process takes more than "a moment". l.Infoln("Database GC started - many Syncthing operations will be unresponsive until it's finished")
select { close(logWait)
case <-internalCtx.Done(): })
case <-time.After(10 * time.Second): defer func() {
l.Infoln("Database GC started - many Syncthing operations will be unresponsive until it's finished") if logTimer.Stop() || err != nil {
<-internalCtx.Done() return
if err != nil || ctx.Err() != nil {
return
}
l.Infof("Database GC done (discarded/remaining: %v/%v blocks, %v/%v versions)", discardedBlocks, matchedBlocks, discardedVersions, matchedVersions)
} }
<-logWait // Make sure messages are sent in order.
l.Infof("Database GC done (discarded/remaining: %v/%v blocks, %v/%v versions)",
discardedBlocks, matchedBlocks, discardedVersions, matchedVersions)
}() }()
t, err := db.newReadWriteTransaction() t, err := db.newReadWriteTransaction()