Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fix a response leak when requests are canceled. This is a workaround for an upstream bug in okhttp.
- Loading branch information
1 parent
b9bd7d2
commit e192794
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
type: fix | ||
fix: | ||
description: Fix a response leak when requests are canceled. This is a workaround | ||
for an upstream bug in okhttp. | ||
links: | ||
- https://github.com/palantir/conjure-java-runtime/pull/1415 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...-clients/src/main/java/com/palantir/conjure/java/okhttp/ResponseCapturingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.conjure.java.okhttp; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* Workaround a bug in okhttp where cancellation in flight results in leaked responses. | ||
* https://github.com/square/okhttp/blob/d28d2cec21641b61f3d34e05dd52f43a717c2d32/okhttp/src/main/java/okhttp3/RealCall.java#L210-L213 | ||
*/ | ||
enum ResponseCapturingInterceptor implements Interceptor { | ||
INSTANCE; | ||
|
||
private static final ThreadLocal<Response> currentResponse = new ThreadLocal<>(); | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Response response = chain.proceed(chain.request()); | ||
currentResponse.set(response); | ||
return response; | ||
} | ||
|
||
static Optional<Response> getResponse() { | ||
return Optional.ofNullable(currentResponse.get()); | ||
} | ||
|
||
static void clearThreadState() { | ||
currentResponse.remove(); | ||
} | ||
} |