Skip to content

Cache Basic auth header on Realm#2267

Merged
hyperxpro merged 2 commits into
AsyncHttpClient:mainfrom
maygemdev:perf/basic-auth-header-cache
Jul 24, 2026
Merged

Cache Basic auth header on Realm#2267
hyperxpro merged 2 commits into
AsyncHttpClient:mainfrom
maygemdev:perf/basic-auth-header-cache

Conversation

@pavel-ptashyts

@pavel-ptashyts pavel-ptashyts commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Summary

  • memoize the generated HTTP Basic authorization header on immutable Realm instances
  • reuse one cached header for both request and proxy Basic authentication
  • document Realm.getBasicAuthHeader() as an intentional public API introduced in 3.0.12
  • document that concurrent first access may perform benign duplicate computation
  • verify request and proxy paths return the same cached String instance

API note

The accessor is public because Realm and the Netty authentication utilities are in different packages. It exposes only the immutable derived header value and avoids adding mutable cache management to the public surface.

Validation

  • ./mvnw -pl client -Dtest='org.asynchttpclient.util.AuthenticatorUtilsTest,org.asynchttpclient.RealmTest' test (44 tests)
  • existing focused authentication, compile, and package checks from the initial change

Attribution

Codex on behalf of Pavel Ptashyts

@pavel-ptashyts
pavel-ptashyts force-pushed the perf/basic-auth-header-cache branch from 9466f51 to b071fff Compare July 20, 2026 08:50
Cache the generated HTTP Basic authorization header on immutable
Realm instances so repeated preemptive and proxy Basic auth requests
can reuse the same header string. This avoids rebuilding credentials
and Base64 output on every dispatch.

Add a regression test that verifies repeated per-request Basic auth
header generation returns the cached value.

Codex on behalf of Pavel Ptashyts

Co-Authored-By: Codex <codex@openai.com>
@pavel-ptashyts
pavel-ptashyts force-pushed the perf/basic-auth-header-cache branch from 9ea7c4f to 06d225a Compare July 20, 2026 15:08
Document the lazy cache semantics of Realm.getBasicAuthHeader(),
including its intentional public API status and benign duplicate
computation during concurrent first access.

Extend the regression test to verify that request and proxy
authentication reuse the same cached header instance.

Codex on behalf of Pavel Ptashyts

Co-Authored-By: Codex <codex@openai.com>
@pavel-ptashyts
pavel-ptashyts force-pushed the perf/basic-auth-header-cache branch from 06d225a to f167215 Compare July 20, 2026 15:23
@hyperxpro
hyperxpro merged commit 9f4926c into AsyncHttpClient:main Jul 24, 2026
13 checks passed
hyperxpro added a commit that referenced this pull request Jul 24, 2026
Motivation:

#2267 introduced `Realm.getBasicAuthHeader()` to cache the computed HTTP
Basic authorization header on immutable `Realm` instances. Two
follow-ups remained: its Javadoc did not clarify that it always
generates a Basic header regardless of the configured `AuthScheme`, and
there was no test verifying that the cached header is scoped to each
`Realm` instance rather than shared across instances.

Modification:

Update the `Realm.getBasicAuthHeader()` Javadoc to explicitly document
that it always encodes credentials as a Basic authorization header,
regardless of the configured scheme. Add a test verifying that two
distinct `Realm` instances with identical credentials produce equal but
distinct cached header instances.

Result:

The behavior of `Realm.getBasicAuthHeader()` is now explicitly
documented, and its per-instance caching is protected by a regression
test against accidental cache sharing.
@pavel-ptashyts
pavel-ptashyts deleted the perf/basic-auth-header-cache branch July 25, 2026 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants