Skip to content

method.upper() recomputed multiple times per attempt inside the request retry loop #342

Description

@TexasCoding

Problem

SyncTransport.request and AsyncTransport.request call method.upper() repeatedly inside the retry loop — once for the auth-sign, once for the httpx call, once inside sign_request itself, plus inside every debug log statement, multiplied across retry attempts and across both transports. Same string allocated each time for lowercase input. Cost per call is microseconds, but #262 specifically hoisted invariant header merging out of the retry loop for the same reason; this is the same pattern still latent.

Evidence

  • kalshi/_base_client.py:286-303 — multiple .upper() calls per attempt in the sync loop
for attempt in range(self._config.max_retries + 1):
    auth_headers = self._auth.sign_request(method.upper(), sign_path) if self._auth else {}
    ...
    logger.debug(
        "Request: %s %s (attempt %d/%d)",
        method.upper(),
        ...
    )
    ...
    response = self._client.request(
        method=method.upper(),
        ...
    )
  • kalshi/_base_client.py:504-535 — identical shape in AsyncTransport.request.
  • kalshi/auth.py:276sign_request also does method.upper() internally
message = ts_str + method.upper() + clean_path

Suggested fix

Normalize once at the top of request(): method = method.upper(). Reuse the normalized variable for the auth sign, httpx call, all log statements, and the retry-budget logs. Drop the redundant method.upper() inside sign_request (transports are now the only callers and always pre-normalize). Mirror the change in AsyncTransport.request and KalshiAuth.sign_request_async. Signing payload is byte-identical for any caller that passed lowercase.

Source

Round-3 independent audit (reviewer: performance).

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance / hot-path concernpolishCode-quality and DX improvements; non-functional

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions