hypersync.session
Documentation for eth_defi.hypersync.session Python module.
Throttle-aware Hypersync client wrapper with stream tuning.
Provides a drop-in replacement for hypersync.HypersyncClient
that rate-limits every API call (stream, recv, get_chain_id,
get_height) through a shared SQLite-backed token bucket, and allows
centralised configuration of Hypersync StreamConfig tuning parameters
(concurrency, batch sizes, response byte limits).
This follows the same pyrate_limiter + SQLiteBucket throttling
pattern used for Hyperliquid, GRVT, Lighter and Derive sessions (see
e.g. eth_defi.hyperliquid.session), adapted for the async
Rust FFI client instead of requests.Session.
For stream tuning parameter documentation see Envio HyperSync StreamConfig tuning.
Usage:
from eth_defi.hypersync.session import create_throttled_hypersync_client
# Create a client with custom concurrency for dense workloads
client = create_throttled_hypersync_client(
hypersync.ClientConfig(url=url, bearer_token=api_key),
concurrency=20,
batch_size=5000,
)
# Use exactly like a regular HypersyncClient — throttling is transparent
# and StreamConfig is built automatically from stored tuning params
receiver = await client.stream(query)
res = await receiver.recv()
Functions
|
Create a Hypersync client with built-in SQLite-backed rate limiting. |
Read |
|
Read |
|
|
Check if obj is a Hypersync client (native or throttled). |
|
Open a Hypersync stream, dispatching correctly for both client types. |
Classes
Drop-in wrapper for |
- class ThrottledHypersyncClient
Bases:
objectDrop-in wrapper for
hypersync.HypersyncClientwith built-in rate limiting and stream tuning.Throttles one-shot API calls (
stream,get_chain_id,get_height) through a sharedpyrate_limiter.Limiterwith an SQLite-backed token bucket.Stream tuning parameters (concurrency, batch sizes, response byte limits) are stored on the client and used to build a
hypersync.StreamConfigautomatically whenstream()is called without an explicit config.recv()is intentionally not throttled — adding a Python-side delay onrecv()only slows down buffer reads without reducing actual API load. Internal Rust retries are disabled (max_num_retries=0) so 429 errors surface immediately to the Python-side retry logic.For stream tuning parameter documentation see Envio HyperSync StreamConfig tuning.
Note
This class intentionally does not subclass
hypersync.HypersyncClient(a Rust-backed PyO3 class). It relies on duck typing — callers that acceptHypersyncClientshould also accept this wrapper. Useis_hypersync_client()forisinstance-style checks that accept both types.- Parameters
client – The underlying native
hypersync.HypersyncClient.limiter – Shared
pyrate_limiter.Limiterfor rate limiting.concurrency – Number of requests in flight — the main throughput knob.
Noneuses the Hypersync server default (10).batch_size – Initial block range for the first wave of requests, before density has been measured.
Noneuses the server default (1000).min_batch_size – Lower limit on projected block count per request.
Noneuses the server default.max_batch_size – Hard cap on blocks per request.
Nonemeans no cap.response_bytes_ceiling – Upper target for response size in bytes (macOS wheel).
Noneuses the server default.response_bytes_floor – Lower target for response size in bytes (macOS wheel).
Noneuses the server default.response_bytes_target – Target response size in bytes (Linux wheel).
Noneuses the server default.
Note
The hypersync 1.1.0 Rust wheel exposes different response-byte parameter names on different platforms. Pass whichever your platform supports; unsupported names are silently ignored.
- __init__(client, limiter, *, concurrency=None, batch_size=None, min_batch_size=None, max_batch_size=None, response_bytes_ceiling=None, response_bytes_floor=None, response_bytes_target=None, max_buffered_bytes=None)
- Parameters
client (hypersync.HypersyncClient) –
limiter (pyrate_limiter.limiter.Limiter) –
- create_stream_config(**overrides)
Build a
hypersync.StreamConfigfrom stored tuning params.Any keyword arguments override the stored values for this single call. Only non-
Nonevalues are passed to the constructor.Example:
# Use all stored defaults config = client.create_stream_config() # Override concurrency for one call config = client.create_stream_config(concurrency=30)
- Return type
hypersync.StreamConfig
- async get_chain_id()
Get chain ID, throttled.
- async get_height()
Get latest block height, throttled.
- async stream(query, config=None)
Open a stream, throttled at the setup level.
When config is
None, ahypersync.StreamConfigis built automatically from the stored tuning parameters viacreate_stream_config(). Pass an explicit config to override completely.- Parameters
config (Optional[hypersync.StreamConfig]) –
- Return type
hypersync.QueryResponseStream
- create_throttled_hypersync_client(config, requests_per_minute=150, limiter=None, *, concurrency=None, batch_size=None, min_batch_size=None, max_batch_size=None, response_bytes_ceiling=None, response_bytes_floor=None, response_bytes_target=None, max_buffered_bytes=None)
Create a Hypersync client with built-in SQLite-backed rate limiting.
For stream tuning parameter documentation see Envio HyperSync StreamConfig tuning.
- Parameters
config (hypersync.ClientConfig) – Hypersync client configuration (URL, bearer token, etc.).
requests_per_minute (int) – Maximum API requests per minute. Defaults to 150 (75% of the Hypersync free-tier 200 RPM limit). Ignored when limiter is provided.
limiter (Optional[pyrate_limiter.limiter.Limiter]) – Optional pre-existing
pyrate_limiter.Limiterto share across multiple clients (e.g. lead discovery + price scanning on the same API key). WhenNone, a new limiter is created.concurrency (Optional[int]) – Number of requests in flight — the main throughput knob.
Noneuses the Hypersync server default (10).batch_size (Optional[int]) – Initial block range for the first wave of requests.
Noneuses the server default (1000).min_batch_size (Optional[int]) – Lower limit on projected block count per request.
max_batch_size (Optional[int]) – Hard cap on blocks per request.
response_bytes_ceiling (Optional[int]) – Upper target for response size in bytes (macOS wheel).
response_bytes_floor (Optional[int]) – Lower target for response size in bytes (macOS wheel).
response_bytes_target (Optional[int]) – Target response size in bytes (Linux wheel).
max_buffered_bytes (Optional[int]) – Cap on bytes of fetched-but-undelivered data (Linux wheel).
- Returns
A
ThrottledHypersyncClientwrapping a nativeHypersyncClient.- Return type
- get_hypersync_concurrency_from_env()
Read
HYPERSYNC_CONCURRENCYfrom the environment.Returns
Nonewhen the variable is unset or blank (meaning use the Hypersync server default of 10). RaisesValueErrorwhen the value is not a positive integer.
- get_hypersync_rpm_from_env()
Read
HYPERSYNC_RPMfrom the environment.Returns
DEFAULT_HYPERSYNC_REQUESTS_PER_MINUTEwhen the variable is unset or blank. RaisesValueErrorwith a clear message when a non-empty value cannot be parsed as an integer.- Return type
- is_hypersync_client(obj)
Check if obj is a Hypersync client (native or throttled).
Use instead of
isinstance(obj, hypersync.HypersyncClient)so thatThrottledHypersyncClientwrappers are also accepted.- Return type
- async open_hypersync_stream(client, query)
Open a Hypersync stream, dispatching correctly for both client types.
For
ThrottledHypersyncClient, callsstream(query)which builds ahypersync.StreamConfigfrom stored tuning parameters and logs them.For a native
hypersync.HypersyncClient, passes a bareStreamConfig()with all server defaults.Use this instead of calling
client.stream(query, config)directly so that tuning parameters are applied transparently.- Parameters
client (Union[hypersync.HypersyncClient, eth_defi.hypersync.session.ThrottledHypersyncClient]) – Either a native
HypersyncClientor aThrottledHypersyncClient.query (hypersync.Query) – The Hypersync query to stream.
- Return type
hypersync.QueryResponseStream