Skip to content

Commit 0af80ff

Browse files
committed
Add per-request timeout.
1 parent 9c89cc1 commit 0af80ff

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/main/java/com/andre601/javabotblockapi/requests/PostAction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void postGuilds(@NotNull JDA jda, @NotNull BotBlockAPI botBlockAPI) throw
174174

175175
botBlockAPI.getAuthTokens().forEach(json::put);
176176

177-
REQUEST_HANDLER.performPOST(json);
177+
REQUEST_HANDLER.performPOST(json, botBlockAPI.getAuthTokens().size());
178178
}
179179

180180
/**
@@ -227,7 +227,7 @@ public void postGuilds(@NotNull ShardManager shardManager, @NotNull BotBlockAPI
227227
json.put("shards", new JSONArray(Arrays.deepToString(shards.toArray())));
228228
botBlockAPI.getAuthTokens().forEach(json::put);
229229

230-
REQUEST_HANDLER.performPOST(json);
230+
REQUEST_HANDLER.performPOST(json, botBlockAPI.getAuthTokens().size());
231231
}
232232

233233
/**
@@ -252,7 +252,7 @@ public void postGuilds(@NotNull String botId, int guilds, @NotNull BotBlockAPI b
252252

253253
botBlockAPI.getAuthTokens().forEach(json::put);
254254

255-
REQUEST_HANDLER.performPOST(json);
255+
REQUEST_HANDLER.performPOST(json, botBlockAPI.getAuthTokens().size());
256256
}
257257

258258
}

src/main/java/com/andre601/javabotblockapi/requests/RequestHandler.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,23 @@ private JSONObject performGET(@NotNull String url) throws IOException, Ratelimit
115115
}
116116
}
117117

118-
void performPOST(@NotNull JSONObject json) throws IOException, RatelimitedException{
118+
void performPOST(@NotNull JSONObject json, int sites) throws IOException, RatelimitedException{
119119
String url = BASE_URL + "count";
120120

121+
OkHttpClient postClient = CLIENT.newBuilder()
122+
.callTimeout(sites*10, TimeUnit.SECONDS)
123+
.readTimeout(sites*10, TimeUnit.SECONDS)
124+
.writeTimeout(sites*10, TimeUnit.SECONDS)
125+
.build();
126+
121127
RequestBody body = RequestBody.create(json.toString(), null);
122128
Request request = new Request.Builder()
123129
.url(url)
124130
.addHeader("Content-Type", "application/json")
125131
.post(body)
126132
.build();
127133

128-
try(Response response = CLIENT.newCall(request).execute()){
134+
try(Response response = postClient.newCall(request).execute()){
129135
ResponseBody responseBody = response.body();
130136
if(responseBody == null)
131137
throw new NullPointerException("Received empty response body.");
@@ -148,25 +154,25 @@ void performPOST(@NotNull JSONObject json) throws IOException, RatelimitedExcept
148154
if(responseJson.has("failure")){
149155
JSONObject failure = responseJson.getJSONObject("failure");
150156

151-
List<String> sites = new ArrayList<>();
157+
List<String> failedSites = new ArrayList<>();
152158
for(String key : failure.keySet()){
153159
try{
154160
JSONArray array = failure.getJSONArray(key);
155-
sites.add(String.format(
161+
failedSites.add(String.format(
156162
"Site: %s, Code: %d, Message: %s",
157163
key,
158164
array.getInt(0),
159165
array.getString(1)
160166
));
161167
}catch(JSONException ex){
162168
Map<String, Object> notFound = failure.toMap();
163-
sites.add("Errors: " + notFound.toString());
169+
failedSites.add("Errors: " + notFound.toString());
164170
}
165171
}
166172

167173
throw new IOException(String.format(
168174
"One or multiple post requests failed! Response(s): %s",
169-
String.join(", ", sites)
175+
String.join(", ", failedSites)
170176
));
171177
}
172178
}

0 commit comments

Comments
 (0)