sync: a page is stored in one transaction with parallel verification; a sync is cut off by idleness, not a fixed 2 minutes

20,000 listings to a newcomer: 120 s (unfinished) -> 24 s.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-19 09:46:27 +01:00
co-authored by Claude Sonnet 5.5
parent fc3a1efba4
commit 7da38a9e7e
8 changed files with 352 additions and 29 deletions
+58
View File
@@ -0,0 +1,58 @@
package p2p
import (
"context"
"sync"
"time"
)
// A sync used to be cut off after a fixed two minutes, however well it was
// going: a newcomer catching up from a large region stopped half way and
// waited for the next anti-entropy round. Now it is cut off when it makes no
// progress for syncIdle, and in any case after syncMax.
const (
syncIdle = 2 * time.Minute
syncMax = 30 * time.Minute
)
type idleKey struct{}
// idleTimer ends a context after a period without progress.
type idleTimer struct {
mu sync.Mutex
timer *time.Timer
idle time.Duration
closed bool
}
func (t *idleTimer) touch() {
t.mu.Lock()
defer t.mu.Unlock()
if !t.closed {
t.timer.Reset(t.idle)
}
}
// withIdleTimeout returns a context that ends after idle without a call to
// progress(ctx), or after max at the latest.
func withIdleTimeout(parent context.Context, idle, max time.Duration) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(parent, max)
t := &idleTimer{idle: idle}
t.timer = time.AfterFunc(idle, cancel)
ctx = context.WithValue(ctx, idleKey{}, t)
return ctx, func() {
t.mu.Lock()
t.closed = true
t.timer.Stop()
t.mu.Unlock()
cancel()
}
}
// progress tells ctx's idle timer that work is going on; for a context without
// one it does nothing.
func progress(ctx context.Context) {
if t, ok := ctx.Value(idleKey{}).(*idleTimer); ok {
t.touch()
}
}