Skip to content

Scale: 6x faster prefilter, batched ban storms, decider backpressure - #7

Merged
FMXExpress merged 2 commits into
mainfrom
scale
Oct 31, 2026
Merged

FMXExpress merged 2 commits into
mainfrom
scale

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

I measured where jevban breaks at scale, then fixed the worst limits in order. For every CPU change, the gate was that each decision line stays identical to main, not just the totals. I checked this with a -v diff on the flood and typical load files, all the synthetic corpora, and LogarX. All identical.

Prefilter: profiled with callgrind

On 40k flood lines, work dropped from 38.0 to 3.4 billion instructions.

main now
Flood (every line a new address) ~6,000 lines/s ~35,000
Typical traffic ~28,000–39,000 ~200,000
500k distinct clients, max_clients=500000 9,300 lines/s, 814 MB 59,000 lines/s, 486 MB

What changed:

  • Signature cache: results are cached per distinct URL, request and user agent, as a bitset of matched rules. Tags are rebuilt in rule order, so logs and prompts are unchanged.
  • Near-known-bad search:
    • BK-tree distances are computed only up to MaxKid + allowance, so they are still exact wherever the search needs them.
    • Levenshtein uses stack rows instead of allocating.
    • Results are cached per path.
  • Hash map tables: clients, slow-scanner history and subnet heat use a hash map (Ban.Hash) instead of sorted string lists.
  • Parser: tokens are recorded as spans instead of a string list, decoding has fast paths, and line times are parsed once per second.
  • Less per-line work: the log summary is built only for lines that get logged, and the model line uses concatenation instead of Format.
  • Lazy client lists: per-client lists are created on first use.
  • Bounded caches: values over 512 characters aren't cached; the signature caches reset at 20k entries per field and the path cache at 50k.

Ban storms: tested with real ipset, iptables and nftables

Before, each ban ran /bin/sh plus the firewall tool on the thread that reads the log, then rewrote the whole state file.

2,000 instant bans, then one more scanner main now
Scanner banned 68–83 s 0.12 s
All 2,001 in the firewall same as above 0.12–0.17 s

How:

  • Firewall commands run on their own thread.
  • Bans queued meanwhile go out in one ipset restore, nft -f or iptables-restore. A failed batch is retried one address at a time, so every failure is still reported for its address.
  • ufw, firewalld, netsh and custom commands still run one per ban, but off the reading thread.
  • The state file is saved at most once a second.

Also checked:

  • 500 IPv4 and 500 IPv6 bans, then expiry: everything was added and removed with no errors.
  • A restart restoring 1,000 bans: one batch, ready in 0.1 s.
  • A forced batch failure: the fallback worked.

Decider backpressure

A local model on a CPU takes minutes per batch, and nothing limited the worker's queue. New decider_backlog setting, default 4: beyond it, new batches are decided by the rules at once. Only real model verdicts are cached, so those clients go back to the model with their next flagged request.

In a test with 300 password guessers at once and a mock decider taking 4 s per batch:

  • main: the last client was decided after 150 s. With Flash 9B on a CPU that would be about 1.5 hours.
  • Now: all 300 were decided in 22 s; the model judged 44 of them.

Not done

  • JSON logs parse at about 20k lines/s (fpjson builds a tree per line), and IIS logs at about 55k.

Unit tests: 153 checks, 0 failed. Three are new: a randomized 200k-operation check of the hash map against a sorted list, iteration, and clear. Iteration notes are in bench/results/iter7-scale/README.md.


Generated by Claude Code

Profiled with callgrind (40k flood lines: 38.0 -> 3.4 billion instructions):
- signature results cached per field value (bit set of matched rules, tags
  rebuilt in rule order); values over 512 chars uncached, caches capped
- near-known-bad: BK-tree distances bounded at MaxKid + allowance (exact
  wherever the search needs them), Levenshtein rows on the stack, results
  cached per path (capped)
- clients, slow-scanner history and subnet heat in a hash map (Ban.Hash,
  open addressing, backward-shift delete) instead of sorted string lists
- the Apache parser records token spans instead of building a string list;
  decode/unescape fast paths; the line time parsed once per second
- summaries by concatenation, the log summary only for logged lines

Every decision line is identical to main on the load files, all synthetic
corpora and LogarX.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NPVFJFY41846T15QqxmGW2
…nt lists

- Firewall commands run on their own thread (TActionWorker); bans queued
  meanwhile go in one ipset restore / nft -f / iptables-restore, a failed
  batch retried one by one. 2,000 instant bans: the next scanner was banned
  after 68-83 s, now 0.12 s, all 2,001 in the firewall within 0.17 s.
- The state file is saved at most once a second, not on every ban.
- decider_backlog (4): past it, new batches are decided by the rules at once
  and their clients go back to the model with their next flagged request.
- Per-client lists created on first use: 500,000 clients in 486 MB, was 814.
- Log writes locked (the action worker logs too); TStrMap randomized test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NPVFJFY41846T15QqxmGW2
@FMXExpress
FMXExpress merged commit 7fad043 into main Oct 31, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants