Skip to main content

Module singleflight

Module singleflight 

Source
Expand description

Singleflight request coalescing for query deduplication.

When multiple threads issue the SAME query (same sql_hash + same parameter bytes) simultaneously, only one actually executes against PostgreSQL. The others wait for the result and receive a shared copy via a condvar.

This is opt-in: enabled via Pool::builder().singleflight(true).

§Key design

Key = hash of (sql_hash, parameter bytes). We use rapidhash to combine the sql_hash with a hash of the parameter slice. If a request is already in-flight with the same key, the caller waits on its condvar instead of executing a new query.

§Limitations

  • Only coalesces query_raw and query_raw_readonly (not execute_raw). Writes must not be coalesced.
  • The result is Arc-shared, so callers receive the same data (no mutations).
  • Large result sets are shared by reference, reducing memory for hot reads.

Structs§

FlightLeader
Handle for the leader thread that will execute the query and notify followers.
FlightState
State shared between a leader and its followers via condvar.
OwnedResultSnapshot
A snapshot of query results that can be shared across threads.
Singleflight
Singleflight coalescing layer.

Enums§

FlightResult
Result of attempting to join a singleflight group.