-
-
Notifications
You must be signed in to change notification settings - Fork 65
implement non-blocking stream server handler #503
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
Open
naoh87
wants to merge
4
commits into
typelevel:main
Choose a base branch
from
naoh87:light_server_stream_runtime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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 hidden or 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 hidden or 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 hidden or 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
66 changes: 66 additions & 0 deletions
66
runtime/src/main/scala/fs2/grpc/server/internal/Fs2StreamServerCallHandler.scala
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright (c) 2018 Gary Coady / Fs2 Grpc Developers | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| * this software and associated documentation files (the "Software"), to deal in | ||
| * the Software without restriction, including without limitation the rights to | ||
| * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| * the Software, and to permit persons to whom the Software is furnished to do so, | ||
| * subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| package fs2.grpc.server.internal | ||
|
|
||
| import cats.effect.Async | ||
| import cats.effect.SyncIO | ||
| import fs2._ | ||
| import fs2.grpc.server.ServerCallOptions | ||
| import fs2.grpc.server.ServerOptions | ||
| import fs2.grpc.server.internal.Fs2ServerCall.Cancel | ||
| import io.grpc.ServerCall | ||
| import io.grpc._ | ||
|
|
||
| object Fs2StreamServerCallHandler { | ||
|
|
||
| private def mkListener[Request]( | ||
| channel: OneShotChannel[Request], | ||
| cancel: Cancel | ||
| ): ServerCall.Listener[Request] = | ||
| new ServerCall.Listener[Request] { | ||
| override def onCancel(): Unit = | ||
| cancel.unsafeRunSync() | ||
|
|
||
| override def onMessage(message: Request): Unit = | ||
| channel.send(message).unsafeRunSync() | ||
|
|
||
| override def onHalfClose(): Unit = | ||
| channel.close().unsafeRunSync() | ||
| } | ||
|
|
||
| def mkHandler[F[_]: Async, G[_], Request, Response]( | ||
| impl: (Stream[F, Request], Metadata) => G[Response], | ||
| options: ServerOptions | ||
| )(start: (Fs2ServerCall[Request, Response], G[Response]) => SyncIO[Cancel]): ServerCallHandler[Request, Response] = | ||
| new ServerCallHandler[Request, Response] { | ||
| private val opt = options.callOptionsFn(ServerCallOptions.default) | ||
|
|
||
| def startCall(call: ServerCall[Request, Response], headers: Metadata): ServerCall.Listener[Request] = { | ||
| for { | ||
| call <- Fs2ServerCall.setup(opt, call) | ||
| _ <- call.request(1) // prefetch size | ||
| channel <- OneShotChannel.empty[Request] | ||
| cancel <- start(call, impl(channel.stream.through(call.requestOnPull), headers)) | ||
| } yield mkListener(channel, cancel) | ||
| }.unsafeRunSync() | ||
| } | ||
| } | ||
This file contains hidden or 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
128 changes: 128 additions & 0 deletions
128
runtime/src/main/scala/fs2/grpc/server/internal/OneShotChannel.scala
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| * Copyright (c) 2018 Gary Coady / Fs2 Grpc Developers | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| * this software and associated documentation files (the "Software"), to deal in | ||
| * the Software without restriction, including without limitation the rights to | ||
| * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| * the Software, and to permit persons to whom the Software is furnished to do so, | ||
| * subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| package fs2.grpc.server.internal | ||
|
|
||
| import cats.effect._ | ||
| import cats.syntax.functor._ | ||
| import fs2._ | ||
| import fs2.grpc.server.internal.OneShotChannel.State | ||
| import scala.collection.immutable.Queue | ||
|
|
||
| private[server] final class OneShotChannel[A](val state: Ref[SyncIO, State[A]]) extends AnyVal { | ||
|
|
||
| import State._ | ||
|
|
||
| /** Send message to stream. | ||
| */ | ||
| def send(a: A): SyncIO[Unit] = | ||
| state | ||
| .modify { | ||
| case open: Open[A] => (open.append(a), SyncIO.unit) | ||
| case s: Suspended[A] => (State.consumed, s.resume(State.open(a))) | ||
| case closed => (closed, SyncIO.unit) | ||
| } | ||
| .flatMap(identity) | ||
|
|
||
| /** Close stream. | ||
| */ | ||
| def close(): SyncIO[Unit] = | ||
| state | ||
| .modify { | ||
| case open: Open[A] => (open.close(), SyncIO.unit) | ||
| case s: Suspended[A] => (State.done, s.resume(State.done)) | ||
| case closed => (closed, SyncIO.unit) | ||
| } | ||
| .flatMap(identity) | ||
|
|
||
| import fs2._ | ||
|
|
||
| /** This method can be called at most once | ||
| */ | ||
| def stream[F[_]](implicit F: Async[F]): Stream[F, A] = { | ||
| def go(): Pull[F, A, Unit] = | ||
| Pull | ||
| .eval(state.getAndSet(State.consumed).to[F]) | ||
| .flatMap { | ||
| case Consumed => | ||
| Pull.eval(F.async[State[A]] { cb => | ||
| val next = new Suspended[A](s => cb(Right(s))) | ||
| state | ||
| .modify { | ||
| case Consumed => (next, None) | ||
| case other => (State.consumed, Some(other)) | ||
| } | ||
| .to[F] | ||
| .map { | ||
| case Some(received) => | ||
| cb(Right(received)) | ||
| None | ||
| case None => | ||
| Some(state.set(State.consumed).to[F]) | ||
| } | ||
| }) | ||
| case other => Pull.pure(other) | ||
| } | ||
| .flatMap { | ||
| case open: Open[A] => open.toPull >> go() | ||
| case other => other.toPull | ||
| } | ||
|
|
||
| go().stream | ||
| } | ||
| } | ||
|
|
||
| private[server] object OneShotChannel { | ||
| def empty[A]: SyncIO[OneShotChannel[A]] = | ||
| Ref[SyncIO].of[State[A]](State.consumed).map(new OneShotChannel[A](_)) | ||
|
|
||
| sealed trait State[A] { | ||
| def toPull[F[_]: Sync]: Pull[F, A, Unit] | ||
| } | ||
|
|
||
| object State { | ||
| class UnexpectedState extends RuntimeException | ||
| private[OneShotChannel] val Consumed: State[Nothing] = new Open(Queue.empty) | ||
| def consumed[A]: State[A] = Consumed.asInstanceOf[State[A]] | ||
|
|
||
| def done[A]: State[A] = new Closed(Queue.empty) | ||
|
|
||
| def open[A](a: A): Open[A] = new Open(Queue(a)) | ||
|
|
||
| class Open[A](queue: Queue[A]) extends State[A] { | ||
| def append(a: A): Open[A] = new Open(queue.enqueue(a)) | ||
|
|
||
| def toPull[F[_]: Sync]: Pull[F, A, Unit] = Pull.output(Chunk.queue(queue)) | ||
|
|
||
| def close(): State[A] = new Closed(queue) | ||
| } | ||
|
|
||
| class Closed[A](queue: Queue[A]) extends State[A] { | ||
| def toPull[F[_]: Sync]: Pull[F, A, Unit] = Pull.output(Chunk.queue(queue)) | ||
| } | ||
|
|
||
| class Suspended[A](f: State[A] => Unit) extends State[A] { | ||
| def resume(state: State[A]): SyncIO[Unit] = SyncIO(f(state)) | ||
|
|
||
| def toPull[F[_]: Sync]: Pull[F, A, Unit] = Pull.raiseError(new UnexpectedState) // never happened | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
@ahjohannessen back pressure is controlled from here of
call.requsetOnPull.Server requests next messages from the client each stream chunk is pulled.
Prefetch and buffer size is declarabled by 2 lines ago.
_ <- call.request(1)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.
Server to client message back pressure implementation is still missing as main branch.
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.
I think this server to client back pressure implementation may cause performance issue.
grpc-java says free to ignore this and main branch does.
https://github.com/grpc/grpc-java/blob/v1.46.0/api/src/main/java/io/grpc/ServerCall.java#L100
I think this feature should be opt-in.