Skip to content

Enhancement: Configurable Timeouts (global + per-call, sync & async) #105

Open

Description

@dougborg

Summary

Add first-class timeout configuration to generated clients for httpx / requests / aiohttp (global default + per-call override) without locking users into a specific strategy.

Motivation

Currently no explicit timeout control is surfaced; relying on library defaults risks indefinite hangs or inconsistent behavior. Making timeouts explicit improves reliability and aligns with common production requirements.

Goals

  • Global timeout settings via APIConfig (e.g. timeout or granular connect/read/write/pool for libraries that support it).
  • Per-call override parameter (e.g. every generated function accepts timeout: Optional[<Type>] = None).
  • Async and sync parity.
  • Zero breaking change for existing code (defaults preserve prior behavior if unspecified, optionally warn if unset).

Library Specifics

  • httpx: support float OR httpx.Timeout object (map simple value to Timeout(timeout)).
  • requests: single float (seconds) passed to request().
  • aiohttp: pass timeout=aiohttp.ClientTimeout(total=...) to ClientSession if provided.

Proposed Implementation

  1. Extend templates to include optional timeout kw-only parameter at the end (guarded so it doesn’t clash with existing params).
  2. In APIConfig add default_timeout: Optional[float|TimeoutConfig].
  3. Helper in generated code resolves effective timeout: effective = timeout if timeout is not None else api_config.default_timeout.
  4. For httpx if effective is numeric -> httpx.Timeout(effective); if already Timeout use as-is.
  5. For aiohttp if numeric -> aiohttp.ClientTimeout(total=effective); if ClientTimeout use as-is.
  6. For requests pass numeric directly.
  7. Document interplay with future retry logic (Enhancement: Built-in Retry Strategy for Generated Clients #102) (timeouts apply per attempt).

Testing

  • Unit tests create operation call with small timeout and ensure underlying library receives it (monkeypatch request / client args capture).
  • Ensure per-call overrides global.
  • Ensure absence of timeout keeps prior behavior (no regression tests fail).

Open Questions

  1. Provide structured multi-phase timeout config (connect/read) out of the gate or start simple numeric? (Recommendation: start simple; can add advanced later.)
  2. Warn when user sets extremely low timeout (<0.01s)?

Next Steps

  • Await maintainer feedback.
  • Implement minimal numeric support first; follow-up issue for advanced granular config if desired.

Related: #102 (retry strategy) – timeouts apply per attempt. Potential future synergy with rate limit handling (#103).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions