Java · Track 3 · Tutorial 2 of 6
Send it safely more than once
Make sending a month safe to repeat: a retry that never counts twice, and code that waits when BrizoConsol asks it to.
1Why sending twice happens
A network drops, a script is run twice, a scheduler fires again. Your code needs to send a month again without it counting twice - and to know when BrizoConsol simply asks it to wait.
2Use an Idempotency-Key
Month.java sends a header made of the company, the month and a version:
Map.of("Idempotency-Key", Brizo.COMPANY_ID + "-" + period + "-" + version)Send it again and BrizoConsol finds the key, answers with the first request, and writes nothing.
gradle -q run -Pmain=MonthCheck
Trial balance: request 92 - 6 created, 0 updated, 0 unchanged, 0 refused
Recalculation: pendingThe same request number as before: nothing was written the second time.
3Change the key when the figures change
A key that was already used never writes again - even if the figures in the body are different. When a month's figures change, change version - v2, v3 - so the new figures are written.
4Let the client wait when a company is busy
BrizoConsol writes one request at a time for a company. A second one arriving meanwhile is turned away with 429 and write_in_flight; nothing was written, and Retry-After says how many seconds to wait. Brizo.call() already does this, up to five attempts:
Brizo.java
// Every call after sign-in goes through here. It sends the access token, and handles the two answers that mean
// "send it again": 401 (the token expired) and 429 (the company is busy, or the minute's requests are used up).
public static Answer call(String method, String path, Object body, Map<String, String> headers) throws Exception {
if (tokens == null) {
getToken();
}
boolean refreshed = false;
for (int attempt = 1; attempt <= 5; attempt++) {
Map<String, String> withToken = new HashMap<>(headers);
withToken.put("Authorization", "Bearer " + tokens.path("access_token").asText());
Answer answer = send(method, path, body, withToken);
if (answer.status() == 401 && !refreshed) {
refreshToken();
refreshed = true;
continue;
}
if (answer.status() == 429) {
String wait = answer.headers().getOrDefault("retry-after", "5");
Thread.sleep(1000L * (wait.matches("\\d+") ? Long.parseLong(wait) : 5));
continue; // nothing was written for this call
}
return answer;
}
throw new RuntimeException(method + " " + path + " is still answering 429 after five attempts.");
}
public static Answer call(String method, String path, Object body) throws Exception {
return call(method, path, body, Map.of());
}
public static Answer call(String method, String path) throws Exception {
return call(method, path, null, Map.of());
}Brizo.call() waits as asked and sends again.5Send in order, not at once
Send one company's requests one after another, not side by side: they'd only queue up behind each other's 429s. Different companies can be sent in parallel - but your group shares 60 requests a minute.
Want more detail? See the documentation