Skip to content

Commit ebde53c

Browse files
committed
Close #54
1 parent 67387c0 commit ebde53c

File tree

19 files changed

+153
-29
lines changed

19 files changed

+153
-29
lines changed

README.adoc

+6-6
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,20 @@ $ cd ./lambdarpc-core/build/dokka/html
166166
[sources,bash]
167167
----
168168
$ cd LambdaRPC.kt
169-
$ ./gradlew :examples:ml.dataservice
170-
$ ./gradlew :examples:ml.mlservice
171-
$ ./gradlew :examples:ml.client
169+
$ ./gradlew :examples:interactive-ml:dataservice
170+
$ ./gradlew :examples:interactive-ml:mlservice
171+
$ ./gradlew :examples:interactive-ml:client
172172
----
173173

174174
* `promise_pipeline` -- an interesting example that shows the possibility to build lazy data processing pipelines using common λRPC functionality.
175175

176176
[sources,bash]
177177
----
178178
$ cd LambdaRPC.kt
179-
$ ./gradlew :examples:promise.service --args=8090
180-
$ ./gradlew :examples:promise.service --args=8091
179+
$ ./gradlew :examples:promise-pipeline:service --args=8090
180+
$ ./gradlew :examples:promise-pipeline:service --args=8091
181181
# Any number of services on different ports
182-
$ ./gradlew :examples:promise.client --args='8090 8091' # Ports of all services
182+
$ ./gradlew :examples:promise-pipeline:client --args='8090 8091' # Ports of all services
183183
----
184184

185185
.lambdarpc-core

examples/build.gradle.kts examples/interactive-ml/build.gradle.kts

+3-17
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,21 @@ dependencies {
2222
implementation("io.github.microutils:kotlin-logging-jvm:2.1.21")
2323
}
2424

25-
val lazy = "io.lambdarpc.examples.promise_pipeline"
26-
27-
tasks.register<JavaExec>("promise.service") {
28-
dependsOn("classes")
29-
classpath = sourceSets["main"].runtimeClasspath
30-
mainClass.set("$lazy.service.ServiceKt")
31-
}
32-
33-
tasks.register<JavaExec>("promise.client") {
34-
dependsOn("classes")
35-
classpath = sourceSets["main"].runtimeClasspath
36-
mainClass.set("$lazy.client.ClientKt")
37-
}
38-
3925
val ml = "io.lambdarpc.examples.interactive_ml"
4026

41-
tasks.register<JavaExec>("ml.dataservice") {
27+
tasks.register<JavaExec>("dataservice") {
4228
dependsOn("classes")
4329
classpath = sourceSets["main"].runtimeClasspath
4430
mainClass.set("$ml.dataservice.ServiceKt")
4531
}
4632

47-
tasks.register<JavaExec>("ml.mlservice") {
33+
tasks.register<JavaExec>("mlservice") {
4834
dependsOn("classes")
4935
classpath = sourceSets["main"].runtimeClasspath
5036
mainClass.set("$ml.mlservice.ServiceKt")
5137
}
5238

53-
tasks.register<JavaExec>("ml.client") {
39+
tasks.register<JavaExec>("client") {
5440
dependsOn("classes")
5541
classpath = sourceSets["main"].runtimeClasspath
5642
mainClass.set("$ml.client.ClientKt")
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
3+
plugins {
4+
kotlin("jvm")
5+
kotlin("plugin.serialization") version "1.6.10"
6+
application
7+
}
8+
9+
tasks.withType<KotlinCompile>().all {
10+
kotlinOptions {
11+
freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
12+
jvmTarget = "11"
13+
}
14+
}
15+
16+
dependencies {
17+
implementation(kotlin("stdlib"))
18+
implementation(project(":lambdarpc-core"))
19+
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
20+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
21+
implementation("org.slf4j:slf4j-simple:1.7.36")
22+
implementation("io.github.microutils:kotlin-logging-jvm:2.1.21")
23+
}
24+
25+
val lazy = "io.lambdarpc.examples.promise_pipeline"
26+
27+
tasks.register<JavaExec>("service") {
28+
dependsOn("classes")
29+
classpath = sourceSets["main"].runtimeClasspath
30+
mainClass.set("$lazy.service.ServiceKt")
31+
}
32+
33+
tasks.register<JavaExec>("client") {
34+
dependsOn("classes")
35+
classpath = sourceSets["main"].runtimeClasspath
36+
mainClass.set("$lazy.client.ClientKt")
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@file:Suppress("SpellCheckingInspection")
2+
3+
package io.lambdarpc.examples.promise_pipeline
4+
5+
import io.lambdarpc.coding.Coder
6+
import io.lambdarpc.dsl.f
7+
import io.lambdarpc.dsl.j
8+
import kotlinx.coroutines.async
9+
import kotlinx.coroutines.coroutineScope
10+
11+
typealias Promise<R> = suspend () -> R
12+
13+
inline fun <reified R> p(rc: Coder<R> = j()): Coder<Promise<R>> = f(rc)
14+
15+
fun <R> lazify(f: suspend () -> R): suspend () -> Promise<R> = {
16+
{ f() }
17+
}
18+
19+
fun <A, R> lazify(
20+
f: suspend (A) -> R
21+
): suspend (Promise<A>) -> Promise<R> = { a ->
22+
{ f(a()) }
23+
}
24+
25+
fun <A, B, R> lazify(
26+
f: suspend (A, B) -> R
27+
): suspend (Promise<A>, Promise<B>) -> Promise<R> = { a, b ->
28+
{
29+
coroutineScope {
30+
val aa = async { a() }
31+
val bb = async { b() }
32+
f(aa.await(), bb.await())
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.lambdarpc.examples.promise_pipeline.client
2+
3+
import io.lambdarpc.dsl.ServiceDispatcher
4+
import io.lambdarpc.dsl.blockingConnectionPool
5+
import io.lambdarpc.examples.promise_pipeline.service.*
6+
import io.lambdarpc.utils.Endpoint
7+
8+
fun main(args: Array<String>) = blockingConnectionPool(
9+
ServiceDispatcher(serviceId to args.map {
10+
Endpoint("localhost", it.toInt())
11+
})
12+
) {
13+
val s = s()
14+
val a = a(s)
15+
val b = List(10) { b }.fold(a) { b, f -> f(b) }
16+
val c = c(s, 2)
17+
val d = d(c)
18+
val e = e(b, d)
19+
println("The answer is: ${e()}")
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.lambdarpc.examples.promise_pipeline.service
2+
3+
import io.lambdarpc.dsl.LibService
4+
import io.lambdarpc.dsl.def
5+
import io.lambdarpc.dsl.j
6+
import io.lambdarpc.examples.promise_pipeline.Promise
7+
import io.lambdarpc.examples.promise_pipeline.lazify
8+
import io.lambdarpc.examples.promise_pipeline.p
9+
import io.lambdarpc.utils.Endpoint
10+
import io.lambdarpc.utils.toSid
11+
12+
val serviceId = "d5ec2813-4468-4deb-b156-aeba87b91bd6".toSid()
13+
14+
val s by serviceId.def(p<Int>())
15+
val a by serviceId.def(p<Int>(), p<Int>())
16+
val b by serviceId.def(p<Int>(), p<Int>())
17+
val c by serviceId.def(p<Int>(), j<Int>(), p<Int>())
18+
val d by serviceId.def(p<Int>(), p<Int>())
19+
val e by serviceId.def(p<Int>(), p<Int>(), p<Int>())
20+
21+
private object Lib {
22+
fun s(): Int = 1
23+
fun a(x: Int): Int = x + 1
24+
fun b(x: Int): Int = x + 3
25+
suspend fun c(x: Promise<Int>, k: Int): Promise<Int> = { x() * k }
26+
fun d(x: Int): Int = x + 8
27+
fun e(x: Int, y: Int): Int = x + y
28+
}
29+
30+
fun main(args: Array<String>) {
31+
val (port) = args
32+
val service = LibService(serviceId, Endpoint("localhost", port.toInt())) {
33+
s of lazify(Lib::s)
34+
a of lazify(Lib::a)
35+
b of lazify(Lib::b)
36+
c of Lib::c
37+
d of lazify(Lib::d)
38+
e of lazify(Lib::e)
39+
}
40+
service.start()
41+
service.awaitTermination()
42+
}

lambdarpc-core/build.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ dependencies {
2727
api("com.google.protobuf:protobuf-java-util:3.19.4")
2828
api("com.google.protobuf:protobuf-kotlin:3.19.4")
2929
api("io.grpc:grpc-kotlin-stub:1.2.1")
30-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
31-
implementation("org.slf4j:slf4j-simple:1.7.36")
32-
implementation("io.github.microutils:kotlin-logging-jvm:2.1.21")
30+
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
31+
api("org.slf4j:slf4j-simple:1.7.36")
32+
api("io.github.microutils:kotlin-logging-jvm:2.1.21")
3333
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.19.0")
3434

3535
testImplementation(kotlin("test"))

settings.gradle.kts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
rootProject.name = "lambdarpc-kt"
2-
include("lambdarpc-core")
3-
include("lambdarpc-coders")
4-
include("examples")
2+
include(
3+
":lambdarpc-core",
4+
":lambdarpc-coders",
5+
":examples:interactive-ml",
6+
":examples:promise-pipeline",
7+
":benchmarks:grpc"
8+
)

0 commit comments

Comments
 (0)