-
Notifications
You must be signed in to change notification settings - Fork 4k
Handle slow security policies without blocking gRPC threads. #10633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d805bd6
Handle slow security policies without blocking gRPC threads.
mateusazis 54cb525
- Short-circuit immediately resolved auth futures.
mateusazis 26b8cc2
fix indentation
mateusazis 09a0190
Address jdcormie's comments
mateusazis 438734a
- Fix indentation
mateusazis 28eccc5
Exception -> RuntimeException
mateusazis 8cf4885
- Refactor test helpers.
mateusazis 889c524
Move ListenableFuture handling logic to BinderTransportSecurity
mateusazis 6a7fd67
Move the lifecycle management of the executor to the BinderServer.
mateusazis 5559320
- revert formatting changes in ServerSecurityPolicyTest
mateusazis 6f6cde9
extra lint and style fixes
mateusazis 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
102 changes: 102 additions & 0 deletions
102
binder/src/main/java/io/grpc/binder/internal/PendingAuthListener.java
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 |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package io.grpc.binder.internal; | ||
|
|
||
| import io.grpc.Metadata; | ||
| import io.grpc.ServerCall; | ||
| import io.grpc.ServerCallHandler; | ||
| import io.grpc.Status; | ||
|
|
||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * A {@link ServerCall.Listener} that can be returned by a {@link io.grpc.ServerInterceptor} to | ||
| * asynchronously advance the gRPC pending resolving a possibly asynchronous security policy check. | ||
| */ | ||
| final class PendingAuthListener<ReqT, RespT> extends ServerCall.Listener<ReqT> { | ||
|
|
||
| private final ConcurrentLinkedQueue<ListenerConsumer<ReqT>> pendingSteps = | ||
| new ConcurrentLinkedQueue<>(); | ||
| private final AtomicReference<ServerCall.Listener<ReqT>> delegateRef = | ||
| new AtomicReference<>(null); | ||
|
|
||
| PendingAuthListener() {} | ||
|
|
||
| void startCall(ServerCall<ReqT, RespT> call, | ||
| Metadata headers, | ||
| ServerCallHandler<ReqT, RespT> next) { | ||
| ServerCall.Listener<ReqT> delegate; | ||
| try { | ||
| delegate = next.startCall(call, headers); | ||
| } catch (RuntimeException e) { | ||
| call.close( | ||
| Status | ||
| .INTERNAL | ||
| .withCause(e) | ||
| .withDescription("Failed to start server call after authorization check"), | ||
| new Metadata()); | ||
| return; | ||
| } | ||
| delegateRef.set(delegate); | ||
| maybeRunPendingSteps(); | ||
| } | ||
|
|
||
| /** | ||
| * Runs any enqueued step in this ServerCall listener as long as the authorization check is | ||
| * complete. Otherwise, no-op and returns immediately. | ||
| */ | ||
| private void maybeRunPendingSteps() { | ||
| @Nullable ServerCall.Listener<ReqT> delegate = delegateRef.get(); | ||
| if (delegate == null) { | ||
| return; | ||
| } | ||
|
|
||
| // This section is synchronized so that no 2 threads may attempt to retrieve elements from the | ||
| // queue in order but end up executing the steps out of order. | ||
| synchronized (this) { | ||
| ListenerConsumer<ReqT> nextStep; | ||
|
mateusazis marked this conversation as resolved.
|
||
| while ((nextStep = pendingSteps.poll()) != null) { | ||
| nextStep.accept(delegate); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onCancel() { | ||
| pendingSteps.offer(ServerCall.Listener::onCancel); | ||
| maybeRunPendingSteps(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onComplete() { | ||
| pendingSteps.offer(ServerCall.Listener::onComplete); | ||
| maybeRunPendingSteps(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onHalfClose() { | ||
| pendingSteps.offer(ServerCall.Listener::onHalfClose); | ||
| maybeRunPendingSteps(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessage(ReqT message) { | ||
| pendingSteps.offer(delegate -> delegate.onMessage(message)); | ||
| maybeRunPendingSteps(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onReady() { | ||
| pendingSteps.offer(ServerCall.Listener::onReady); | ||
| maybeRunPendingSteps(); | ||
| } | ||
|
|
||
| /** | ||
| * Similar to Java8's {@link java.util.function.Consumer}, but redeclared in order to support | ||
| * Android SDK 21. | ||
| */ | ||
| private interface ListenerConsumer<ReqT> { | ||
| void accept(ServerCall.Listener<ReqT> listener); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.