-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle slow security policies without blocking gRPC threads. (#10633)
* Handle slow security policies without blocking gRPC threads. - Introduce PendingAuthListener to handle a ListenableFuture<Status>, progressing the gRPC through each stage in sequence once the future completes and is OK. - Move unit tests away from `checkAuthorizationForService` and into `checkAuthorizationForServiceAsync` since that should be the only method called in production now. - Some tests in `ServerSecurityPolicyTest` had their expectations updated; they previously called synchornous APIs that transformed failed `ListenableFuture<Status>` into one or another status. Now, we call the sync API, so those transformations do not happen anymore, thus the test needs to deal with failed futures directly. - I couldn't figure out if this PR needs extra tests. AFAICT `BinderSecurityTest` should already cover the new codepaths, but please let me know otherwise.
- Loading branch information
1 parent
4477269
commit a053889
Showing
6 changed files
with
331 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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; | ||
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.