Skip to content

Commit

Permalink
Make NettyTransportClient.getCurrentId() thread safe (#1707)
Browse files Browse the repository at this point in the history
Fix issue #1705.

- Use CAS to make it thread safe and limited in the declared range.

Signed-off-by: cj <power4j@outlook.com>
  • Loading branch information
John-Chan authored Sep 14, 2020
1 parent b7b5403 commit 34fc435
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,12 @@ public ClusterResponse sendRequest(ClusterRequest request) throws Exception {
}

private int getCurrentId() {
if (idGenerator.get() > MAX_ID) {
idGenerator.set(0);
}
return idGenerator.incrementAndGet();
int pre, next;
do {
pre = idGenerator.get();
next = pre >= MAX_ID ? MIN_ID : pre + 1;
} while (!idGenerator.compareAndSet(pre, next));
return next;
}

/*public CompletableFuture<ClusterResponse> sendRequestAsync(ClusterRequest request) {
Expand All @@ -266,5 +268,6 @@ private int getCurrentId() {
return future;
}*/

private static final int MIN_ID = 1;
private static final int MAX_ID = 999_999_999;
}

0 comments on commit 34fc435

Please sign in to comment.