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

+10 -11
View File
@@ -10,14 +10,17 @@ import (
"time"
"github.com/pelletier/go-toml/v2"
"git.security-command.org/Overlord/dibs/internal/ratelimit"
)
// MinInterval is the shortest allowed polling interval per URL.
const MinInterval = 60 * time.Second
// MinInterval is the shortest allowed polling interval per URL. The
// scheduler enforces it too, whatever the config says.
const MinInterval = ratelimit.URLFloor
const (
// Seller policies for an item: sold by the shop itself; sold or at least
// shipped by it (e.g. Fulfilled by Amazon); or anyone.
// The seller policies accept offers sold by the shop itself, offers it
// sells or only ships (such as Fulfilled by Amazon), or any offer.
SellerFirstParty = "first_party"
SellerFulfilled = "fulfilled"
SellerAny = "any"
@@ -30,7 +33,6 @@ const (
FormatJSON = "json"
)
// Defaults for keys that may be omitted.
const (
DefaultInterval = 3 * time.Minute
DefaultCooldown = 30 * time.Minute
@@ -48,7 +50,7 @@ type Config struct {
Items []Item `toml:"item"`
Accounts map[string]Account `toml:"accounts"`
Notify Notify `toml:"notify"`
// Plugins maps a plugin name to its file. After Load the paths are absolute.
// Plugins maps plugin names to files. Load makes the paths absolute.
Plugins map[string]string `toml:"plugins"`
Sandbox Sandbox `toml:"sandbox"`
@@ -89,7 +91,6 @@ type Webhook struct {
Format string `toml:"format"`
}
// Sandbox holds the limits for Lua plugin calls.
type Sandbox struct {
// Timeout applies to matches, parse and cart_link.
Timeout Duration `toml:"timeout"`
@@ -152,7 +153,6 @@ func (it Item) AcceptsSeller(seller string) bool {
return rank <= policyRanks[it.Seller]
}
// Options control validation that depends on the environment.
type Options struct {
// Builtins are the names of the embedded plugins.
Builtins []string
@@ -167,8 +167,7 @@ func DefaultPath() (string, error) {
return filepath.Join(dir, "dibs", "dibs.toml"), nil
}
// Load reads, decodes and validates the config at path. All problems found
// are returned together as Errors.
// Load returns every problem it finds at once, as Errors.
func Load(path string, opts Options) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil { return nil, err }
@@ -206,7 +205,7 @@ func Load(path string, opts Options) (*Config, error) {
switch {
case err == nil:
case errors.As(err, &strict):
// The rest of the document was decoded, so keep validating it.
// The rest of the document was decoded, so validation goes on.
for _, e := range strict.Errors {
msg := "unknown key"
if k := e.Key(); len(k) > 0 && k[len(k)-1] == "quantity" {
+2 -2
View File
@@ -107,8 +107,8 @@ func (ps positions) keyAt(line, col int) string {
for path, p := range ps {
if p.Line == line && p.Column <= col && p.Column >= bestCol {
// Prefer the longer path when an array element and its first key
// share a position.
// An array element and its first key can share a position; the
// longer path wins.
if p.Column > bestCol || len(path) > len(best) {
best, bestCol = path, p.Column
}
+3 -5
View File
@@ -16,7 +16,6 @@ import (
"github.com/pelletier/go-toml/v2"
)
// Ranges for the [sandbox] limits.
const (
minSandboxTimeout = 100 * time.Millisecond
maxSandboxTimeout = 30 * time.Second
@@ -47,7 +46,6 @@ var goTypeNames = map[string]string{
"config.Duration": `a duration string such as "3m"`,
}
// Error is one problem in the config file.
type Error struct {
File string
Line int // 1-based; 0 if unknown
@@ -71,7 +69,7 @@ func (e Error) Error() string {
return b.String()
}
// Errors is every problem found in one config file, sorted by position.
// Errors is sorted by position in the file.
type Errors []Error
func (es Errors) Error() string {
@@ -99,8 +97,8 @@ func (v *validator) decodeError(de *toml.DecodeError, msg string) {
msg = friendlyDecodeMsg(strings.TrimPrefix(de.Error(), "toml: "))
}
// The decoder's key has no array indexes, so prefer the indexed path of
// the key at that position.
// The decoder's key has no array indexes; the key indexed at the same
// position has them.
key := v.pos.keyAt(line, col)
if key == "" {
key = strings.Join(de.Key(), ".")