-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify UnaryBlockingCall #225
Changes from 2 commits
536d92a
4af7f5d
ea06ddc
5e23db9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2022-2023 The Connect Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.connectrpc.impl | ||
|
||
import com.connectrpc.ResponseMessage | ||
import com.connectrpc.UnaryBlockingCall | ||
import com.connectrpc.http.Cancelable | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
/** | ||
* Callback that handles asynchronous response. | ||
*/ | ||
internal typealias ResponseCallback<T> = (ResponseMessage<T>) -> Unit | ||
|
||
/** | ||
* Represents a cancelable asynchronous operation. When the function | ||
* is invoked, the operation is initiated. When that operation completes | ||
* it MUST invoke the callback, even when canceled. The value returned | ||
* from the function can be called to abort the operation and have it | ||
* return early. | ||
*/ | ||
internal typealias AsyncOperation<T> = (callback: ResponseCallback<T>) -> Cancelable | ||
|
||
/** | ||
* Concrete implementation of [UnaryBlockingCall] which transforms | ||
* the given async operation into a synchronous/blocking one. | ||
*/ | ||
internal class UnaryCall<Output>( | ||
private val block: AsyncOperation<Output>, | ||
) : UnaryBlockingCall<Output> { | ||
private val executed = AtomicBoolean() | ||
|
||
/** | ||
* initialized to null and then replaced with non-null | ||
* function when [execute] or [cancel] is called. | ||
*/ | ||
private var cancelFunc = AtomicReference<Cancelable>() | ||
|
||
/** | ||
* Execute the underlying operation and block until it completes. | ||
*/ | ||
override fun execute(): ResponseMessage<Output> { | ||
check(executed.compareAndSet(false, true)) { "already executed" } | ||
|
||
val resultReady = CountDownLatch(1) | ||
val result = AtomicReference<ResponseMessage<Output>>() | ||
val cancelFn = block { responseMessage -> | ||
result.set(responseMessage) | ||
resultReady.countDown() | ||
} | ||
|
||
if (!cancelFunc.compareAndSet(null, cancelFn)) { | ||
// concurrently cancelled before we could set the | ||
// cancel function, so we need to cancel what we | ||
// just started | ||
cancelFn() | ||
} | ||
resultReady.await() | ||
return result.get() | ||
} | ||
|
||
/** | ||
* Cancel the underlying request. | ||
*/ | ||
override fun cancel() { | ||
val cancelFn = cancelFunc.getAndSet {} // set to (non-null) no-op | ||
if (cancelFn != null) { | ||
cancelFn() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2022-2023 The Connect Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.connectrpc.impl | ||
|
||
import com.connectrpc.Code | ||
import com.connectrpc.ConnectException | ||
import com.connectrpc.ResponseMessage | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Test | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
|
||
class UnaryCallTest { | ||
@Test | ||
fun testExecute() { | ||
val executor = Executors.newSingleThreadExecutor { | ||
val t = Thread(it) | ||
t.isDaemon = true | ||
t | ||
} | ||
try { | ||
val result = Object() | ||
val call = UnaryCall<Any> { callback -> | ||
val future = executor.submit { | ||
try { | ||
Thread.sleep(250L) | ||
} catch (ex: InterruptedException) { | ||
callback.invoke( | ||
ResponseMessage.Failure( | ||
headers = emptyMap(), | ||
trailers = emptyMap(), | ||
cause = ConnectException(code = Code.CANCELED, exception = ex), | ||
), | ||
) | ||
return@submit | ||
} | ||
callback.invoke( | ||
ResponseMessage.Success( | ||
result, | ||
headers = emptyMap(), | ||
trailers = emptyMap(), | ||
), | ||
) | ||
} | ||
return@UnaryCall { | ||
future.cancel(true) | ||
} | ||
} | ||
val resp = call.execute() | ||
assertThat(resp).isInstanceOf(ResponseMessage.Success::class.java) | ||
val msg = resp.success { it.message }!! | ||
assertThat(msg).isEqualTo(result) | ||
} finally { | ||
assertThat(executor.shutdownNow()).isEmpty() | ||
} | ||
} | ||
|
||
@Test | ||
fun testCancel() { | ||
val executor = Executors.newFixedThreadPool(2) { | ||
val t = Thread(it) | ||
t.isDaemon = true | ||
t | ||
} | ||
try { | ||
val start = System.nanoTime() | ||
val call = UnaryCall<Any> { callback -> | ||
val future = executor.submit { | ||
try { | ||
Thread.sleep(1_000L) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it would be possible to coordinate these threads without relying on sleep. Maybe a countdown latch which is counted down here (before it just calls .wait() indefinitely). Then in the execution thread below instead of a sleep on line 108 we wait on the countdown latch to signal that this callable has started and will be interrupted when we call cancel. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. I also added another test for the case where |
||
} catch (ex: InterruptedException) { | ||
callback.invoke( | ||
ResponseMessage.Failure( | ||
headers = emptyMap(), | ||
trailers = emptyMap(), | ||
cause = ConnectException(code = Code.CANCELED, exception = ex), | ||
), | ||
) | ||
return@submit | ||
} | ||
callback.invoke( | ||
ResponseMessage.Success( | ||
Object(), | ||
headers = emptyMap(), | ||
trailers = emptyMap(), | ||
), | ||
) | ||
} | ||
return@UnaryCall { | ||
future.cancel(true) | ||
} | ||
} | ||
// Cancel should happen before normal completion | ||
// and should interrupt the above task. | ||
executor.execute { | ||
Thread.sleep(250L) | ||
call.cancel() | ||
} | ||
val resp = call.execute() | ||
val duration = System.nanoTime() - start | ||
|
||
assertThat(resp).isInstanceOf(ResponseMessage.Failure::class.java) | ||
val connEx = resp.failure { it.cause }!! | ||
assertThat(connEx.code).isEqualTo(Code.CANCELED) | ||
|
||
val millis = TimeUnit.MILLISECONDS.convert(duration, TimeUnit.NANOSECONDS) | ||
// we give extra 250ms grace period to avoid flaky failures | ||
assertThat(millis).isLessThan(500L) | ||
} finally { | ||
assertThat(executor.shutdownNow()).isEmpty() | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this test be simplified to just call success on the callback without a delay? (So remove try/catch block above). Since nothing calls cancel, that shouldn't ever be invoked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, good call.