Skip to content

Commit

Permalink
Close responses to avoid leaked connections (#60)
Browse files Browse the repository at this point in the history
As it seems, only closing the body of a request is not sufficient. With
this, we close all responses we get from requests. Doing so, I have not
gotten any leaked connection error in my setup.
  • Loading branch information
pfeuffer authored May 13, 2024
1 parent 22b9920 commit 540ed61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
16 changes: 9 additions & 7 deletions src/main/java/com/cloudogu/scmmanager/ScmMigratedV1Notifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ public void notify(String revision, BuildStatus buildStatus) {

@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isRedirect()) {
String location = response.header("Location");
if (!Strings.isNullOrEmpty(location)) {
notifyV2(location, revision, buildStatus);
try (response) {
if (response.isRedirect()) {
String location = response.header("Location");
if (!Strings.isNullOrEmpty(location)) {
notifyV2(location, revision, buildStatus);
} else {
LOG.warn("server returned redirect without location header");
}
} else {
LOG.warn("server returned redirect without location header");
LOG.debug("expected redirect, but server returned status code {}", response.code());
}
} else {
LOG.debug("expected redirect, but server returned status code {}", response.code());
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/cloudogu/scmmanager/ScmV2Notifier.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cloudogu.scmmanager;

import com.google.common.annotations.VisibleForTesting;
import io.jenkins.plugins.okhttp.api.JenkinsOkHttpClient;
import net.sf.json.JSONObject;
import okhttp3.Call;
import okhttp3.Callback;
Expand Down Expand Up @@ -101,12 +100,14 @@ public void onFailure(Call call, IOException e) {
}

@Override
public void onResponse(Call call, Response response) throws IOException {
LOG.info(
"status notify for repository {} and revision {} returned {}",
namespaceAndName, revision, response.code()
);
completionListener.accept(response);
public void onResponse(Call call, Response response) {
try (response) {
LOG.info(
"status notify for repository {} and revision {} returned {}",
namespaceAndName, revision, response.code()
);
completionListener.accept(response);
}
}
});
}
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/com/cloudogu/scmmanager/scm/api/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ public void onFailure(Call call, IOException ex) {

@Override
public void onResponse(Call call, Response response) {
if (response.code() == 200) {
try (ResponseBody body = response.body()) {
if (body == null) {
future.complete(null);
} else {
T t = objectMapper.readValue(body.bytes(), type);
future.complete(t);
try (response) {
if (response.code() == 200) {
try (ResponseBody body = response.body()) {
if (body == null) {
future.complete(null);
} else {
T t = objectMapper.readValue(body.bytes(), type);
future.complete(t);
}
} catch (Exception ex) {
future.completeExceptionally(ex);
}
} catch (Exception ex) {
future.completeExceptionally(ex);
} else {
future.completeExceptionally(new IllegalReturnStatusException(response.code()));
}
} else {
future.completeExceptionally(new IllegalReturnStatusException(response.code()));
}
}
});
Expand Down

0 comments on commit 540ed61

Please sign in to comment.