This repository was archived by the owner on May 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
feat: recycle channel on consecutive new stream failures #2903
Closed
+124
−5
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,9 @@ public class ChannelPoolDpImpl implements ChannelPool { | |
| private static final String DEFAULT_LOG_NAME = "pool"; | ||
| private static final AtomicInteger INDEX = new AtomicInteger(); | ||
|
|
||
| // TODO: Move to client configuration. | ||
| private static final int CONSECUTIVE_OPEN_SESSION_FAILURE_THRESHOLD = 5; | ||
|
|
||
| private final String poolLogId; | ||
|
|
||
| @VisibleForTesting volatile int minGroups; | ||
|
|
@@ -95,6 +98,12 @@ public class ChannelPoolDpImpl implements ChannelPool { | |
| @GuardedBy("this") | ||
| private boolean closed = false; | ||
|
|
||
| @GuardedBy("this") | ||
| private long lastRecycleNano = 0; | ||
|
|
||
| @GuardedBy("this") | ||
| private Duration recycleBackoff = Duration.ofMillis(1); | ||
|
|
||
| public ChannelPoolDpImpl( | ||
| Supplier<ManagedChannel> channelSupplier, | ||
| ChannelPoolConfiguration config, | ||
|
|
@@ -221,6 +230,8 @@ public void start(Listener responseListener, Metadata headers) { | |
| public void onBeforeSessionStart(PeerInfo peerInfo) { | ||
| afeId = AfeId.extract(peerInfo); | ||
| synchronized (ChannelPoolDpImpl.this) { | ||
| channelWrapper.consecutiveFailures = 0; | ||
| recycleBackoff = Duration.ofMillis(1); | ||
| rehomeChannel(channelWrapper, afeId); | ||
| sessionsPerAfeId.add(afeId); | ||
| } | ||
|
|
@@ -232,6 +243,8 @@ public void onClose(Status status, Metadata trailers) { | |
| synchronized (ChannelPoolDpImpl.this) { | ||
| if (afeId != null) { | ||
| sessionsPerAfeId.remove(afeId); | ||
| } else if (!status.isOk() && status.getCode() != Code.CANCELLED) { | ||
| channelWrapper.consecutiveFailures++; | ||
| } | ||
| releaseChannel(channelWrapper, status); | ||
| } | ||
|
|
@@ -306,12 +319,12 @@ private void releaseChannel(ChannelWrapper channelWrapper, Status status) { | |
| channelWrapper.group.numStreams--; | ||
| channelWrapper.numOutstanding--; | ||
|
|
||
| if (shouldRecycleChannel(status)) { | ||
| if (shouldRecycleChannel(channelWrapper, status)) { | ||
| recycleChannel(channelWrapper); | ||
| } | ||
| } | ||
|
|
||
| private static boolean shouldRecycleChannel(Status status) { | ||
| private static boolean shouldRecycleChannel(ChannelWrapper channelWrapper, Status status) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also unrelated to this PR.. I'm wondering how this works.. if we get unimplemented, shouldn't the client fallback to unary? (iiuc which doesn't use this channel pool) recycling the channel and try to create a new one likely still won't work?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we will eventually fallback to unary if recycling doesn't help. But I've added a backoff not to spam new connections rapidly and indefinitely. |
||
| if (status.getCode() == Code.UNIMPLEMENTED) { | ||
| return true; | ||
| } | ||
|
|
@@ -322,6 +335,10 @@ private static boolean shouldRecycleChannel(Status status) { | |
| return true; | ||
| } | ||
|
|
||
| if (channelWrapper.consecutiveFailures >= CONSECUTIVE_OPEN_SESSION_FAILURE_THRESHOLD) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -332,6 +349,13 @@ private void recycleChannel(ChannelWrapper channelWrapper) { | |
| return; | ||
| } | ||
|
|
||
| if (lastRecycleNano > System.nanoTime() - recycleBackoff.toNanos()) { | ||
| return; | ||
| } | ||
|
|
||
| lastRecycleNano = System.nanoTime(); | ||
| recycleBackoff = recycleBackoff.multipliedBy(2); | ||
|
|
||
| channelWrapper.group.channels.remove(channelWrapper); | ||
| channelWrapper.channel.shutdown(); | ||
| // Checking for starting group because we don't want to delete the stating group. | ||
|
|
@@ -480,6 +504,7 @@ static class ChannelWrapper { | |
| private final ManagedChannel channel; | ||
| private final Instant createdAt; | ||
| private int numOutstanding = 0; | ||
| private int consecutiveFailures = 0; | ||
|
|
||
| public ChannelWrapper(AfeChannelGroup group, ManagedChannel channel, Clock clock) { | ||
| this.group = group; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR, but I feel releaseChannel should be called releaseStream or something, with recycleChannel the naming gets a little confusing..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the meaning here was that a session is closing and releasing the channel it was using.