Skip to content

refactor(api): move blocking HTTP calls from RestTemplate to RestClient - #4058

Open
Macbet wants to merge 2 commits into
refactor/spring-core-retryfrom
refactor/api-restclient
Open

Macbet wants to merge 2 commits into
refactor/spring-core-retryfrom
refactor/api-restclient

Conversation

@Macbet

@Macbet Macbet commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

What changed and why?

Part 4 of #4052. Stacked on the retry pull request.

The API built RestTemplate in several ways, in some places once per request and without timeouts. Every blocking outbound call now uses a RestClient built once per service through RestClientFactory, which replaces RestTemplateFactory. WebClient code is untouched. Apache HttpStatus constants are replaced with Spring's.

RestClientFactory keeps what RestTemplate did on the wire and on failure: the URI encoding mode, the Accept header, Content-Length: 0 on a request without a body, and a ResourceAccessException when the status line or first body byte cannot be read. Redirect policy per client is unchanged, and the webhook client keeps its pool and SameOriginRedirectStrategy.

Behaviour changes:

  • GitLab hook create / update, GitHub token and discovery, GitLab, Bitbucket and Azure DevOps discovery, and the TF Cloud importer now have a 10s connect and 30s read timeout. They had none.
  • Each discovery service shares one connection pool (20 per route, 50 total, 5s wait for a connection) instead of a new pool per request.
  • HTTP error messages no longer contain the request URL.
  • The GitHub proxy setting is read once at startup. A missing or invalid http.proxyPort falls back to 80 instead of failing the call.

Known and unchanged: on a network error the destination URL still ends up in last_error, as it did with RestTemplate. For Slack that URL contains the secret path.

Testing

Ran: mvn -pl api clean test -> 3623 tests, 0 failures.
Checked: wire-level tests against a raw socket server for bodyless DELETE/POST, read timeouts and redirects on each client; a parity test compares the Accept header with what a real RestTemplate sends; HMAC is asserted over the exact bytes sent.

@Macbet
Macbet added this pull request to stack #4056 November 29, 2026 00:11
@github-actions github-actions Bot added terrakube-api area/scheduler Touches job scheduling, queueing or locking area/policy Touches OPA policy governance area/notifications Touches notifications and webhooks author/contributor Authored by a returning contributor size/XL 1000 or more lines changed, consider splitting type/refactor Refactor, performance or style change labels Nov 29, 2026
@Macbet
Macbet marked this pull request as ready for review November 29, 2026 00:30
@Macbet
Macbet force-pushed the refactor/api-restclient branch from 445ffd8 to 8e67a7d Compare November 29, 2026 00:45
@Macbet
Macbet force-pushed the refactor/api-restclient branch from 8e67a7d to e181e38 Compare November 29, 2026 01:17
@Macbet
Macbet force-pushed the refactor/api-restclient branch 2 times, most recently from 01cc45e to 54ab407 Compare November 29, 2026 10:00
@Macbet
Macbet force-pushed the refactor/api-restclient branch from 54ab407 to 84b7fef Compare November 29, 2026 10:02
@Macbet
Macbet force-pushed the refactor/api-restclient branch from 84b7fef to 2c93512 Compare November 29, 2026 10:05
@Macbet Macbet closed this Nov 29, 2026
@Macbet Macbet reopened this Nov 29, 2026
@Macbet
Macbet force-pushed the refactor/api-restclient branch from 2d58d58 to e6ecdc3 Compare November 29, 2026 10:32
@Macbet
Macbet force-pushed the refactor/api-restclient branch from e6ecdc3 to f0d1be9 Compare November 29, 2026 12:58
@alfespa17 alfespa17 self-assigned this Nov 29, 2026
@Macbet
Macbet force-pushed the refactor/api-restclient branch from f0d1be9 to f7aef18 Compare November 29, 2026 19:29
The API built RestTemplate in several ways, in some places once per
request and without timeouts. Every blocking outbound call now uses a
RestClient built once per service through RestClientFactory, which
replaces RestTemplateFactory. WebClient code is untouched.

RestClientFactory keeps what RestTemplate did on the wire and on
failure: the URI encoding mode, the Accept header, Content-Length: 0 on
a request without a body, and a ResourceAccessException when the status
line or first body byte cannot be read. Redirect policy per client is
unchanged, and the webhook client keeps its pool and
SameOriginRedirectStrategy.

Behaviour changes:
- GitLab hook create/update, GitHub token and discovery, GitLab,
  Bitbucket and Azure DevOps discovery, and the TF Cloud importer now
  have a 10s connect and 30s read timeout; they had none.
- Each discovery service shares one connection pool (20 per route, 50
  total, 5s wait for a connection) instead of a new pool per request.
- HTTP error messages no longer contain the request URL.
- The GitHub proxy setting is read once at startup; a missing or invalid
  http.proxyPort falls back to 80 instead of failing the call.

Apache HttpStatus constants are replaced with Spring's.

Refs #4052
Commented-out-code false positive (java:S125), unnamed catch variables (java:S7467), an empty
switch arm (java:S108), concatenated HTTP heads (java:S6126), multi-call assertThatThrownBy
lambdas (java:S5778) and locals hiding the server field (java:S1117).
@Macbet
Macbet force-pushed the refactor/api-restclient branch from f7aef18 to ebd4f1f Compare November 29, 2026 19:34
@sonarqubecloud

Copy link
Copy Markdown

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

Labels

area/notifications Touches notifications and webhooks area/policy Touches OPA policy governance area/scheduler Touches job scheduling, queueing or locking author/contributor Authored by a returning contributor size/XL 1000 or more lines changed, consider splitting terrakube-api type/refactor Refactor, performance or style change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants