-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Override gRPC Context storage #28199
Merged
Merged
Changes from all commits
Commits
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 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
39 changes: 39 additions & 0 deletions
39
...loyment/src/test/java/io/quarkus/grpc/server/interceptors/GrpcContextPropagationTest.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,39 @@ | ||
package io.quarkus.grpc.server.interceptors; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.grpc.examples.helloworld.Greeter; | ||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.grpc.examples.helloworld.HelloReply; | ||
import io.grpc.examples.helloworld.HelloRequest; | ||
import io.quarkus.grpc.GrpcClient; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
/** | ||
* Test reproducing <a href="https://github.com/quarkusio/quarkus/issues/26830">#26830</a>. | ||
*/ | ||
public class GrpcContextPropagationTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer( | ||
() -> ShrinkWrap.create(JavaArchive.class) | ||
.addPackage(GreeterGrpc.class.getPackage()) | ||
.addClasses(MyFirstInterceptor.class, MyInterceptedGreeting.class)); | ||
|
||
@GrpcClient | ||
Greeter greeter; | ||
|
||
@Test | ||
void test() { | ||
HelloReply foo = greeter.sayHello(HelloRequest.newBuilder().setName("foo").build()).await().indefinitely(); | ||
assertThat(foo.getMessage()).isEqualTo("hello k1 - 1"); | ||
foo = greeter.sayHello(HelloRequest.newBuilder().setName("foo").build()).await().indefinitely(); | ||
assertThat(foo.getMessage()).isEqualTo("hello k1 - 2"); | ||
} | ||
|
||
} |
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
18 changes: 18 additions & 0 deletions
18
...c/deployment/src/test/java/io/quarkus/grpc/server/interceptors/MyInterceptedGreeting.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,18 @@ | ||
package io.quarkus.grpc.server.interceptors; | ||
|
||
import io.grpc.examples.helloworld.Greeter; | ||
import io.grpc.examples.helloworld.HelloReply; | ||
import io.grpc.examples.helloworld.HelloRequest; | ||
import io.quarkus.grpc.GrpcService; | ||
import io.smallrye.common.annotation.Blocking; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@GrpcService | ||
public class MyInterceptedGreeting implements Greeter { | ||
@Override | ||
@Blocking | ||
public Uni<HelloReply> sayHello(HelloRequest request) { | ||
return Uni.createFrom().item(() -> HelloReply.newBuilder() | ||
.setMessage("hello " + MyFirstInterceptor.KEY_1.get() + " - " + MyFirstInterceptor.KEY_2.get()).build()); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
extensions/grpc/runtime/src/main/java/io/grpc/override/ContextStorageOverride.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,67 @@ | ||
package io.grpc.override; | ||
|
||
import io.grpc.Context; | ||
import io.smallrye.common.vertx.VertxContext; | ||
import io.vertx.core.Vertx; | ||
|
||
/** | ||
* Override gRPC context storage to rely on duplicated context when available. | ||
*/ | ||
public class ContextStorageOverride extends Context.Storage { | ||
|
||
private static final ThreadLocal<Context> fallback = new ThreadLocal<>(); | ||
|
||
private static final String GRPC_CONTEXT = "GRPC_CONTEXT"; | ||
|
||
@Override | ||
public Context doAttach(Context toAttach) { | ||
Context current = current(); | ||
io.vertx.core.Context dc = Vertx.currentContext(); | ||
if (dc != null && VertxContext.isDuplicatedContext(dc)) { | ||
dc.putLocal(GRPC_CONTEXT, toAttach); | ||
} else { | ||
fallback.set(toAttach); | ||
} | ||
return current; | ||
} | ||
|
||
@Override | ||
public void detach(Context context, Context toRestore) { | ||
io.vertx.core.Context dc = Vertx.currentContext(); | ||
if (toRestore != Context.ROOT) { | ||
if (dc != null && VertxContext.isDuplicatedContext(dc)) { | ||
dc.putLocal(GRPC_CONTEXT, toRestore); | ||
} else { | ||
fallback.set(toRestore); | ||
} | ||
} else { | ||
if (dc != null && VertxContext.isDuplicatedContext(dc)) { | ||
// Do nothing duplicated context are not shared. | ||
} else { | ||
fallback.set(null); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Context current() { | ||
if (VertxContext.isOnDuplicatedContext()) { | ||
Context current = Vertx.currentContext().getLocal(GRPC_CONTEXT); | ||
if (current == null) { | ||
return Context.ROOT; | ||
} | ||
return current; | ||
} else { | ||
Context current = fallback.get(); | ||
if (current == null) { | ||
return Context.ROOT; | ||
} | ||
return current; | ||
} | ||
} | ||
|
||
@Override | ||
public void attach(Context toAttach) { | ||
// do nothing, should not be called. | ||
} | ||
} |
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
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.
wow that's nasty (in gRPC) , the
io.grpc.override.ContextStorageOverride
hardcoded class.