Skip to content

Commit ce3426b

Browse files
committed
introduce new module rsocket-internal-io which is a support module for core and transport implementations
This allows to drop using internal declarations, as well as not exposing it to library consumers, until explicitly requested
1 parent e5e292b commit ce3426b

File tree

25 files changed

+151
-74
lines changed

25 files changed

+151
-74
lines changed

rsocket-core/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ kotlin {
2424
sourceSets {
2525
commonMain {
2626
dependencies {
27+
implementation(projects.rsocketInternalIo)
28+
2729
api(libs.kotlinx.coroutines.core)
2830
api(libs.ktor.io)
2931
}

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/frame/io/payload.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,16 +19,17 @@ package io.rsocket.kotlin.frame.io
1919
import io.ktor.utils.io.core.*
2020
import io.ktor.utils.io.core.internal.*
2121
import io.ktor.utils.io.pool.*
22+
import io.rsocket.kotlin.internal.io.*
2223
import io.rsocket.kotlin.payload.*
2324

2425
internal fun ByteReadPacket.readMetadata(pool: ObjectPool<ChunkBuffer>): ByteReadPacket {
25-
val length = readLength()
26+
val length = readInt24()
2627
return readPacket(pool, length)
2728
}
2829

2930
internal fun BytePacketBuilder.writeMetadata(metadata: ByteReadPacket?) {
3031
metadata?.let {
31-
writeLength(it.remaining.toInt())
32+
writeInt24(it.remaining.toInt())
3233
writePacket(it)
3334
}
3435
}

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/internal/CloseOperations.kt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,17 +28,6 @@ internal inline fun <T : Closeable, R> T.closeOnError(block: (T) -> R): R {
2828
}
2929
}
3030

31-
private val onUndeliveredCloseable: (Closeable) -> Unit = Closeable::close
32-
33-
@Suppress("FunctionName")
34-
internal fun <E : Closeable> SafeChannel(capacity: Int): Channel<E> =
35-
Channel(capacity, onUndeliveredElement = onUndeliveredCloseable)
36-
3731
internal fun <E : Closeable> SendChannel<E>.safeTrySend(element: E) {
3832
trySend(element).onFailure { element.close() }
3933
}
40-
41-
internal fun Channel<out Closeable>.fullClose(cause: Throwable?) {
42-
close(cause) // close channel to provide right cause
43-
cancel() // force call of onUndeliveredElement to release buffered elements
44-
}

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/internal/Prioritizer.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,15 +17,16 @@
1717
package io.rsocket.kotlin.internal
1818

1919
import io.rsocket.kotlin.frame.*
20+
import io.rsocket.kotlin.internal.io.*
2021
import kotlinx.coroutines.*
2122
import kotlinx.coroutines.channels.*
2223
import kotlinx.coroutines.selects.*
2324

2425
private val selectFrame: suspend (Frame) -> Frame = { it }
2526

2627
internal class Prioritizer {
27-
private val priorityChannel = SafeChannel<Frame>(Channel.UNLIMITED)
28-
private val commonChannel = SafeChannel<Frame>(Channel.UNLIMITED)
28+
private val priorityChannel = channelForCloseable<Frame>(Channel.UNLIMITED)
29+
private val commonChannel = channelForCloseable<Frame>(Channel.UNLIMITED)
2930

3031
suspend fun send(frame: Frame) {
3132
currentCoroutineContext().ensureActive()
@@ -43,7 +44,7 @@ internal class Prioritizer {
4344
}
4445

4546
fun close(error: Throwable?) {
46-
priorityChannel.fullClose(error)
47-
commonChannel.fullClose(error)
47+
priorityChannel.cancelWithCause(error)
48+
commonChannel.cancelWithCause(error)
4849
}
4950
}

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/internal/RSocketRequester.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import io.ktor.utils.io.pool.*
2222
import io.rsocket.kotlin.*
2323
import io.rsocket.kotlin.frame.*
2424
import io.rsocket.kotlin.internal.handler.*
25+
import io.rsocket.kotlin.internal.io.*
2526
import io.rsocket.kotlin.payload.*
2627
import kotlinx.coroutines.*
2728
import kotlinx.coroutines.channels.*
@@ -77,7 +78,7 @@ internal class RSocketRequester(
7778

7879
val id = streamsStorage.nextId()
7980

80-
val channel = SafeChannel<Payload>(Channel.UNLIMITED)
81+
val channel = channelForCloseable<Payload>(Channel.UNLIMITED)
8182
val handler = RequesterRequestStreamFrameHandler(id, streamsStorage, channel, pool)
8283
streamsStorage.save(id, handler)
8384

@@ -93,7 +94,7 @@ internal class RSocketRequester(
9394

9495
val id = streamsStorage.nextId()
9596

96-
val channel = SafeChannel<Payload>(Channel.UNLIMITED)
97+
val channel = channelForCloseable<Payload>(Channel.UNLIMITED)
9798
val limiter = Limiter(0)
9899
val payloadsJob = Job(this@RSocketRequester.coroutineContext.job)
99100
val handler = RequesterRequestChannelFrameHandler(id, streamsStorage, limiter, payloadsJob, channel, pool)

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/internal/handler/RequesterRequestChannelFrameHandler.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package io.rsocket.kotlin.internal.handler
1919
import io.ktor.utils.io.core.internal.*
2020
import io.ktor.utils.io.pool.*
2121
import io.rsocket.kotlin.internal.*
22+
import io.rsocket.kotlin.internal.io.*
2223
import io.rsocket.kotlin.payload.*
2324
import kotlinx.coroutines.*
2425
import kotlinx.coroutines.channels.*
@@ -42,7 +43,7 @@ internal class RequesterRequestChannelFrameHandler(
4243

4344
override fun handleError(cause: Throwable) {
4445
streamsStorage.remove(id)
45-
channel.fullClose(cause)
46+
channel.cancelWithCause(cause)
4647
sender.cancel("Request failed", cause)
4748
}
4849

@@ -55,7 +56,7 @@ internal class RequesterRequestChannelFrameHandler(
5556
}
5657

5758
override fun cleanup(cause: Throwable?) {
58-
channel.fullClose(cause)
59+
channel.cancelWithCause(cause)
5960
sender.cancel("Connection closed", cause)
6061
}
6162

@@ -78,7 +79,7 @@ internal class RequesterRequestChannelFrameHandler(
7879
if (sender.isCancelled) return false
7980

8081
val isFailed = streamsStorage.remove(id) != null
81-
if (isFailed) channel.fullClose(cause)
82+
if (isFailed) channel.cancelWithCause(cause)
8283
return isFailed
8384
}
8485
}

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/internal/handler/RequesterRequestStreamFrameHandler.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package io.rsocket.kotlin.internal.handler
1919
import io.ktor.utils.io.core.internal.*
2020
import io.ktor.utils.io.pool.*
2121
import io.rsocket.kotlin.internal.*
22+
import io.rsocket.kotlin.internal.io.*
2223
import io.rsocket.kotlin.payload.*
2324
import kotlinx.coroutines.channels.*
2425

@@ -39,11 +40,11 @@ internal class RequesterRequestStreamFrameHandler(
3940

4041
override fun handleError(cause: Throwable) {
4142
streamsStorage.remove(id)
42-
channel.fullClose(cause)
43+
channel.cancelWithCause(cause)
4344
}
4445

4546
override fun cleanup(cause: Throwable?) {
46-
channel.fullClose(cause)
47+
channel.cancelWithCause(cause)
4748
}
4849

4950
override fun onReceiveComplete() {

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/internal/handler/ResponderRequestChannelFrameHandler.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import io.ktor.utils.io.core.internal.*
2020
import io.ktor.utils.io.pool.*
2121
import io.rsocket.kotlin.*
2222
import io.rsocket.kotlin.internal.*
23+
import io.rsocket.kotlin.internal.io.*
2324
import io.rsocket.kotlin.payload.*
2425
import kotlinx.coroutines.*
2526
import kotlinx.coroutines.channels.*
@@ -32,7 +33,7 @@ internal class ResponderRequestChannelFrameHandler(
3233
pool: ObjectPool<ChunkBuffer>
3334
) : ResponderFrameHandler(pool), ReceiveFrameHandler {
3435
val limiter = Limiter(initialRequest)
35-
val channel = SafeChannel<Payload>(Channel.UNLIMITED)
36+
val channel = channelForCloseable<Payload>(Channel.UNLIMITED)
3637

3738
@OptIn(ExperimentalStreamsApi::class)
3839
override fun start(payload: Payload): Job = responder.handleRequestChannel(payload, id, this)
@@ -47,13 +48,13 @@ internal class ResponderRequestChannelFrameHandler(
4748

4849
override fun handleError(cause: Throwable) {
4950
streamsStorage.remove(id)
50-
channel.fullClose(cause)
51+
channel.cancelWithCause(cause)
5152
}
5253

5354
override fun handleCancel() {
5455
streamsStorage.remove(id)
5556
val cancelError = CancellationException("Request cancelled")
56-
channel.fullClose(cancelError)
57+
channel.cancelWithCause(cancelError)
5758
job?.cancel(cancelError)
5859
}
5960

@@ -62,7 +63,7 @@ internal class ResponderRequestChannelFrameHandler(
6263
}
6364

6465
override fun cleanup(cause: Throwable?) {
65-
channel.fullClose(cause)
66+
channel.cancelWithCause(cause)
6667
}
6768

6869
override fun onSendComplete() {
@@ -72,7 +73,7 @@ internal class ResponderRequestChannelFrameHandler(
7273

7374
override fun onSendFailed(cause: Throwable): Boolean {
7475
val isFailed = streamsStorage.remove(id) != null
75-
if (isFailed) channel.fullClose(cause)
76+
if (isFailed) channel.cancelWithCause(cause)
7677
return isFailed
7778
}
7879

rsocket-core/src/commonMain/kotlin/io/rsocket/kotlin/metadata/CompositeMetadata.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import io.ktor.utils.io.pool.*
2222
import io.rsocket.kotlin.*
2323
import io.rsocket.kotlin.core.*
2424
import io.rsocket.kotlin.frame.io.*
25+
import io.rsocket.kotlin.internal.io.*
2526

2627
@ExperimentalMetadataApi
2728
public fun CompositeMetadata(vararg entries: Metadata): CompositeMetadata =
@@ -39,7 +40,7 @@ public sealed interface CompositeMetadata : Metadata {
3940
override fun BytePacketBuilder.writeSelf() {
4041
entries.forEach {
4142
writeMimeType(it.mimeType)
42-
writeLength(it.content.remaining.toInt()) //write metadata length
43+
writeInt24(it.content.remaining.toInt()) //write metadata length
4344
writePacket(it.content) //write metadata content
4445
}
4546
}
@@ -58,7 +59,7 @@ public sealed interface CompositeMetadata : Metadata {
5859
val list = mutableListOf<Entry>()
5960
while (isNotEmpty) {
6061
val type = readMimeType()
61-
val length = readLength()
62+
val length = readInt24()
6263
val packet = readPacket(pool, length)
6364
list.add(Entry(type, packet))
6465
}

rsocket-core/src/commonTest/kotlin/io/rsocket/kotlin/TestConnection.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import io.ktor.utils.io.core.*
2121
import io.ktor.utils.io.core.internal.*
2222
import io.ktor.utils.io.pool.*
2323
import io.rsocket.kotlin.frame.*
24-
import io.rsocket.kotlin.internal.*
24+
import io.rsocket.kotlin.internal.io.*
2525
import io.rsocket.kotlin.test.*
2626
import io.rsocket.kotlin.transport.*
2727
import kotlinx.coroutines.*
@@ -37,13 +37,13 @@ class TestConnection : Connection, ClientTransport {
3737
override val coroutineContext: CoroutineContext =
3838
Job() + Dispatchers.Unconfined + TestExceptionHandler
3939

40-
private val sendChannel = Channel<ByteReadPacket>(Channel.UNLIMITED)
41-
private val receiveChannel = Channel<ByteReadPacket>(Channel.UNLIMITED)
40+
private val sendChannel = channelForCloseable<ByteReadPacket>(Channel.UNLIMITED)
41+
private val receiveChannel = channelForCloseable<ByteReadPacket>(Channel.UNLIMITED)
4242

4343
init {
4444
coroutineContext.job.invokeOnCompletion {
4545
sendChannel.close(it)
46-
receiveChannel.fullClose(it)
46+
receiveChannel.cancelWithCause(it)
4747
}
4848
}
4949

0 commit comments

Comments
 (0)