Skip to content

Commit a1ff8ec

Browse files
committed
add backoff for recycling a channel
1 parent c6206f8 commit a1ff8ec

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/channels/ChannelPoolDpImpl.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public class ChannelPoolDpImpl implements ChannelPool {
6565
private static final String DEFAULT_LOG_NAME = "pool";
6666
private static final AtomicInteger INDEX = new AtomicInteger();
6767

68-
private static final int CONSECUTIVE_OPEN_FAILURE_THRESHOLD = 5;
68+
// TODO: Move to client configuration.
69+
private static final int CONSECUTIVE_OPEN_SESSION_FAILURE_THRESHOLD = 5;
6970

7071
private final String poolLogId;
7172

@@ -97,6 +98,12 @@ public class ChannelPoolDpImpl implements ChannelPool {
9798
@GuardedBy("this")
9899
private boolean closed = false;
99100

101+
@GuardedBy("this")
102+
private long lastRecycleNano = 0;
103+
104+
@GuardedBy("this")
105+
private Duration recycleBackoff = Duration.ofMillis(1);
106+
100107
public ChannelPoolDpImpl(
101108
Supplier<ManagedChannel> channelSupplier,
102109
ChannelPoolConfiguration config,
@@ -224,6 +231,7 @@ public void onBeforeSessionStart(PeerInfo peerInfo) {
224231
afeId = AfeId.extract(peerInfo);
225232
synchronized (ChannelPoolDpImpl.this) {
226233
channelWrapper.consecutiveFailures = 0;
234+
recycleBackoff = Duration.ofMillis(1);
227235
rehomeChannel(channelWrapper, afeId);
228236
sessionsPerAfeId.add(afeId);
229237
}
@@ -327,7 +335,7 @@ private static boolean shouldRecycleChannel(ChannelWrapper channelWrapper, Statu
327335
return true;
328336
}
329337

330-
if (channelWrapper.consecutiveFailures >= CONSECUTIVE_OPEN_FAILURE_THRESHOLD) {
338+
if (channelWrapper.consecutiveFailures >= CONSECUTIVE_OPEN_SESSION_FAILURE_THRESHOLD) {
331339
return true;
332340
}
333341

@@ -341,6 +349,13 @@ private void recycleChannel(ChannelWrapper channelWrapper) {
341349
return;
342350
}
343351

352+
if (lastRecycleNano > System.nanoTime() - recycleBackoff.toNanos()) {
353+
return;
354+
}
355+
356+
lastRecycleNano = System.nanoTime();
357+
recycleBackoff = recycleBackoff.multipliedBy(2);
358+
344359
channelWrapper.group.channels.remove(channelWrapper);
345360
channelWrapper.channel.shutdown();
346361
// Checking for starting group because we don't want to delete the stating group.

0 commit comments

Comments
 (0)