Skip to content

Commit b4a4163

Browse files
boymanjoriampogo
authored andcommitted
sdk/java: increase index to core url monotically
The client keeps a list of core URLs which it uses to load balance requests to the server. An index is kept corresponding to a position in this list. When an error occurs the index is increased. Previously, it was possible for separate requests to read the same index and increase it separately. We have updated the logic to mitigate this by finding the position in the list (performing the modulo operation) just before a request is made. Closes #727
1 parent 8a09678 commit b4a4163

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

sdk/java/src/main/java/com/chain/http/Client.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ private <T> T post(String path, Object body, ResponseCreator<T> respCreator)
318318
int idx = this.urlIndex.get();
319319
URL endpointURL;
320320
try {
321-
URI u = new URI(this.urls.get(idx).toString() + "/" + path);
321+
URI u = new URI(this.urls.get(idx % this.urls.size()).toString() + "/" + path);
322322
u = u.normalize();
323323
endpointURL = new URL(u.toString());
324324
} catch (MalformedURLException ex) {
@@ -473,7 +473,7 @@ private void nextURL(int failedIndex) {
473473

474474
// A request to the url at failedIndex just failed. Move to the next
475475
// URL in the list.
476-
int nextIndex = (failedIndex + 1) % this.urls.size();
476+
int nextIndex = failedIndex + 1;
477477
this.urlIndex.compareAndSet(failedIndex, nextIndex);
478478
}
479479

0 commit comments

Comments
 (0)