I'm trying to use a recently added feature from OkHttp 3.12.0: full-operation timeouts. For that, I also rely on the new Invocation
class from retrofit 2.5.0 that allows me to retrieve the method annotations.
The annotation is:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Timeout {
int value();
TimeUnit unit();
}
The retrofit interface is:
public interface AgentApi {
@Timeout(value = 100, unit = TimeUnit.MILLISECONDS)
@GET("something")
Call<String> getSomething();
}
And the interceptor is:
class TimeoutInterceptor implements Interceptor {
@NonNull
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request();
final Invocation tag = request.tag(Invocation.class);
final Method method = tag != null ? tag.method() : null;
final Timeout timeout = method != null ? method.getAnnotation(Timeout.class) : null;
if (timeout != null) {
chain.call().timeout().timeout(timeout.value(), timeout.unit());
}
return chain.proceed(request);
}
}
I've correctly added the TimeoutInterceptor with .addInterceptor(...)
in the OkHttpClient provided to the Retrofit Builder.
Unfortunately, it doesn't work as I expected. The calls are not failing when the timeout is reached?
Though it works fine when using the chain methods from the interceptor:
chain
.withConnectTimeout(connect, unit)
.withReadTimeout(read, unit)
.withWriteTimeout(write, unit)
Is this because the call timeout must be set before the call is enqueued? (and the Interceptor is triggered too late in the process?), or is this something else?
from OkHttp Call timeout from Retrofit Interceptor using Annotations is not applied
No comments:
Post a Comment