-
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
Auto-dispatch to I/O context #218
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ package com.connectrpc.http | |
|
||
import com.connectrpc.StreamResult | ||
import okio.Buffer | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
typealias Cancelable = () -> Unit | ||
|
||
|
@@ -46,92 +45,3 @@ interface HTTPClientInterface { | |
*/ | ||
fun stream(request: HTTPRequest, duplex: Boolean, onResult: suspend (StreamResult<Buffer>) -> Unit): Stream | ||
} | ||
|
||
interface Stream { | ||
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. Minor cleanup in here: I hoisted the |
||
suspend fun send(buffer: Buffer): Result<Unit> | ||
|
||
fun sendClose() | ||
|
||
fun receiveClose() | ||
|
||
fun isSendClosed(): Boolean | ||
|
||
fun isReceiveClosed(): Boolean | ||
} | ||
|
||
fun Stream( | ||
onSend: suspend (Buffer) -> Result<Unit>, | ||
onSendClose: () -> Unit = {}, | ||
onReceiveClose: () -> Unit = {}, | ||
): Stream { | ||
val isSendClosed = AtomicBoolean() | ||
val isReceiveClosed = AtomicBoolean() | ||
return object : Stream { | ||
override suspend fun send(buffer: Buffer): Result<Unit> { | ||
if (isSendClosed()) { | ||
return Result.failure(IllegalStateException("cannot send. underlying stream is closed")) | ||
} | ||
return try { | ||
onSend(buffer) | ||
} catch (e: Throwable) { | ||
Result.failure(e) | ||
} | ||
} | ||
|
||
override fun sendClose() { | ||
if (isSendClosed.compareAndSet(false, true)) { | ||
onSendClose() | ||
} | ||
} | ||
|
||
override fun receiveClose() { | ||
if (isReceiveClosed.compareAndSet(false, true)) { | ||
try { | ||
onReceiveClose() | ||
} finally { | ||
// When receive side is closed, the send side is | ||
// implicitly closed as well. | ||
// We don't use sendClose() because we don't want to | ||
// invoke onSendClose() since that will try to actually | ||
// half-close the HTTP stream, which will fail since | ||
// closing the receive side cancels the entire thing. | ||
isSendClosed.set(true) | ||
} | ||
} | ||
} | ||
|
||
override fun isSendClosed(): Boolean { | ||
return isSendClosed.get() | ||
} | ||
|
||
override fun isReceiveClosed(): Boolean { | ||
return isReceiveClosed.get() | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns a new stream that applies the given function to each | ||
* buffer when send is called. The result of that function is | ||
* what is passed along to the original stream. | ||
*/ | ||
fun Stream.transform(apply: (Buffer) -> Buffer): Stream { | ||
val delegate = this | ||
return object : Stream { | ||
override suspend fun send(buffer: Buffer): Result<Unit> { | ||
return delegate.send(apply(buffer)) | ||
} | ||
override fun sendClose() { | ||
delegate.sendClose() | ||
} | ||
override fun receiveClose() { | ||
delegate.receiveClose() | ||
} | ||
override fun isSendClosed(): Boolean { | ||
return delegate.isSendClosed() | ||
} | ||
override fun isReceiveClosed(): Boolean { | ||
return delegate.isReceiveClosed() | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should code that deals with the UI still be dispatched to Dispatchers.Main? I'm not familiar with android development but we might want to keep this here.
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.
The documentation for
LifecycleCoroutineScope
(the type returned bylifecycleScope
) states that it is already tied toDispatchers.Main
. So the above call tolifecycleScope.launch
should already dispatch in the right context.I can set it explicitly above if you'd prefer. I wasn't really sure what was the most idiomatic for Android apps -- to omit when using the default or to always specify explicitly. I guess, even if it is idiomatic to omit, we might want to state it explicitly, just for maximum clarity.
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.
Ok - I was looking for that documentation and didn't find that information. I think this is ok as-is.