-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement non-blocking unary request server runtime
- Loading branch information
Showing
4 changed files
with
301 additions
and
54 deletions.
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
98 changes: 98 additions & 0 deletions
98
runtime/src/main/scala/fs2/grpc/server/internal/Fs2ServerCall.scala
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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.effect.std.Dispatcher | ||
import fs2._ | ||
import fs2.grpc.server.ServerCallOptions | ||
import io.grpc._ | ||
|
||
private[server] object Fs2ServerCall { | ||
type Cancel = SyncIO[Unit] | ||
|
||
def setup[I, O]( | ||
options: ServerCallOptions, | ||
call: ServerCall[I, O] | ||
): SyncIO[Fs2ServerCall[I, O]] = | ||
SyncIO { | ||
call.setMessageCompression(options.messageCompression) | ||
options.compressor.map(_.name).foreach(call.setCompression) | ||
new Fs2ServerCall[I, O](call) | ||
} | ||
} | ||
|
||
private[server] final class Fs2ServerCall[Request, Response]( | ||
call: ServerCall[Request, Response] | ||
) { | ||
|
||
import Fs2ServerCall.Cancel | ||
|
||
def stream[F[_]](response: Stream[F, Response], dispatcher: Dispatcher[F])(implicit F: Sync[F]): SyncIO[Cancel] = | ||
run( | ||
response.pull.peek1 | ||
.flatMap { | ||
case Some((_, stream)) => | ||
Pull.suspend { | ||
call.sendHeaders(new Metadata()) | ||
stream.map(call.sendMessage).pull.echo | ||
} | ||
case None => Pull.done | ||
} | ||
.stream | ||
.compile | ||
.drain, | ||
dispatcher | ||
) | ||
|
||
def unary[F[_]](response: F[Response], dispatcher: Dispatcher[F])(implicit F: Sync[F]): SyncIO[Cancel] = | ||
run( | ||
F.map(response) { message => | ||
call.sendHeaders(new Metadata()) | ||
call.sendMessage(message) | ||
}, | ||
dispatcher | ||
) | ||
|
||
def request(n: Int): SyncIO[Unit] = | ||
SyncIO(call.request(n)) | ||
|
||
def close(status: Status, metadata: Metadata): SyncIO[Unit] = | ||
SyncIO(call.close(status, metadata)) | ||
|
||
private def run[F[_]](completed: F[Unit], dispatcher: Dispatcher[F])(implicit F: Sync[F]): SyncIO[Cancel] = { | ||
SyncIO { | ||
val cancel = dispatcher.unsafeRunCancelable(F.guaranteeCase(completed) { | ||
case Outcome.Succeeded(_) => close(Status.OK, new Metadata()).to[F] | ||
case Outcome.Errored(e) => handleError(e).to[F] | ||
case Outcome.Canceled() => close(Status.CANCELLED, new Metadata()).to[F] | ||
}) | ||
SyncIO(cancel()).void | ||
} | ||
} | ||
|
||
private def handleError(t: Throwable): SyncIO[Unit] = t match { | ||
case ex: StatusException => close(ex.getStatus, Option(ex.getTrailers).getOrElse(new Metadata())) | ||
case ex: StatusRuntimeException => close(ex.getStatus, Option(ex.getTrailers).getOrElse(new Metadata())) | ||
case ex => close(Status.INTERNAL.withDescription(ex.getMessage).withCause(ex), new Metadata()) | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
runtime/src/main/scala/fs2/grpc/server/internal/Fs2UnaryServerCallHandler.scala
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 |
---|---|---|
@@ -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.Async | ||
import cats.effect.Ref | ||
import cats.effect.Sync | ||
import cats.effect.SyncIO | ||
import cats.effect.std.Dispatcher | ||
import fs2.grpc.server.ServerCallOptions | ||
import fs2.grpc.server.ServerOptions | ||
import io.grpc._ | ||
|
||
private[server] object Fs2UnaryServerCallHandler { | ||
|
||
import Fs2ServerCall.Cancel | ||
|
||
sealed trait CallerState[A] | ||
object CallerState { | ||
def init[A](cb: A => SyncIO[Cancel]): SyncIO[Ref[SyncIO, CallerState[A]]] = | ||
Ref[SyncIO].of[CallerState[A]](PendingMessage(cb)) | ||
} | ||
case class PendingMessage[A](callback: A => SyncIO[Cancel]) extends CallerState[A] { | ||
def receive(a: A): PendingHalfClose[A] = PendingHalfClose(callback, a) | ||
} | ||
case class PendingHalfClose[A](callback: A => SyncIO[Cancel], received: A) extends CallerState[A] { | ||
def call(): SyncIO[Called[A]] = callback(received).map(Called.apply) | ||
} | ||
case class Called[A](cancel: Cancel) extends CallerState[A] | ||
case class Cancelled[A]() extends CallerState[A] | ||
|
||
private def mkListener[Request, Response]( | ||
call: Fs2ServerCall[Request, Response], | ||
state: Ref[SyncIO, CallerState[Request]] | ||
): ServerCall.Listener[Request] = | ||
new ServerCall.Listener[Request] { | ||
override def onCancel(): Unit = | ||
state.get | ||
.flatMap { | ||
case Called(cancel) => cancel >> state.set(Cancelled()) | ||
case _ => SyncIO.unit | ||
} | ||
.unsafeRunSync() | ||
|
||
override def onMessage(message: Request): Unit = | ||
state.get | ||
.flatMap { | ||
case s: PendingMessage[Request] => | ||
state.set(s.receive(message)) | ||
case _: PendingHalfClose[Request] => | ||
sendError(Status.INTERNAL.withDescription("Too many requests")) | ||
case _ => | ||
SyncIO.unit | ||
} | ||
.unsafeRunSync() | ||
|
||
override def onHalfClose(): Unit = | ||
state.get | ||
.flatMap { | ||
case s: PendingHalfClose[Request] => | ||
s.call().flatMap(state.set) | ||
case _: PendingMessage[Request] => | ||
sendError(Status.INTERNAL.withDescription("Half-closed without a request")) | ||
case _ => | ||
SyncIO.unit | ||
} | ||
.unsafeRunSync() | ||
|
||
private def sendError(status: Status): SyncIO[Unit] = | ||
state.set(Cancelled()) >> call.close(status, new Metadata()) | ||
} | ||
|
||
def unary[F[_]: Sync, Request, Response]( | ||
impl: (Request, Metadata) => F[Response], | ||
options: ServerOptions, | ||
dispatcher: Dispatcher[F] | ||
): 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] = | ||
startCallSync(call, opt)(call => req => call.unary(impl(req, headers), dispatcher)).unsafeRunSync() | ||
} | ||
|
||
def stream[F[_]: Sync, Request, Response]( | ||
impl: (Request, Metadata) => fs2.Stream[F, Response], | ||
options: ServerOptions, | ||
dispatcher: Dispatcher[F] | ||
): 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] = | ||
startCallSync(call, opt)(call => req => call.stream(impl(req, headers), dispatcher)).unsafeRunSync() | ||
} | ||
|
||
private def startCallSync[F[_], Request, Response]( | ||
call: ServerCall[Request, Response], | ||
options: ServerCallOptions | ||
)(f: Fs2ServerCall[Request, Response] => Request => SyncIO[Cancel]): SyncIO[ServerCall.Listener[Request]] = { | ||
for { | ||
call <- Fs2ServerCall.setup(options, call) | ||
// We expect only 1 request, but we ask for 2 requests here so that if a misbehaving client | ||
// sends more than 1 requests, ServerCall will catch it. | ||
_ <- call.request(2) | ||
state <- CallerState.init(f(call)) | ||
} yield mkListener[Request, Response](call, state) | ||
} | ||
} |
Oops, something went wrong.