Skip to content

feat(java-client): allow caller-supplied OkHttpClient injection - #326

Open
lygaret wants to merge 1 commit into
redis:mainfrom
WindfallData:feat/inject-ok-http
Open

feat(java-client): allow caller-supplied OkHttpClient injection#326
lygaret wants to merge 1 commit into
redis:mainfrom
WindfallData:feat/inject-ok-http

Conversation

@lygaret

@lygaret lygaret commented Jun 29, 2026

Copy link
Copy Markdown

adds a backwards-compatible Builder.httpClient(OkHttpClient) method: when set, the OkHttp client used downstream is derived from the caller's; otherwise, the existing default client is built.

when a client is provided:

  • timeout setting is ignored
  • don't close on '#close()', the library doesn't own it

in both cases

  • the User-Agent/X-Client-Version interceptor is added

additionally, expose okhttp as api (not implementation) so consumers can build their own OkHttpClient against the same okhttp version the client uses.

tests:

  • caller supplied interceptor reaches the server
  • user-agent/x-client-version remain with custom client
  • user-agent/x-client-version remain with default client
  • close() leaves the caller supplied client usable

Note

Low Risk
Backward-compatible builder extension and dependency visibility change; lifecycle fix reduces risk of breaking shared OkHttp pools on close.

Overview
Adds optional Builder.httpClient(OkHttpClient) so integrators can reuse a shared OkHttp instance (custom interceptors, connection pooling, TLS). When set, the client is built via newBuilder() on the supplied instance, timeout() is not applied, and close() no longer shuts down the dispatcher or evicts the pool—the library only tears down HTTP resources for its default-built client.

OkHttp is published as an api dependency so consumers compile against the same OkHttp version as the client. The library still adds its User-Agent / X-Client-Version interceptor in both paths.

Tests cover custom interceptors reaching the wire, default and custom client headers, and that close() leaves a caller-owned client usable.

Reviewed by Cursor Bugbot for commit c137d7e. Bugbot is set up for automated code reviews on this repo. Configure here.

adds a backwards-compatible `Builder.httpClient(OkHttpClient)` method:
when set, the `OkHttp` client used downstream is derived from the caller's;
otherwise, the existing default client is built.

when a client is provided:

- `timeout` setting is ignored
- don't close on '#close()', the library doesn't own it

in both cases
- the `User-Agent`/`X-Client-Version` interceptor is added

additionally, expose okhttp as `api` (not `implementation`) so consumers can
build their own OkHttpClient against the same okhttp version the client uses.

tests:
- caller supplied interceptor reaches the server
- user-agent/x-client-version remain with custom client
- user-agent/x-client-version remain with default client
- close() leaves the caller supplied client usable
@abrookins

Copy link
Copy Markdown
Collaborator

Awesome. Looks good to me, @lygaret! I'll run CI and merge if everything passes.

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