Skip to content

Commit e1ae30d

Browse files
committed
Refactor #57 #56 #53 #46
1 parent 23c9c9d commit e1ae30d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+243
-195
lines changed

README.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fun main() = runBlocking(serviceDispatcher) {
6969
// Keep track of the loss function values
7070
val history = mutableListOf<Metric>()
7171
var lastEpoch = 0
72-
val rawModel = Model(...)
72+
val rawModel = Model(layers=1050)
7373
// Bind dataloader with dataEndpoint, so mlservice will communicate directly
7474
// with the dataservice on the dataEndpoint without client in the middle
7575
val boundLoader = dataloader.toBound()

benchmarks/grpc/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88

99
tasks.withType<KotlinCompile>().all {
1010
kotlinOptions {
11-
freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
11+
freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn")
1212
jvmTarget = "11"
1313
}
1414
}

benchmarks/grpc/src/main/kotlin/io/lambdarpc/benchmarks/grpc/Main.kt

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.lambdarpc.benchmarks.grpc
22

3+
import io.lambdarpc.context.ServiceDispatcher
4+
import io.lambdarpc.context.blockingConnectionPool
35
import io.lambdarpc.dsl.*
46
import io.lambdarpc.utils.Endpoint
57
import io.lambdarpc.utils.ServiceId

build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
kotlin("jvm") version "1.6.10" apply false
2+
kotlin("jvm") version "1.6.21" apply false
33
}
44

55
allprojects {
@@ -9,5 +9,5 @@ allprojects {
99
}
1010

1111
group = "io.lambdarpc"
12-
version = "0.0.1"
12+
version = "0.0.1-SNAPSHOT"
1313
}

examples/interactive-ml/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88

99
tasks.withType<KotlinCompile>().all {
1010
kotlinOptions {
11-
freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
11+
freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn")
1212
jvmTarget = "11"
1313
}
1414
}

examples/interactive-ml/src/main/kotlin/io/lambdarpc/examples/interactive_ml/client/Client.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.lambdarpc.examples.interactive_ml.client
22

3-
import io.lambdarpc.dsl.ServiceDispatcher
4-
import io.lambdarpc.dsl.blockingConnectionPool
3+
import io.lambdarpc.context.ServiceDispatcher
4+
import io.lambdarpc.context.blockingConnectionPool
55
import io.lambdarpc.dsl.toBound
66
import io.lambdarpc.examples.interactive_ml.dataEndpoint
77
import io.lambdarpc.examples.interactive_ml.dataservice.facade.dataLoader

examples/interactive-ml/src/main/kotlin/io/lambdarpc/examples/interactive_ml/mlservice/Facade.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ val fit by mlServiceId.def( // Define declaration for suspend (Epoch, Metric) ->
2929
)
3030

3131
private object ModelCoder : Coder<Model> {
32-
override fun encode(value: Model, context: CodingContext): Entity =
32+
override suspend fun encode(value: Model, context: CodingContext): Entity =
3333
Entity(RawData.copyFrom(byteArrayOf(value.weight.toByte())))
3434

35-
override fun decode(entity: Entity, context: CodingContext): Model =
35+
override suspend fun decode(entity: Entity, context: CodingContext): Model =
3636
Model(entity.data.toByteArray().first().toInt())
3737
}

examples/promise-pipeline/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88

99
tasks.withType<KotlinCompile>().all {
1010
kotlinOptions {
11-
freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
11+
freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn")
1212
jvmTarget = "11"
1313
}
1414
}

examples/promise-pipeline/src/main/kotlin/io/lambdarpc/examples/promise_pipeline/client/Client.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.lambdarpc.examples.promise_pipeline.client
22

3-
import io.lambdarpc.dsl.ServiceDispatcher
4-
import io.lambdarpc.dsl.blockingConnectionPool
3+
import io.lambdarpc.context.ServiceDispatcher
4+
import io.lambdarpc.context.blockingConnectionPool
55
import io.lambdarpc.examples.promise_pipeline.service.*
66
import io.lambdarpc.utils.Endpoint
77

lambdarpc-coders/build.gradle.kts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ plugins {
77

88
tasks.withType<KotlinCompile>().all {
99
kotlinOptions {
10-
freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
10+
freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn")
1111
jvmTarget = "11"
1212
}
1313
}
1414

1515
dependencies {
1616
implementation(kotlin("stdlib"))
1717
implementation(project(":lambdarpc-core"))
18-
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
19-
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
18+
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1")
19+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3")
2020
}

lambdarpc-coders/src/main/kotlin/io/lambdarpc/coding/coders/JsonFunctionsListCoder.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import io.lambdarpc.transport.grpc.Entity
66
import io.lambdarpc.transport.grpc.FunctionPrototype
77

88
class JsonFunctionsListEncoder<F>(private val encoder: FunctionEncoder<F>) : Encoder<List<F>> {
9-
override fun encode(value: List<F>, context: CodingContext): Entity {
9+
override suspend fun encode(value: List<F>, context: CodingContext): Entity {
1010
val prototypes = value.map { encoder.encode(it, context).toByteArray() }
1111
return j<List<ByteArray>>().encode(prototypes, context)
1212
}
1313
}
1414

1515
class JsonFunctionsListDecoder<F>(private val decoder: FunctionDecoder<F>) : Decoder<List<F>> {
16-
override fun decode(entity: Entity, context: CodingContext): List<F> {
16+
override suspend fun decode(entity: Entity, context: CodingContext): List<F> {
1717
val prototypes = j<List<ByteArray>>().decode(entity, context).map {
1818
FunctionPrototype.parseFrom(it)
1919
}
@@ -28,9 +28,9 @@ class JsonFunctionsListCoder<F>(
2828
private val listEncoder by lazy { JsonFunctionsListEncoder(encoder) }
2929
private val listDecoder by lazy { JsonFunctionsListDecoder(decoder) }
3030

31-
override fun encode(value: List<F>, context: CodingContext): Entity =
31+
override suspend fun encode(value: List<F>, context: CodingContext): Entity =
3232
listEncoder.encode(value, context)
3333

34-
override fun decode(entity: Entity, context: CodingContext): List<F> =
34+
override suspend fun decode(entity: Entity, context: CodingContext): List<F> =
3535
listDecoder.decode(entity, context)
3636
}

lambdarpc-core/build.gradle.kts

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ plugins {
1414

1515
tasks.withType<KotlinCompile>().all {
1616
kotlinOptions {
17-
freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
17+
freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn", "-Xexplicit-api=warning")
1818
jvmTarget = "11"
1919
}
2020
}
2121

2222
dependencies {
23-
implementation(kotlin("stdlib"))
24-
runtimeOnly("io.grpc:grpc-netty-shaded:1.44.0")
25-
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
26-
api("io.grpc:grpc-protobuf:1.44.0")
27-
api("com.google.protobuf:protobuf-java-util:3.19.4")
28-
api("com.google.protobuf:protobuf-kotlin:3.19.4")
23+
runtimeOnly("io.grpc:grpc-netty-shaded:1.46.0")
24+
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1")
25+
api("io.grpc:grpc-protobuf:1.46.0")
26+
api("com.google.protobuf:protobuf-java-util:3.20.1")
27+
api("com.google.protobuf:protobuf-kotlin:3.20.1")
2928
api("io.grpc:grpc-kotlin-stub:1.2.1")
30-
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
29+
api("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3")
30+
api("org.slf4j:slf4j-api:1.7.36")
3131
api("org.slf4j:slf4j-simple:1.7.36")
3232
api("io.github.microutils:kotlin-logging-jvm:2.1.21")
33-
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.19.0")
33+
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.20.0")
3434

3535
testImplementation(kotlin("test"))
3636
testImplementation(platform("org.junit:junit-bom:5.8.2"))

lambdarpc-core/config/detekt.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ complexity:
44
excludes: [ '**/functions/frontend/**' ]
55
TooManyFunctions:
66
excludes:
7-
- '**/Utils.kt'
7+
- '**/utils.kt'
88
- '**/test**'
99

1010
exceptions:
@@ -21,7 +21,7 @@ formatting:
2121
excludes: [ '**/exceptions/Exceptions.kt' ]
2222
MaximumLineLength:
2323
excludes:
24-
- '**/ToBound.kt'
24+
- '**/toBoundConverters.kt'
2525
ParameterListWrapping:
2626
active: false
2727

@@ -36,7 +36,7 @@ style:
3636
- '**/test/**'
3737
MaxLineLength:
3838
excludes:
39-
- '**/ToBound.kt'
39+
- '**/toBoundConverters.kt'
4040

4141
naming:
4242
# Does not always agree with IDEA default warnings
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.lambdarpc
2+
3+
@DslMarker
4+
public annotation class LambdaRPCBuilder
5+
6+
@RequiresOptIn(level = RequiresOptIn.Level.WARNING)
7+
@Retention(AnnotationRetention.BINARY)
8+
public annotation class LambdaRPCExperimentalAPI

lambdarpc-core/src/main/kotlin/io/lambdarpc/coding/Coder.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import io.lambdarpc.transport.grpc.Entity
55
/**
66
* Encodes data and functions.
77
*/
8-
interface Encoder<T> {
9-
fun encode(value: T, context: CodingContext): Entity
8+
public interface Encoder<in T> {
9+
public suspend fun encode(value: T, context: CodingContext): Entity
1010
}
1111

1212
/**
1313
* Decodes data and functions.
1414
*/
15-
interface Decoder<T> {
16-
fun decode(entity: Entity, context: CodingContext): T
15+
public interface Decoder<out T> {
16+
public suspend fun decode(entity: Entity, context: CodingContext): T
1717
}
1818

1919
/**
2020
* Encodes and decodes data and functions.
2121
*/
22-
interface Coder<T> : Encoder<T>, Decoder<T>
22+
public interface Coder<T> : Encoder<T>, Decoder<T>

lambdarpc-core/src/main/kotlin/io/lambdarpc/coding/CodingContext.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import io.lambdarpc.transport.grpc.Entity
66
/**
77
* Contains all needed information and state for encoding and decoding.
88
*/
9-
class CodingContext internal constructor(
9+
public class CodingContext internal constructor(
1010
internal val functionContext: FunctionCodingContext
1111
)
1212

1313
/**
1414
* Scope in which encoding and decoding of data and functions looks same.
1515
*/
16-
class CodingScope(val context: CodingContext) {
17-
fun <T> Encoder<T>.encode(value: T): Entity = encode(value, context)
18-
fun <T> Decoder<T>.decode(entity: Entity): T = decode(entity, context)
16+
public class CodingScope(public val context: CodingContext) {
17+
public suspend fun <T> Encoder<T>.encode(value: T): Entity = encode(value, context)
18+
public suspend fun <T> Decoder<T>.decode(entity: Entity): T = decode(entity, context)
1919
}
2020

21-
inline fun <R> withContext(context: CodingContext, block: CodingScope.() -> R): R =
21+
public inline fun <R> withContext(context: CodingContext, block: CodingScope.() -> R): R =
2222
CodingScope(context).block()

lambdarpc-core/src/main/kotlin/io/lambdarpc/coding/FunctionCoder.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import io.lambdarpc.transport.grpc.FunctionPrototype
66
/**
77
* Encodes functions.
88
*/
9-
interface FunctionEncoder<F> {
9+
public interface FunctionEncoder<in F> {
1010
/**
1111
* Creates the prototype of the function that can be serialized.
1212
*/
13-
fun encode(function: F, context: CodingContext): FunctionPrototype
13+
public fun encode(function: F, context: CodingContext): FunctionPrototype
1414
}
1515

1616
/**
1717
* Decodes functions.
1818
*/
19-
interface FunctionDecoder<F> {
19+
public interface FunctionDecoder<out F> {
2020
/**
2121
* Creates a callable proxy object [FrontendFunction] with interface [F]
2222
* that communicates with the backend part.
2323
*/
24-
fun decode(prototype: FunctionPrototype, context: CodingContext): F
24+
public fun decode(prototype: FunctionPrototype, context: CodingContext): F
2525
}
2626

27-
interface FunctionCoder<F> : FunctionEncoder<F>, FunctionDecoder<F>
27+
public interface FunctionCoder<F> : FunctionEncoder<F>, FunctionDecoder<F>

lambdarpc-core/src/main/kotlin/io/lambdarpc/coding/FunctionCoderAdapter.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package io.lambdarpc.coding
33
import io.lambdarpc.transport.grpc.Entity
44
import io.lambdarpc.transport.serialization.Entity
55

6-
class FunctionCoderAdapter<F>(private val coder: FunctionCoder<F>) : Coder<F> {
7-
override fun encode(value: F, context: CodingContext): Entity = Entity(coder.encode(value, context))
6+
public class FunctionCoderAdapter<F>(private val coder: FunctionCoder<F>) : Coder<F> {
7+
override suspend fun encode(value: F, context: CodingContext): Entity =
8+
Entity(coder.encode(value, context))
89

9-
override fun decode(entity: Entity, context: CodingContext): F {
10+
override suspend fun decode(entity: Entity, context: CodingContext): F {
1011
require(entity.hasFunction()) { "Entity should contain function" }
1112
return coder.decode(entity.function, context)
1213
}

lambdarpc-core/src/main/kotlin/io/lambdarpc/coding/coders/JsonCoder.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ import java.nio.charset.Charset
1313
/**
1414
* [JsonCoder] uses `kotlinx.serialization` to serialize data to JSON.
1515
*/
16-
class JsonCoder<T>(private val serializer: KSerializer<T>) : Coder<T> {
17-
override fun encode(value: T, context: CodingContext): Entity {
16+
public class JsonCoder<T>(private val serializer: KSerializer<T>) : Coder<T> {
17+
override suspend fun encode(value: T, context: CodingContext): Entity {
1818
val string = Json.encodeToString(serializer, value)
1919
return Entity(RawData.copyFrom(string, Charset.defaultCharset()))
2020
}
2121

22-
override fun decode(entity: Entity, context: CodingContext): T {
22+
override suspend fun decode(entity: Entity, context: CodingContext): T {
2323
require(entity.hasData()) { "Entity should contain data" }
2424
val string = entity.data.toString(Charset.defaultCharset())
2525
return Json.decodeFromString(serializer, string)
2626
}
2727
}
2828

29-
inline fun <reified T> JsonCoder() = JsonCoder<T>(serializer())
29+
@Suppress("FunctionName")
30+
public inline fun <reified T> JsonCoder(): JsonCoder<T> = JsonCoder(serializer())

lambdarpc-core/src/main/kotlin/io/lambdarpc/dsl/ConnectionPool.kt lambdarpc-core/src/main/kotlin/io/lambdarpc/context/ConnectionPool.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.lambdarpc.dsl
1+
package io.lambdarpc.context
22

33
import io.lambdarpc.transport.ConnectionProvider
44
import io.lambdarpc.transport.MultipleUseConnectionProvider
@@ -15,8 +15,8 @@ import kotlin.coroutines.EmptyCoroutineContext
1515
/**
1616
* Holds a pool of opened connections.
1717
*/
18-
class ConnectionPool internal constructor() : AbstractCoroutineContextElement(Key), Closeable {
19-
companion object Key : CoroutineContext.Key<ConnectionPool>
18+
public class ConnectionPool : AbstractCoroutineContextElement(Key), Closeable {
19+
public companion object Key : CoroutineContext.Key<ConnectionPool>
2020

2121
private val _pool = MultipleUseConnectionProvider<Endpoint> { SimpleGrpcConnection(it, usePlainText = true) }
2222
internal val endpointConnectionProvider: ConnectionProvider<Endpoint>
@@ -27,14 +27,14 @@ class ConnectionPool internal constructor() : AbstractCoroutineContextElement(Ke
2727
}
2828
}
2929

30-
suspend fun <R> useConnectionPool(
30+
public suspend fun <R> useConnectionPool(
3131
context: CoroutineContext = EmptyCoroutineContext,
3232
block: suspend CoroutineScope.() -> R
3333
): R = ConnectionPool().use {
3434
withContext(context + it) { block() }
3535
}
3636

37-
fun <R> blockingConnectionPool(
37+
public fun <R> blockingConnectionPool(
3838
context: CoroutineContext = EmptyCoroutineContext,
3939
block: suspend CoroutineScope.() -> R
4040
): R = runBlocking {

0 commit comments

Comments
 (0)