Search code examples
javaapache-flinkokhttp

When should I close response for Interceptor?


I have a Flink Job that is making a POST Request and an Interceptor that's handling 429 error code.

Somehow I'm wondering if I need to close the response in the Interceptor. Sometimes I'm facing a warning about closing the response, and I want to ensure that the code is working correctly.

Here's my Interceptor Class:

public Response intercept(Chain chain) throws IOException {

    Response response = chain.proceed(chain.request());

    if (!response.isSuccessful() && response.code() == 429) {
        LOG.error("Rate limit exceeded. Waiting 20 seconds.");
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        response = chain.proceed(chain.request());
    }
    return response;
}

Should it be enough if I close it before the return? Like the following

response.body().close();
return response;

Thanks!


Solution

  • You mustn’t close the response in an interceptor. If you do, the response will be unreadable to the code that made the request.