Skip to content

Commit

Permalink
sdk/java: increase index to core url monotically
Browse files Browse the repository at this point in the history
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
  • Loading branch information
boymanjor authored and iampogo committed Mar 8, 2017
1 parent 8a09678 commit b4a4163
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sdk/java/src/main/java/com/chain/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ private <T> T post(String path, Object body, ResponseCreator<T> respCreator)
int idx = this.urlIndex.get();
URL endpointURL;
try {
URI u = new URI(this.urls.get(idx).toString() + "/" + path);
URI u = new URI(this.urls.get(idx % this.urls.size()).toString() + "/" + path);
u = u.normalize();
endpointURL = new URL(u.toString());
} catch (MalformedURLException ex) {
Expand Down Expand Up @@ -473,7 +473,7 @@ private void nextURL(int failedIndex) {

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

Expand Down

0 comments on commit b4a4163

Please sign in to comment.