generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: propagate FTL headers through kotlin-runtime
There's some weirdness at play, in that the client uses okhttp3, and the server uses grpc.io. This appears to be how Wire works, but there's basically zero documentation so I can't be sure.
- Loading branch information
1 parent
e264cbc
commit e7f89d5
Showing
5 changed files
with
59 additions
and
8 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
35 changes: 35 additions & 0 deletions
35
kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/server/ServerInterceptor.kt
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,35 @@ | ||
package xyz.block.ftl.server | ||
|
||
import io.grpc.* | ||
import io.grpc.ServerInterceptor | ||
|
||
const val ftlVerbHeader = "FTL-Verb" | ||
const val ftlRequestIdHeader = "FTL-Request-ID" | ||
|
||
internal class ServerInterceptor : ServerInterceptor { | ||
|
||
companion object { | ||
internal var callersMetadata = Metadata.Key.of(ftlVerbHeader, Metadata.ASCII_STRING_MARSHALLER) | ||
internal var requestIdMetadata = Metadata.Key.of(ftlRequestIdHeader, Metadata.ASCII_STRING_MARSHALLER) | ||
|
||
internal var callers = Context.key<List<String>>(ftlVerbHeader) | ||
internal var requestId = Context.key<String>(ftlRequestIdHeader) | ||
} | ||
|
||
override fun <ReqT : Any?, RespT : Any?> interceptCall( | ||
call: ServerCall<ReqT, RespT>?, | ||
headers: Metadata?, | ||
next: ServerCallHandler<ReqT, RespT>? | ||
): ServerCall.Listener<ReqT> { | ||
var context = Context.current() | ||
|
||
headers?.getAll(callersMetadata)?.apply { | ||
context = context.withValue(callers, this.toList()) | ||
} | ||
headers?.get(requestIdMetadata)?.apply { | ||
context = context.withValue(requestId, this) | ||
} | ||
|
||
return Contexts.interceptCall(context, call, headers, next) | ||
} | ||
} |