Add scheduler, rate limiting and headless mode.

Hard-coded 60s URL and 10s host floors with jitter and per-host
backoff, shared between dibs processes through a locked state file.
The scheduler shuffles due URLs each round and runs one worker per
host; 'dibs --headless' watches and logs JSON.
This commit is contained in:
Overlord committed 2026-10-14 09:04:15 +02:00
1 parent bb010356a0
commit 2175c0e5aa
30 files changed
+1434 -126

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
// Package fsutil has file helpers shared by the packages that persist data.
package fsutil
import (
"io/fs"
"os"
"path/filepath"
)
// WriteFileAtomic replaces path with data, so a crash never leaves a
// half-written file behind. Missing parent directories are created for the
// user only.
func WriteFileAtomic(path string, data []byte, perm fs.FileMode) error {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { return err }
tmp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*")
if err != nil { return err }
defer func() { _ = os.Remove(tmp.Name()) }()
if err := tmp.Chmod(perm); err != nil {
_ = tmp.Close()
return err
}
if _, err := tmp.Write(data); err != nil {
_ = tmp.Close()
return err
}
if err := tmp.Close(); err != nil { return err }
return os.Rename(tmp.Name(), path)
}
+26
View File
@@ -0,0 +1,26 @@
package fsutil
import (
"os"
"path/filepath"
)
// Lock takes an exclusive lock on path, creating it if needed, and blocks
// until it gets it. The lock is shared with other processes. The returned
// function releases it.
func Lock(path string) (func(), error) {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { return nil, err }
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil { return nil, err }
if err := lockFile(f); err != nil {
_ = f.Close()
return nil, err
}
return func() {
_ = unlockFile(f)
_ = f.Close()
}, nil
}
+11
View File
@@ -0,0 +1,11 @@
//go:build unix
package fsutil
import (
"os"
"syscall"
)
func lockFile(f *os.File) error { return syscall.Flock(int(f.Fd()), syscall.LOCK_EX) }
func unlockFile(f *os.File) error { return syscall.Flock(int(f.Fd()), syscall.LOCK_UN) }
+17
View File
@@ -0,0 +1,17 @@
//go:build windows
package fsutil
import (
"os"
"golang.org/x/sys/windows"
)
func lockFile(f *os.File) error {
return windows.LockFileEx(windows.Handle(f.Fd()), windows.LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, new(windows.Overlapped))
}
func unlockFile(f *os.File) error {
return windows.UnlockFileEx(windows.Handle(f.Fd()), 0, 1, 0, new(windows.Overlapped))
}