Fix: Reuse shared OPA HTTP client - #31
Conversation
NickLarsenNZ
left a comment
There was a problem hiding this comment.
Approving this on the grounds that @Jimvin tested it.
He did ask:
What would be nice would be a way to configure the number of threads from the nificluster CR. At the moment the thread pool size is 32 by default, tuning this would be helpful.
Release Notesnifi-opa-plugin: shared HTTP client for OPA authorization requestsPreviously, the NiFi OPA authorizer could create a new HTTP client for each authorization request, spawning large numbers of selector and executor threads under NiFi load and eventually exhausting native threads. With this release, all OPA authorization requests share a single pooled HTTP client with a fixed-size daemon-thread executor, reusing one connection pool and selector infrastructure. The client is configurable via |
This change replaces the OPA SDK's per-request HTTP transport with a shared
java.net.http.HttpClientAddressing #30The majority of this PR is based on the code provided by:
#30 (comment) - Thanks!! :)
Background
The existing SDK transport can create a new
HttpClientfor each authorisation request. Under NiFi load, this may create large numbers of selector and executor threads, eventually causing native thread exhaustion and errors such as:The new
PooledHttpClientreuses one HTTP client, connection pool, selector infrastructure, and fixed-size executor across OPA authorisation requests.Changes
Added a shared
PooledHttpClientimplementation.Configured a fixed-size daemon-thread executor.
Added configurable HTTP settings (
OPA_HTTP_POOL_SIZE, default32OPA_HTTP_CONNECT_TIMEOUT_MS, default2000OPA_HTTP_REQUEST_TIMEOUT_MS, default5000Preserved the URI reconstruction behavior used by the upstream OPA HTTP client.
Added validation for invalid or non-positive configuration values /defensive
Removed shared
dumpCachestate so cache invalidation is scoped to the current OPA response.Improved exception handling and fail-closed behavior.
Marked variables and fields as final where reassignment is not expected.
Configuration
Existing deployments do not need to add configuration because defaults are provided
Example:
At startup, the authoriser logs the active HTTP client configuration: