generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
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 (#338)
As part of this, switched to using plain gRPC for client calls (rather than okhttp). Also switched to using bit for builds in scripts. Bit has much better dependency specificiation than make so, for example, when repeatedly running integration tests, it won't constantly rebuild the JARs. ![echo-trace](https://github.com/TBD54566975/ftl/assets/41767/0dce3832-ba6f-434f-ba9f-9eb23a0dd8e0)
- Loading branch information
1 parent
ddb2c6e
commit 13914eb
Showing
10 changed files
with
113 additions
and
57 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
51 changes: 36 additions & 15 deletions
51
kotlin-runtime/ftl-runtime/src/main/kotlin/xyz/block/ftl/client/grpc.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 |
---|---|---|
@@ -1,20 +1,41 @@ | ||
package xyz.block.ftl.client | ||
|
||
import com.squareup.wire.GrpcClient | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Protocol | ||
import java.time.Duration | ||
import io.grpc.* | ||
import io.grpc.netty.NettyChannelBuilder | ||
import io.grpc.netty.NettyServerBuilder | ||
import xyz.block.ftl.logging.Logging | ||
import xyz.block.ftl.server.ServerInterceptor | ||
import java.net.InetSocketAddress | ||
import java.net.URL | ||
import java.util.concurrent.TimeUnit.SECONDS | ||
|
||
internal fun makeGrpcClient(endpoint: String): GrpcClient { | ||
return GrpcClient.Builder() | ||
.client( | ||
OkHttpClient.Builder() | ||
.readTimeout(Duration.ofSeconds(10)) | ||
.writeTimeout(Duration.ofSeconds(10)) | ||
.callTimeout(Duration.ofSeconds(10)) | ||
.protocols(listOf(Protocol.H2_PRIOR_KNOWLEDGE)) | ||
.build() | ||
) | ||
.baseUrl(endpoint) | ||
internal fun makeGrpcClient(endpoint: String): ManagedChannel { | ||
val url = URL(endpoint) | ||
// TODO: Check if URL is https and use SSL? | ||
return NettyChannelBuilder | ||
.forAddress(InetSocketAddress(url.host, url.port)) | ||
.keepAliveTime(5, SECONDS) | ||
.intercept(VerbServiceClientInterceptor()) | ||
.usePlaintext() | ||
.build() | ||
} | ||
|
||
private class VerbServiceClientInterceptor : ClientInterceptor { | ||
override fun <ReqT : Any?, RespT : Any?> interceptCall( | ||
method: MethodDescriptor<ReqT, RespT>?, | ||
callOptions: CallOptions?, | ||
next: Channel? | ||
): ClientCall<ReqT, RespT> { | ||
val call = next?.newCall(method, callOptions) | ||
return object : ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(call) { | ||
override fun start(responseListener: Listener<RespT>?, headers: Metadata?) { | ||
ServerInterceptor.callers.get().forEach { caller -> | ||
headers?.put(ServerInterceptor.callersMetadata, caller) | ||
} | ||
headers?.put(ServerInterceptor.requestIdMetadata, ServerInterceptor.requestId.get()) | ||
super.start(responseListener, headers) | ||
} | ||
} | ||
} | ||
|
||
} |
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) | ||
} | ||
} |
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