Reputation: 1240
I'm trying to protect against a particular error case I'm running into. When I make an HttpResponse to build a request and execute it, I run the risk of hitting some rate limit issues (even using a Google RateLimiter to try and alleviate this). What I thought would make sense is to use an HttpBackOffUnsuccessfulResponseHandler with an ExponentialBackoff and that would solve the problem (as the wait times being requested are very small.)
However, in the course of testing, I've come to realize the api endpoint I'm hitting requires a nonce and you can't make the same call in succession with the same nonce (this data is part of the header on the request.)
I'm trying to determine if there is a way I can use the existing HttpBackOffUnsuccessfulResponseHandler and have it update one of the headers before each retry. Is this something easy to do, or am I basically going to have to create my own version of HttpBackOffUnsuccessfulResponseHandler? Or at this point, would it make sense that in the case of detecting a rate limit violation, to just catch then manually and rebuild/resend the request after waiting for the designated time? (The API endpoints I'm hitting will include how much longer I need to wait due to rate limiting.)
I considered just extending HttpBackOffUnsuccessfulResponseHandler but the way it is implemented, I cannot override the handleResponse call that might give me a chance to update the request before it is resent.
And example of how I'm currently building the request is as follows (sanitized):
//Please note these headers are just an example. This data should not be treated as real.
HttpHeaders headers = new HttpHeaders()
.setContentType("text/plain").setContentLength(0L)
.set("header-1", header1Data.toString)
.set("payload", payload.toString); // Contains the nonce that would need to be updated.
HttpResponse resp = httpRequestFactory.buildPostRequest(url, null)
.setHeaders(headers)
.setConnectTimeout(30000)
.setReadTimeout(30000)
.setUnsuccessfulResponseHandler(
new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackoff())
//BlanketBackoffRequirement is a custom implementation of the BackOffRequired interface
.setBackOffRequired(BlanketBackoffRequirement.SERVER_ERROR_OR_RATE_LIMIT)
)
.execute();
Upvotes: 1
Views: 196