Skip to content

Commit

Permalink
Use shutdownNow in client close, #212
Browse files Browse the repository at this point in the history
* and close client when ActorSystem has terminated
  • Loading branch information
patriknw committed Jun 29, 2018
1 parent 2c7a89c commit 1b84384
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions codegen/src/main/twirl/templates/ScalaClient/Client.scala.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import akka.grpc.scaladsl.{ SingleResponseRequestBuilder, StreamResponseRequestB
import akka.grpc.GrpcClientSettings
import akka.stream.{ Materializer, OverflowStrategy }
import akka.stream.scaladsl.{ Flow, Sink, Source }
import akka.stream.ActorMaterializer
import akka.stream.Materializer
import scala.concurrent.{ ExecutionContext, Future }

Expand All @@ -27,6 +28,12 @@ final class @{service.name}Client(settings: GrpcClientSettings)(implicit mat: Ma
private val channel = NettyClientUtils.createChannel(settings)
private val options = NettyClientUtils.callOptions(settings)

mat match {
case m: ActorMaterializer =>
m.system.whenTerminated.foreach(_ => close())(ex)
case _ =>
}

@for(method <- service.methods) {
private val @{method.name}RequestBuilder = {
@if(method.methodType == akka.grpc.gen.Unary) {
Expand Down Expand Up @@ -64,8 +71,12 @@ final class @{service.name}Client(settings: GrpcClientSettings)(implicit mat: Ma
@{method.name}().invoke(in)
}

// TODO perhaps return Future[Done]?
def close(): Unit = channel.shutdown()
// TODO expose the close state as Future[Done]

/**
* Initiates a shutdown in which preexisting and new calls are cancelled.
*/
def close(): Unit = channel.shutdownNow()

}

Expand Down

0 comments on commit 1b84384

Please sign in to comment.