Skip to content

perf(traffic): write the full-text index off the request path - #543

Open
Phantom-IN wants to merge 1 commit into
BaaRai:mainfrom
Phantom-IN:perf/traffic-async-fts-index
Open

Phantom-IN wants to merge 1 commit into
BaaRai:mainfrom
Phantom-IN:perf/traffic-async-fts-index

Conversation

@Phantom-IN

Copy link
Copy Markdown
Contributor

Fixes #542.

Recording no longer writes the full-text index inline. recordViews writes the exchange, bodies, blob refs and trace as before, then hands the index text to a single background worker (traffic/fts_queue.go). The proxy replies without waiting for the trigram indexing.

How it stays correct:

  • Searches stay consistent. ftsFilter waits until everything queued before the call is indexed, so a body search finds any exchange recorded before it. That covers traffic_search, ResponseContaining and the traffic page search, since they all go through ftsFilter.
  • Deletes are handled. The worker inserts with WHERE EXISTS (exchange with this rowid and id), so if DeleteHost removes an exchange before its entry is written, the entry is dropped and can't later match a new exchange that reuses the rowid.
  • Memory is bounded. At most 16 bodies wait in the queue; a recorder blocks when it's full. add is called after wmu is released, because the worker needs wmu.
  • Shutdown drains it. Close indexes what's left, then closes the db. Without FTS5, or on a zero-value Traffic, recording indexes inline like before.

BenchmarkRecordScript measures record time for a 1 MiB script with the indexer idle: 52.3 ms on main, 1.47 ms here. With a real 1.3 MB minified bundle, main took 163 ms.

Tests: a body search right after 20 back-to-back recordings (more than the queue holds) finds the last one, and a queued entry for a missing exchange isn't written. go test -race ./traffic/ passes, and ./server/ and ./agent/ pass apart from the two tests that fail on my Mac on main too.

The proxy records an exchange in its Response hook, before the client gets
the response, and recordViews wrote the trigram index of each body (up to
4 MiB) inside its transaction while holding wmu. That is almost all of the
recording cost: a 1 MB minified script takes ~163 ms with the index and
~1 ms without, and every other request being recorded waits on wmu behind it.

Queue the index write instead and let one background worker do it.
Searches that use the index wait for everything queued before them, so a
body search still finds an exchange recorded before it. The worker only
writes an entry whose exchange still exists with the same rowid and id, so
a delete in between can't leave an entry that matches a later exchange.
The queue holds at most 16 bodies; a recorder waits when it is full.

Fixes BaaRai#542.
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.

Capture proxy holds each response while the full-text index is written

1 participant