-
Notifications
You must be signed in to change notification settings - Fork 177
Add support for accessing gRPC Metadata in RPC implementation code. #729
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
Open
zhumazhenis
wants to merge
10
commits into
grpc:master
Choose a base branch
from
zhumazhenis:zhumazhenis/grpc-metadata-coroutine-ctx
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3c4b25a
Add interceptor for propagating gRPC Metadata to coroutineContext.
zhumazhenis 02ea177
Refactor.
zhumazhenis 624ece8
Refactor.
zhumazhenis 504b3a7
Refactor returning exception.
zhumazhenis 6263fd8
Format.
zhumazhenis 9b00c32
Use kt-proto.
zhumazhenis d2f4a00
Refactor test.
zhumazhenis 8c8f215
Refactor doc.
zhumazhenis ab37852
Resolve comments.
zhumazhenis d59230c
Format.
zhumazhenis 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
Some comments aren't visible on the classic Files Changed page.
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
34 changes: 34 additions & 0 deletions
34
stub/src/main/java/io/grpc/kotlin/MetadataCoroutineContextInterceptor.kt
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,34 @@ | ||
| package io.grpc.kotlin | ||
|
|
||
| import io.grpc.Metadata | ||
| import io.grpc.ServerCall | ||
| import kotlin.coroutines.CoroutineContext | ||
|
|
||
| /** | ||
| * Propagates gRPC Metadata (HTTP Headers) to coroutineContext. | ||
zhumazhenis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * Attach the interceptor to gRPC Server and then access the Metadata using grpcMetadata() function. | ||
| * | ||
| * Example usage: | ||
| * | ||
| * ServerBuilder.forPort(8060) | ||
| * .addService(GreeterImpl()) | ||
| * .intercept(MetadataCoroutineContextInterceptor()) | ||
| * | ||
| * grpcMetadata() | ||
| */ | ||
| class MetadataCoroutineContextInterceptor : CoroutineContextServerInterceptor() { | ||
| final override fun coroutineContext(call: ServerCall<*, *>, headers: Metadata): CoroutineContext { | ||
| return MetadataElement(value = headers) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Used for accessing the gRPC Metadata from coroutineContext. | ||
| * Example usage: | ||
| * coroutineContext[MetadataElement]?.value | ||
| */ | ||
| internal data class MetadataElement(val value: Metadata) : CoroutineContext.Element { | ||
zhumazhenis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| companion object Key : CoroutineContext.Key<MetadataElement> | ||
|
|
||
| override val key: CoroutineContext.Key<MetadataElement> get() = Key | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
stub/src/test/java/io/grpc/kotlin/MetadataCoroutineContextInterceptorTest.kt
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,81 @@ | ||
| package io.grpc.kotlin | ||
|
|
||
| import io.grpc.BindableService | ||
| import io.grpc.Channel | ||
| import io.grpc.Metadata | ||
| import io.grpc.Status | ||
| import io.grpc.StatusException | ||
| import io.grpc.examples.helloworld.GreeterGrpcKt | ||
| import io.grpc.examples.helloworld.HelloReply | ||
| import io.grpc.examples.helloworld.HelloRequest | ||
| import io.grpc.inprocess.InProcessChannelBuilder | ||
| import io.grpc.inprocess.InProcessServerBuilder | ||
| import io.grpc.testing.GrpcCleanupRule | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.jupiter.api.Assertions | ||
| import org.junit.runner.RunWith | ||
| import org.junit.runners.JUnit4 | ||
|
|
||
| @RunWith(JUnit4::class) | ||
| class MetadataCoroutineContextInterceptorTest { | ||
| @Rule | ||
| @JvmField | ||
| val grpcCleanup = GrpcCleanupRule() | ||
|
|
||
| @Test | ||
| fun `interceptor provides gRPC Metadata to coroutineContext`() { | ||
| val key = Metadata.Key.of("test-header", Metadata.ASCII_STRING_MARSHALLER) | ||
| val clientStub = | ||
| GreeterGrpcKt.GreeterCoroutineStub(testChannel(object : GreeterGrpcKt.GreeterCoroutineImplBase() { | ||
| override suspend fun sayHello(request: HelloRequest): HelloReply { | ||
| val metadata = grpcMetadata() | ||
| return HelloReply.newBuilder() | ||
zhumazhenis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .setMessage(metadata.get(key).toString()) | ||
| .build() | ||
| } | ||
| })) | ||
| val metadata = Metadata() | ||
| metadata.put(key, "Test message") | ||
zhumazhenis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| val response = runBlocking { clientStub.sayHello(HelloRequest.getDefaultInstance(), metadata) } | ||
|
|
||
| Assertions.assertEquals("Test message", response.message) | ||
| } | ||
|
|
||
| @Test | ||
| fun `fails to extract gRPC Metadata if interceptor is not injected`() { | ||
| val key = Metadata.Key.of("test-header", Metadata.ASCII_STRING_MARSHALLER) | ||
| val clientStub = | ||
| GreeterGrpcKt.GreeterCoroutineStub(testChannel(object : GreeterGrpcKt.GreeterCoroutineImplBase() { | ||
| override suspend fun sayHello(request: HelloRequest): HelloReply { | ||
| val metadata = grpcMetadata() | ||
| return HelloReply.newBuilder() | ||
zhumazhenis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .setMessage(metadata.get(key).toString()) | ||
| .build() | ||
| } | ||
| }, false)) | ||
| val metadata = Metadata() | ||
| metadata.put(key, "Test message") | ||
|
|
||
| val exception = Assertions.assertThrows(StatusException::class.java) { | ||
zhumazhenis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| runBlocking { clientStub.sayHello(HelloRequest.getDefaultInstance(), metadata) } | ||
zhumazhenis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Assertions.assertEquals(Status.INTERNAL.code, exception.status.code) | ||
zhumazhenis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Assertions.assertEquals( | ||
| "gRPC Metadata not found in coroutineContext. Ensure that MetadataCoroutineContextInterceptor is used in gRPC server.", | ||
| exception.status.description | ||
| ) | ||
| } | ||
|
|
||
| private fun testChannel(service: BindableService, attachInterceptor: Boolean = true): Channel { | ||
| val serverName = InProcessServerBuilder.generateName() | ||
| var builder = InProcessServerBuilder.forName(serverName).directExecutor() | ||
| if (attachInterceptor) { | ||
| builder = builder.intercept(MetadataCoroutineContextInterceptor()) | ||
| } | ||
| grpcCleanup.register(builder.addService(service).build().start()) | ||
| return grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()) | ||
| } | ||
| } | ||
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.