Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @author <a href="https://github.com/bitgorbovsky">Ivan Bannikov</a>
*/
public interface IProtoClientPool {
public interface IProtoClientPool extends AutoCloseable {

/**
* Sets new instance groups configuration for pool.
Expand Down Expand Up @@ -115,6 +115,7 @@ CompletableFuture<IProtoClient> get(String tag, int index)
*
* @throws Exception the exception
*/
@Override
void close() throws Exception;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
import io.tarantool.core.protocol.Handlers;
import io.tarantool.core.protocol.IProtoResponse;
import io.tarantool.pool.PoolEntry.PoolEntryMetrics;
import io.tarantool.pool.availability.AvailabilityPolicy;
import io.tarantool.pool.availability.Metadata;
import io.tarantool.pool.availability.heartbeat.DefaultAvailabilityPolicy;
import io.tarantool.pool.availability.heartbeat.DefaultNoopAvailabilityPolicy;
import io.tarantool.pool.exceptions.PoolClosedException;
import io.tarantool.pool.exceptions.PoolException;

Expand Down Expand Up @@ -97,8 +101,12 @@ public class IProtoClientPoolImpl implements IProtoClientPool {
/** Flag switching graceful shutdown handling. */
private final boolean gracefulShutdown;

/** Options for enabling and tuning heartbeats. */
private final HeartbeatOpts heartbeatOpts;
/**
* The default availability policy of the connections. It is used for the groups that do not set
* their own policy: the heartbeat policy if the heartbeat options are set, otherwise the policy
* that never changes the availability.
*/
private final AvailabilityPolicy availabilityPolicy;

/** Options for tuning watchers in connections. */
private final WatcherOptions watcherOpts;
Expand Down Expand Up @@ -334,7 +342,10 @@ public IProtoClientPoolImpl(
this.reconnectAfter = 1_000L;
this.isClosed = new AtomicBoolean(false);
this.gracefulShutdown = gracefulShutdown;
this.heartbeatOpts = heartbeatOpts;
this.availabilityPolicy =
heartbeatOpts == null
? new DefaultNoopAvailabilityPolicy()
: new DefaultAvailabilityPolicy(heartbeatOpts);
this.connectionStatistics = new ConnectionStatistics();
this.watcherOpts = watcherOpts;
this.metricsRegistry = metricsRegistry;
Expand All @@ -348,9 +359,16 @@ public IProtoClientPoolImpl(

@Override
public void setGroups(List<InstanceConnectionGroup> clientGroups) {
if (isClosed.get()) {
throw new PoolClosedException("Pool is already closed!");
}

Map<String, Boolean> actualTags = new HashMap<>();
int newTotal = 0;
synchronized (connectionPoolLock) {
if (isClosed.get()) {
throw new PoolClosedException("Pool is already closed!");
}
for (InstanceConnectionGroup group : clientGroups) {
String tag = group.getTag();
int size = group.getSize();
Expand Down Expand Up @@ -508,7 +526,7 @@ public void setReconnectAfter(long reconnectAfter) throws IllegalArgumentExcepti
public void forEach(Consumer<IProtoClient> action) {
for (List<PoolEntry> group : entries.values()) {
for (PoolEntry entry : group) {
action.accept(entry.getClient());
action.accept(entry.client());
}
}
}
Expand All @@ -531,12 +549,13 @@ private void expandGroup(List<PoolEntry> connects, InstanceConnectionGroup group
new PoolEntry(
factory,
timerResource.get(),
group,
connects.size(),
new Metadata(group, connects.size()) {},
gracefulShutdown,
connectTimeout,
reconnectAfter,
heartbeatOpts,
group.getAvailabilityPolicy() != null
? group.getAvailabilityPolicy()
: this.availabilityPolicy,
watcherOpts,
this.connectionStatistics,
metricsRegistry,
Expand Down Expand Up @@ -571,7 +590,6 @@ private void shrinkGroup(List<PoolEntry> connects, int count) {
for (int i = connects.size() - 1; i >= count; i--) {
PoolEntry entry = connects.remove(i);
if (entry != null) {
entry.unlock();
entry.close();
}
}
Expand Down
Loading
Loading