Skip to content

Commit 68e7577

Browse files
Cleanup thread data after processing request to fix memory leak (#1652)
cleanup thread data after processing request
1 parent fa920cb commit 68e7577

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

api/src/main/resources/custom_templates/ApiClient.mustache

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,22 +1100,26 @@ protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>
11001100

11011101
protected <T> T processResponse(CloseableHttpResponse response, TypeReference<T> returnType) throws ApiException, IOException, ParseException {
11021102
int statusCode = response.getCode();
1103-
lastStatusCodeByThread.put(Thread.currentThread().getId(), statusCode);
1103+
long threadId = Thread.currentThread().getId();
1104+
lastStatusCodeByThread.put(threadId, statusCode);
11041105
if (statusCode == HttpStatus.SC_NO_CONTENT) {
11051106
return null;
11061107
}
11071108

11081109
Map<String, List<String>> responseHeaders = transformResponseHeaders(response.getHeaders());
1109-
lastResponseHeadersByThread.put(Thread.currentThread().getId(), responseHeaders);
1110-
1111-
if (isSuccessfulStatus(statusCode)) {
1112-
return this.deserialize(response, returnType);
1113-
} else {
1114-
String message = EntityUtils.toString(response.getEntity());
1115-
throw new ApiException(message, statusCode, responseHeaders, message);
1116-
}
1117-
}
1118-
1110+
lastResponseHeadersByThread.put(threadId, responseHeaders);
1111+
1112+
try {
1113+
if (isSuccessfulStatus(statusCode)) {
1114+
return this.deserialize(response, returnType);
1115+
} else {
1116+
String message = EntityUtils.toString(response.getEntity());
1117+
throw new ApiException(message, statusCode, responseHeaders, message);
1118+
}
1119+
} finally {
1120+
cleanupThreadData(threadId);
1121+
}
1122+
}
11191123
/**
11201124
* Invoke API by sending HTTP request with the given options.
11211125
*
@@ -1282,6 +1286,13 @@ protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>
12821286
}
12831287
}
12841288

1289+
/**
1290+
* Removes thread-specific data from status and response header maps.
1291+
*/
1292+
private void cleanupThreadData(long threadId) {
1293+
lastStatusCodeByThread.remove(threadId);
1294+
lastResponseHeadersByThread.remove(threadId);
1295+
}
12851296

12861297
/**
12871298
* Update query and header parameters based on authentication settings.

0 commit comments

Comments
 (0)