Skip to content

Commit 981c907

Browse files
authored
Curl engine impl (#2)
Curl engine shows better performance over CIO
1 parent fb23916 commit 981c907

File tree

8 files changed

+33
-18
lines changed

8 files changed

+33
-18
lines changed

.github/workflows/buildLinuxArm64.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
name: Build Linux64
1+
name: Build Linux64 curl-engine-branch
22
on:
33
workflow_dispatch:
44

55
jobs:
66
build:
7-
runs-on: ubuntu-22.04
7+
runs-on: ubuntu-22
88
steps:
99
- name: Checkout code
1010
uses: actions/checkout@v3
@@ -15,13 +15,15 @@ jobs:
1515
java-version: 17
1616

1717
- name: Install libcurl
18-
run: apt-get install libcurl4-gnutls-dev
18+
run: |
19+
sudo apt-get update
20+
sudo apt-get install libcurl3-dev
1921
2022
- name: Run build
2123
run: ./gradlew build
2224

2325
- name: Upload artifacts
24-
uses: actions/upload-artifact@v3
26+
uses: actions/upload-artifact@v4
2527
with:
26-
name: kotlin-native-arm-build
27-
path: sample/build/bin/linuxArm64/releaseExecutable
28+
name: kotlin-native-build
29+
path: sample/build/bin

gradle.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ kotlin.code.style=official
44
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M"
55

66
#Ktor
7-
io.ktor.development=true
7+
io.ktor.development=true
8+
9+
#kotlin.native.cacheKind.linuxX64=none

lambda-runtime/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ kotlin {
1313
implementation(libs.kotlin.serialization.json)
1414
implementation(libs.kotlin.io.core)
1515
implementation(libs.kotlin.date.time)
16-
implementation(libs.ktor.client.cio)
16+
//implementation(libs.ktor.client.cio)
17+
implementation(libs.ktor.client.curl)
1718
implementation(libs.ktor.client.logging)
1819
implementation(libs.ktor.content.negotiation)
1920
implementation(libs.ktor.content.json)

lambda-runtime/src/commonMain/kotlin/io/github/trueangle/knative/lambda/runtime/LambdaRuntime.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import io.github.trueangle.knative.lambda.runtime.handler.LambdaStreamHandler
99
import io.github.trueangle.knative.lambda.runtime.log.Log
1010
import io.github.trueangle.knative.lambda.runtime.log.LogLevel
1111
import io.ktor.client.HttpClient
12-
import io.ktor.client.engine.cio.CIO
12+
import io.ktor.client.engine.curl.Curl
1313
import io.ktor.client.plugins.HttpTimeout
1414
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
15-
import io.ktor.client.plugins.logging.LogLevel as KtorLogLevel
1615
import io.ktor.client.plugins.logging.Logger
1716
import io.ktor.client.plugins.logging.Logging
1817
import io.ktor.http.content.OutgoingContent.WriteChannelContent
@@ -24,9 +23,11 @@ import io.ktor.utils.io.writeStringUtf8
2423
import kotlinx.coroutines.runBlocking
2524
import kotlinx.serialization.json.Json
2625
import kotlin.system.exitProcess
26+
import kotlin.time.TimeSource
27+
import io.ktor.client.plugins.logging.LogLevel as KtorLogLevel
2728

2829
object LambdaRuntime {
29-
private val httpClient = HttpClient(CIO) {
30+
private val httpClient = HttpClient(Curl) {
3031
install(HttpTimeout)
3132
install(ContentNegotiation) {
3233
json(Json { explicitNulls = false })
@@ -72,6 +73,7 @@ object LambdaRuntime {
7273
} else {
7374
handler as LambdaBufferedHandler<I, O>
7475
val response = bufferedResponse(context) { handler.handleRequest(event, context) }
76+
7577
client.sendResponse(context, response, outputTypeInfo)
7678
}
7779
} catch (e: LambdaRuntimeException) {

lambda-runtime/src/commonMain/kotlin/io/github/trueangle/knative/lambda/runtime/api/LambdaRuntimeClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import io.ktor.http.contentType
2424
import io.ktor.util.reflect.TypeInfo
2525
import kotlinx.serialization.json.Json
2626
import kotlin.time.Duration.Companion.minutes
27+
import kotlin.time.TimeSource
2728
import io.ktor.http.ContentType.Application.Json as ContentTypeJson
2829

2930
@PublishedApi
@@ -37,7 +38,6 @@ internal class LambdaClient(private val httpClient: HttpClient) {
3738
requestTimeoutMillis = 60 * 1000 * 30 // todo
3839
}
3940
}
40-
4141
val context = contextFromResponseHeaders(response)
4242
val body = try {
4343
response.body(bodyType) as T
@@ -49,7 +49,7 @@ internal class LambdaClient(private val httpClient: HttpClient) {
4949
}
5050

5151
suspend fun <T> sendResponse(event: Context, body: T, bodyType: TypeInfo): HttpResponse {
52-
Log.trace("sendResponse from handler: $body")
52+
Log.trace("Response from handler: $body")
5353

5454
val response = httpClient.post {
5555
url("${invokeUrl}/invocation/${event.awsRequestId}/response")

sample/build.gradle.kts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ kotlin {
88
val isArm64 = System.getProperty("os.arch") == "aarch64"
99
val nativeTarget = if (isArm64) linuxArm64() else linuxX64()
1010

11-
nativeTarget.binaries {
12-
executable {
13-
entryPoint = "com.github.trueangle.knative.lambda.runtime.sample.main"
11+
nativeTarget.apply {
12+
binaries {
13+
executable {
14+
entryPoint = "com.github.trueangle.knative.lambda.runtime.sample.main"
15+
}
1416
}
1517
}
1618

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package = libcurl
2+
headers = curl/curl.h
3+
headerFilter = curl/*
4+
5+
#compilerOpts.linux = -I/opt/homebrew/Cellar/curl/8.9.1/include
6+
#linkerOpts.linux = -L/opt/homebrew/Cellar/curl/8.9.1/lib -lcurl
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.trueangle.knative.lambda.runtime.sample
22

3-
import com.github.trueangle.knative.lambda.runtime.sample.handler.ObjectBodyLambdaHandler
3+
import com.github.trueangle.knative.lambda.runtime.sample.handler.StringBodyLambdaHandler
44
import io.github.trueangle.knative.lambda.runtime.LambdaRuntime
55

6-
fun main() = LambdaRuntime.run { ObjectBodyLambdaHandler() }
6+
fun main() = LambdaRuntime.run { StringBodyLambdaHandler() }

0 commit comments

Comments
 (0)