-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The problem comes from the default gRPC context storage using a thread-local. This commit overrides the storage implementation (using the recommended method) to use the duplicated context and fallback to a thread-local.
- Loading branch information
1 parent
cd4f781
commit b82b235
Showing
6 changed files
with
148 additions
and
13 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
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