Skip to content

Commit

Permalink
Fix infinite loop when retrying to get a connection
Browse files Browse the repository at this point in the history
- Fix infinite loop
- CHANGELOG.md

Closes #440
  • Loading branch information
nickkkccc committed Nov 16, 2023
1 parent 8600dd2 commit 4ed593a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bugfixes

- Fix Instant converter to parse 8 bytes datetime ([#408](https://github.com/tarantool/cartridge-java/issues/408))
- Fix infinite loop when retrying to get a connection ([#440](https://github.com/tarantool/cartridge-java/issues/440))

### Internal and API changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,27 @@ static final class RoundRobinStrategy implements ConnectionSelectionStrategy {

private final TarantoolConnectionIterator connectionIterator;
private final AtomicInteger available;
private final int connectionsCount;

RoundRobinStrategy(Collection<TarantoolConnection> connections) {
this.available = new AtomicInteger(connections.size());
this.connectionsCount = connections.size();
this.available = new AtomicInteger(this.connectionsCount);
this.connectionIterator = new TarantoolConnectionIterator(connections.stream()
.peek(conn -> conn.addConnectionCloseListener(c -> available.getAndDecrement()))
.collect(Collectors.toList()));
}

@Override
public TarantoolConnection next() throws NoAvailableConnectionsException {
if (available.get() > 0) {
while (connectionIterator.hasNext()) {
TarantoolConnection connection = connectionIterator.next();
if (connection.isConnected()) {
return connection;
}
int attempts = 0;
while (available.get() > 0 && connectionIterator.hasNext()) {
TarantoolConnection connection = connectionIterator.next();
if (connection.isConnected()) {
return connection;
}

if (++attempts > connectionsCount) {
break;
}
}
throw new NoAvailableConnectionsException();
Expand Down Expand Up @@ -84,10 +89,12 @@ static final class ParallelRoundRobinStrategy implements ConnectionSelectionStra
private final TarantoolClientConfig config;
private final CyclingIterator<TarantoolConnectionIterator> iteratorsIterator;
private final AtomicInteger available;
private final int connectionsCount;

ParallelRoundRobinStrategy(TarantoolClientConfig config, Collection<TarantoolConnection> connections) {
this.config = config;
this.available = new AtomicInteger(connections.size());
this.connectionsCount = connections.size();
this.available = new AtomicInteger(this.connectionsCount);
this.iteratorsIterator = new CyclingIterator<>(populateIterators(connections));
}

Expand All @@ -106,12 +113,14 @@ private Collection<TarantoolConnectionIterator> populateIterators(

@Override
public TarantoolConnection next() throws NoAvailableConnectionsException {
if (available.get() > 0) {
while (iteratorsIterator.hasNext()) {
TarantoolConnection connection = iteratorsIterator.next().next();
if (connection.isConnected()) {
return connection;
}
int attempts = 0;
while (available.get() > 0 && iteratorsIterator.hasNext()) {
TarantoolConnection connection = iteratorsIterator.next().next();
if (connection.isConnected()) {
return connection;
}
if (++attempts > connectionsCount) {
break;
}
}
throw new NoAvailableConnectionsException();
Expand Down

0 comments on commit 4ed593a

Please sign in to comment.