feat(gax): implement dynamic channel refreshing on 401 retries#13212
feat(gax): implement dynamic channel refreshing on 401 retries#13212blakeli0 wants to merge 1 commit into
Conversation
4f508a8 to
9e55d01
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to automatically refresh transport channels when an UnauthenticatedException occurs, specifically within environments where the isMwlidEnvironment variable is set. Key changes include adding a refresh method to the TransportChannel interface and ChannelPool implementation, incorporating a 5-second debounce for refreshes, and updating the retry logic to trigger these refreshes. Review feedback highlights a potential bug in the debounce initialization, suggests using constants for magic numbers, recommends caching environment variable lookups to improve performance, and advises using imports instead of fully qualified names for better readability.
| private ScheduledFuture<?> resizeFuture = null; | ||
|
|
||
| private final Object entryWriteLock = new Object(); | ||
| private long lastRefreshTimeNanos = 0; |
There was a problem hiding this comment.
Initializing lastRefreshTimeNanos to 0 can lead to the first refresh being skipped if System.nanoTime() returns a value close to zero (which is possible depending on the JVM's arbitrary time origin). Additionally, the 5-second debounce interval should be defined as a constant.
| private long lastRefreshTimeNanos = 0; | |
| private static final long REFRESH_DEBOUNCE_THRESHOLD_NANOS = java.util.concurrent.TimeUnit.SECONDS.toNanos(5); | |
| private long lastRefreshTimeNanos = System.nanoTime() - REFRESH_DEBOUNCE_THRESHOLD_NANOS; |
| // replaces the list) | ||
| synchronized (entryWriteLock) { | ||
| long now = System.nanoTime(); | ||
| if (now - lastRefreshTimeNanos < TimeUnit.SECONDS.toNanos(5)) { |
| */ | ||
| @Override | ||
| public ApiFuture<ResponseT> submit(RetryingFuture<ResponseT> retryingFuture) { | ||
| if ("true".equalsIgnoreCase(System.getenv("isMwlidEnvironment"))) { |
| if (cause instanceof com.google.api.gax.rpc.UnauthenticatedException) { | ||
| RetryingContext context = retryingFuture.getRetryingContext(); | ||
| if (context instanceof com.google.api.gax.rpc.ApiCallContext) { | ||
| com.google.api.gax.rpc.TransportChannel transportChannel = | ||
| ((com.google.api.gax.rpc.ApiCallContext) context).getTransportChannel(); |
| if ("true".equalsIgnoreCase(System.getenv("isMwlidEnvironment")) | ||
| && previousThrowable instanceof UnauthenticatedException) { | ||
| return true; | ||
| } |
There was a problem hiding this comment.
9e55d01 to
188158f
Compare
This PR implements dynamic channel refreshing on 401 Unauthenticated retries under the isMwlidEnvironment environment variable. It introduces compile-time type-safe refresh contracts across TransportChannel and ApiCallContext, with debouncing protection in ChannelPool to prevent connection stampedes.