4
4
5
5
package kotlinx.rpc.grpc.internal
6
6
7
+ import kotlinx.atomicfu.atomic
7
8
import kotlinx.coroutines.CancellationException
8
9
import kotlinx.coroutines.CoroutineScope
9
10
import kotlinx.coroutines.cancel
@@ -21,8 +22,6 @@ import kotlinx.rpc.grpc.StatusCode
21
22
import kotlinx.rpc.grpc.StatusException
22
23
import kotlinx.rpc.grpc.StatusRuntimeException
23
24
import kotlinx.rpc.internal.utils.InternalRpcApi
24
- import kotlin.concurrent.atomics.AtomicBoolean
25
- import kotlin.concurrent.atomics.ExperimentalAtomicApi
26
25
27
26
@InternalRpcApi
28
27
public fun <Request , Response > CoroutineScope.unaryServerMethodDefinition (
@@ -105,7 +104,6 @@ private fun <Request, Response> CoroutineScope.serverCallHandler(
105
104
serverCallListenerImpl(call, implementation)
106
105
}
107
106
108
- @OptIn(ExperimentalAtomicApi ::class )
109
107
private fun <Request , Response > CoroutineScope.serverCallListenerImpl (
110
108
handler : ServerCall <Request , Response >,
111
109
implementation : (Flow <Request >) -> Flow <Response >,
@@ -116,7 +114,7 @@ private fun <Request, Response> CoroutineScope.serverCallListenerImpl(
116
114
val requestsStarted = AtomicBoolean (false ) // enforces read-once
117
115
118
116
val requests = flow {
119
- check(requestsStarted.compareAndSet(expectedValue = false , newValue = true )) {
117
+ check(requestsStarted.value. compareAndSet(expect = false , update = true )) {
120
118
" requests flow can only be collected once"
121
119
}
122
120
@@ -141,7 +139,7 @@ private fun <Request, Response> CoroutineScope.serverCallListenerImpl(
141
139
val failure = runCatching {
142
140
implementation(requests).collect {
143
141
// once we have a response message, check if we've sent headers yet - if not, do so
144
- if (headersSent.compareAndSet(expectedValue = false , newValue = true )) {
142
+ if (headersSent.value. compareAndSet(expect = false , update = true )) {
145
143
mutex.withLock {
146
144
handler.sendHeaders(GrpcTrailers ())
147
145
}
@@ -152,7 +150,7 @@ private fun <Request, Response> CoroutineScope.serverCallListenerImpl(
152
150
}.exceptionOrNull()
153
151
// check headers again once we're done collecting the response flow - if we received
154
152
// no elements or threw an exception, then we wouldn't have sent them
155
- if (failure == null && headersSent.compareAndSet(expectedValue = false , newValue = true )) {
153
+ if (failure == null && headersSent.value. compareAndSet(expect = false , update = true )) {
156
154
mutex.withLock {
157
155
handler.sendHeaders(GrpcTrailers ())
158
156
}
@@ -217,6 +215,10 @@ private fun <Request, Response> CoroutineScope.serverCallListenerImpl(
217
215
)
218
216
}
219
217
218
+ private class AtomicBoolean (initialValue : Boolean ) {
219
+ val value = atomic(initialValue)
220
+ }
221
+
220
222
private class ServerCallListenerState {
221
223
var isReceiving = true
222
224
}
0 commit comments