Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release limiters when response is returned #1255

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@
import com.netflix.concurrency.limits.Limiter;
import com.palantir.logsafe.Preconditions;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import okhttp3.Interceptor;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSource;

/**
* Flow control in Conjure is a collaborative effort between servers and clients. Servers advertise an overloaded state
Expand All @@ -52,7 +46,7 @@
* 429 and 503 response codes are used for backpressure, whilst 200 -> 399 request codes are used for determining
* new limits and all other codes are not factored in to timings.
* <p>
* Concurrency permits are only released when the response body is closed.
* Concurrency permits are released when the response is received.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was discussed in the past and in general would be kinda against the principle of concurrency limiting: the fact that the body is being streamed usually means that the worker thread might have been released, but streaming the body still usually consumes resources (especially if it's just a blocking read that goes from some external resource and writes to the response body).

I think the reason why you are not observing any 429 in general in that situation is that the internal product that I expect you're referring too has request/sec rate limiting implemented, not concurrency limiting. I suspect releasing more of requests here would eventually lead to 503s from the product.

*/
final class ConcurrencyLimitingInterceptor implements Interceptor {
private static final ImmutableSet<Integer> DROPPED_CODES = ImmutableSet.of(429, 503);
Expand All @@ -75,71 +69,16 @@ public Response intercept(Chain chain) throws IOException {
if (DROPPED_CODES.contains(response.code())) {
listener.onDropped();
return response;
} else if (!response.isSuccessful() || response.isRedirect()) {
} else if (!response.isSuccessful() || response.isRedirect() || response.body() == null) {
listener.onIgnore();
return response;
} else {
return wrapResponse(listener, response);
listener.onSuccess();
return response;
}
} catch (IOException e) {
listener.onIgnore();
throw e;
}
}

private static Response wrapResponse(Limiter.Listener listener, Response response) throws IOException {
// OkHttp guarantees not-null to execute() and callbacks, but not at this level.
if (response.body() == null) {
listener.onIgnore();
return response;
} else if (response.body().source().exhausted()) {
// this case exists for Feign, which does not properly close empty responses
listener.onSuccess();
return response;
}
ResponseBody currentBody = response.body();
ResponseBody newResponseBody =
ResponseBody.create(
currentBody.contentType(),
currentBody.contentLength(),
wrapSource(currentBody.source(), listener));
return response.newBuilder()
.body(newResponseBody)
.build();
}

private static BufferedSource wrapSource(BufferedSource currentSource, Limiter.Listener listener) {
return (BufferedSource) Proxy.newProxyInstance(
BufferedSource.class.getClassLoader(),
new Class<?>[] {BufferedSource.class},
new ReleaseConcurrencyLimitProxy(currentSource, listener));
}

/**
* This proxy enables e.g. Okio to make additive additions to their API without breaking us.
*/
private static final class ReleaseConcurrencyLimitProxy implements InvocationHandler {
private final BufferedSource delegate;
private final Limiter.Listener listener;
private boolean closed = false;

private ReleaseConcurrencyLimitProxy(BufferedSource delegate, Limiter.Listener listener) {
this.delegate = delegate;
this.listener = listener;
}

@Override
public Object invoke(Object _proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("close") && !closed) {
closed = true;
listener.onSuccess();
}

try {
return method.invoke(delegate, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ public void wrapsResponseBody() throws IOException {
String data = "data";
ResponseBody body = ResponseBody.create(MediaType.parse("application/json"), data);
when(chain.proceed(request)).thenReturn(response.newBuilder().body(body).build());
Response wrappedResponse = interceptor.intercept(chain);
verifyZeroInteractions(listener);
assertThat(wrappedResponse.body().string()).isEqualTo(data);
Response interceptedResponse = interceptor.intercept(chain);
verify(listener).onSuccess();
// Previously onSuccess would be called after the response body was read. This asserts that that behavior no
// longer exists.
assertThat(interceptedResponse.body().string()).isEqualTo(data);
verifyZeroInteractions(listener);
}

@Test
Expand Down