Skip to content

Per-request overhead in check(): trust_env=True executor round trips and Configuration copies that call Logger.setLevel #337

Open

Description

@SPIRITT-lidor

Versions: openfga-sdk 0.10.6 (also 0.10.5, and main at 0545cfe), async client, Python 3.12, aiohttp 3.14.5.

Each OpenFgaClient.check() call does two kinds of avoidable per-request work. In our micro-benchmark, a check() costs about 2.5 ms of client CPU. The same JSON POST through a plain aiohttp.ClientSession costs about 0.4 to 0.8 ms.

1. trust_env=True: two thread-pool round trips per request

RESTClientObject.__init__ creates aiohttp.ClientSession(connector=connector, trust_env=True) (rest.py#L160). The session cannot be configured or replaced through the public API.

With trust_env=True, aiohttp 3.14.5 does this on every request, not once per session (aiohttp/client.py, ClientSession._request):

  • L776-780: if auth is None, it reads ~/.netrc (or $NETRC) in the default executor, through run_in_executor(None, self._get_netrc_auth, url.host). The SDK sends credentials as headers, so auth is always None.
  • L813-817: if proxy is None, it resolves the proxy environment through asyncio.to_thread(get_env_proxy_for_url, url). The SDK passes proxy only when configuration.proxy is set.

In our benchmark, this accounts for about 1.4 ms CPU and 1 ms wall time per check().

2. A new Configuration per request model, which calls Logger.setLevel

check() builds CheckRequest(tuple_key=TupleKey(...)) without local_vars_configuration (client/client.py#L667). Each model __init__ then calls Configuration.get_default_copy() (models/check_request.py#L63-L64, and the same in tuple_key.py). That method builds a new Configuration() or deep-copies the default (configuration.py#L359-L370). Both paths run the debug setter (configuration.py#L413-L428). The setter calls setLevel(logging.WARNING) on the openfga_sdk and urllib3 loggers.

In CPython, Logger.setLevel calls logging.Manager._clear_cache(), which takes the module lock and clears the level cache of every logger in the process. That gives 4 setLevel calls per check() (2 models x 2 loggers), plus the cleared caches in all other loggers. The cost grows with the logger count. We measured 16 µs per setLevel with 300 loggers, 62 µs with 1,300 and 174 µs with 4,300. This is small in a small process. In our service's py-spy profile, logging._clear_cache was the hottest leaf in the check path.

Measurements

The client runs in one process, and a local aiohttp stub returns {"allowed": true} from a separate process. Each figure is client CPU (time.process_time) per call, over 3,000 calls and 300 extra loggers, as the range of 3 runs. The runs used Python 3.12.16 on 2 vCPUs in a gVisor sandbox. Absolute numbers on bare metal will be lower.

Variant CPU per call Wall per call
check(), stock 2.41-2.76 ms 2.2-2.9 ms
check(), no-op debug setter 2.19-2.43 ms 2.0-2.2 ms
check(), session replaced with trust_env=False 1.02-1.21 ms 1.3-1.5 ms
Plain aiohttp POST, trust_env=False 0.41-0.71 ms 0.6-1.0 ms
Plain aiohttp POST, trust_env=True 1.84-2.26 ms 1.6-2.2 ms

0.10.5 and 0.10.6 give the same results. In production, we ran py-spy during a preview load test (about 20 forward-auth requests/s). The SDK was 83% of the samples in our OpenFGA check path, and logging._clear_cache was the hottest leaf. The check path itself was about 3% of the process samples.

Suggestions

  1. Make trust_env configurable on ClientConfiguration, keeping the current default if you prefer. Alternatively, accept a caller-provided aiohttp.ClientSession or connector.
  2. Let the client pass its own Configuration as local_vars_configuration when it builds request models, as response deserialization already does. Alternatively, make the debug setter skip setLevel when the level does not change.

I searched open and closed issues and PRs in this repo and in openfga/sdk-generator, and found no existing report of this overhead. openfga/sdk-generator#549 asks for a caller-provided aiohttp session for connection reuse. That change would also let callers set trust_env=False. I'm happy to send a PR for either change, here or in sdk-generator if it belongs in the templates.

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions