-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathRobustClient.java
More file actions
297 lines (256 loc) · 8.92 KB
/
RobustClient.java
File metadata and controls
297 lines (256 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package oakbot.chat;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.SSLHandshakeException;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Executes an HTTP request, retrying after a short pause if the request fails
* due to glitchy network problems such as socket timeouts and empty HTTP
* responses.
* @author Michael Angstadt
*/
public class RobustClient {
private static final Logger logger = Logger.getLogger(RobustClient.class.getName());
private static final Pattern response409Regex = Pattern.compile("\\d+");
private final CloseableHttpClient client;
private final HttpUriRequest request;
private long retryPause = 5000;
private int maxAttempts = 3, attempts;
private List<Integer> expectedStatusCodes;
/**
* @param client the HTTP client
* @param request the request to send
*/
public RobustClient(CloseableHttpClient client, HttpUriRequest request) {
this.client = client;
this.request = request;
}
/**
* Sets the amount of time to wait between retries (defaults to 5 seconds).
* @param retryPause the amount of time in milliseconds
* @return this
*/
public RobustClient retryPause(long retryPause) {
this.retryPause = retryPause;
return this;
}
/**
* Sets the number of times to try sending the request before giving up
* (defaults to 3).
* @param attempts the number of attempts (must be greater than zero)
* @return this
*/
public RobustClient attempts(Integer attempts) {
this.maxAttempts = attempts;
return this;
}
/**
* <p>
* Sets the status code(s) that are expected to be returned in the response.
* If one of the status codes in this list is not returned, then the request
* will be retried. By default, ALL status codes are accepted.
* </p>
* <p>
* HTTP 404 responses are always treated as valid and are always returned.
* HTTP 409 responses, which indicate that the bot is sending messages too
* quickly, are automatically retried.
* </p>
* @param statusCodes the status codes
* @return this
*/
public RobustClient statusCodes(Integer... statusCodes) {
this.expectedStatusCodes = Arrays.asList(statusCodes);
return this;
}
/**
* Sends the request, parsing the response body as JSON. If the body does
* not contain valid JSON, then the request is retried.
* @return the response
* @throws IOException if a valid response was not returned after the
* specified number of attempts
*/
public JsonResponse asJson() throws IOException {
attempts = 0;
ObjectMapper mapper = new ObjectMapper();
while (attempts < maxAttempts) {
try (CloseableHttpResponse response = execute()) {
if (response.getStatusLine().getStatusCode() == 404) {
return new JsonResponse(response, true);
}
JsonNode node;
try (InputStream in = response.getEntity().getContent()) {
node = mapper.readTree(in);
}
return new JsonResponse(response, node);
} catch (JsonParseException e) {
//make the request again if a non-JSON response is returned
logger.log(Level.SEVERE, "Could not parse the response body as JSON. Retrying the request in " + retryPause + "ms.", e);
try {
Thread.sleep(retryPause);
} catch (InterruptedException e2) {
throw new RuntimeException(e2);
}
}
}
throw new IOException("Was expecting a JSON response body from " + request.getURI() + ", but never got parseable JSON after " + attempts + " attempts.");
}
/**
* Sends the request.
* @return the response
* @throws IOException if a valid response was not returned after the
* specified number of attempts
*/
public CloseableHttpResponse asHttp() throws IOException {
attempts = 0;
return execute();
}
/**
* Sends the request.
* @return the response
* @throws IOException if a valid response was not returned after the
* specified number of attempts
*/
private CloseableHttpResponse execute() throws IOException {
long sleep = 0;
final long maxSleep = TimeUnit.SECONDS.toMillis(60);
while (attempts < maxAttempts) {
attempts++;
if (sleep > 0) {
logger.info("Sleeping for " + sleep + " ms before resending the request...");
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/*
* Update the sleep amount for the next attempt (if there is one).
*/
sleep = attempts * retryPause;
if (sleep > maxSleep) {
sleep = maxSleep;
}
/*
* Send the request.
*/
CloseableHttpResponse response;
try {
response = client.execute(request);
} catch (NoHttpResponseException | SocketException | ConnectTimeoutException | SSLHandshakeException e) {
logger.log(Level.SEVERE, e.getClass().getSimpleName() + " thrown from request " + request.getURI() + ". Retrying.", e);
continue;
}
int actualStatusCode = response.getStatusLine().getStatusCode();
/*
* An HTTP 409 response means that the bot is sending messages too
* quickly. The response body contains the number of seconds the bot
* must wait before it can post another message.
*/
if (actualStatusCode == 409) {
String body = EntityUtils.toString(response.getEntity());
logger.info("HTTP " + actualStatusCode + " returned [url=" + request.getURI() + "]: " + body);
Long waitTime = parse409Response(body);
sleep = (waitTime == null) ? 5000 : waitTime;
//do not count this against the max attempts
attempts--;
HttpClientUtils.closeQuietly(response);
continue;
}
/**
* The bot sometimes gets HTTP 429 ("too many requests") responses
* when pinging the chat rooms for messages. This issue first
* occurred when the bot was in seven rooms at once, so it may have
* reached some kind of usage limit. It appears the bot can be in
* five rooms at a time without triggering this response code.
*/
if (actualStatusCode == 429) {
String body = EntityUtils.toString(response.getEntity());
logger.info("HTTP " + actualStatusCode + " returned [url=" + request.getURI() + "]: " + body);
sleep = 5000;
HttpClientUtils.closeQuietly(response);
continue;
}
/*
* Different requests handle 404s differently, so return the
* response if it's a 404.
*/
if (actualStatusCode == 404) {
return response;
}
/*
* If the status code was incorrect, re-send the request.
*/
if (expectedStatusCodes != null && !expectedStatusCodes.contains(actualStatusCode)) {
String body = EntityUtils.toString(response.getEntity());
logger.severe("The following status codes were expected " + expectedStatusCodes + ", but the actual status code was " + actualStatusCode + ". Retrying. The response body was: " + body);
HttpClientUtils.closeQuietly(response);
continue;
}
return response;
}
throw new IOException("Request to " + request.getURI() + " could not be sent after " + attempts + " attempts.");
}
/**
* Parses an HTTP 409 response, which indicates that the bot is sending
* messages too quickly.
* @param response the HTTP 409 response body (e.g. "You can perform this
* action again in 2 seconds")
* @return the amount of time (in milliseconds) the bot must wait before the
* chat system will accept new messages, or null if this value could not be
* parsed from the response
* @throws IOException if there's a problem getting the response body
*/
private static Long parse409Response(String body) throws IOException {
Matcher m = response409Regex.matcher(body);
if (!m.find()) {
return null;
}
int seconds = Integer.parseInt(m.group(0));
return TimeUnit.SECONDS.toMillis(seconds);
}
/**
* Represents an HTTP response whose body contains JSON.
* @author Michael Angstadt
*/
public static class JsonResponse {
private final CloseableHttpResponse response;
private final JsonNode body;
private final boolean http404;
public JsonResponse(CloseableHttpResponse response, JsonNode body) {
this.response = response;
this.body = body;
this.http404 = false;
}
public JsonResponse(CloseableHttpResponse response, boolean http404) {
this.response = response;
this.body = null;
this.http404 = true;
}
public CloseableHttpResponse getResponse() {
return response;
}
public JsonNode getBody() {
return body;
}
public boolean isHttp404() {
return http404;
}
}
}