Skip to content

Commit 9effe50

Browse files
authored
Make grpc server shutdown timeout configurable (#117)
* Make grpc server shutdown timeout configurable * Fix * Polish
1 parent ba8964c commit 9effe50

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version = 1.7.1
1+
sbt.version = 1.9.3
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.devsisters.shardcake
22

3+
import zio._
4+
35
import java.util.concurrent.Executor
46

57
/**
68
* The configuration for the gRPC client.
79
*
810
* @param maxInboundMessageSize the maximum message size allowed to be received by the grpc client
911
* @param executor a custom executor to pass to grpc-java when creating gRPC clients and servers
12+
* @param shutdownTimeout the timeout to wait for the gRPC server to shutdown before forcefully shutting it down
1013
*/
11-
case class GrpcConfig(maxInboundMessageSize: Int, executor: Option[Executor])
14+
case class GrpcConfig(maxInboundMessageSize: Int, executor: Option[Executor], shutdownTimeout: Duration)
1215

1316
object GrpcConfig {
1417
val default: GrpcConfig =
15-
GrpcConfig(maxInboundMessageSize = 32 * 1024 * 1024, None)
18+
GrpcConfig(maxInboundMessageSize = 32 * 1024 * 1024, None, 3.seconds)
1619
}

protocol-grpc/src/main/scala/com/devsisters/shardcake/GrpcShardingService.scala

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import com.devsisters.shardcake.interfaces.Pods.BinaryMessage
55
import com.devsisters.shardcake.protobuf.sharding.ZioSharding.ShardingService
66
import com.devsisters.shardcake.protobuf.sharding._
77
import com.google.protobuf.ByteString
8+
import io.grpc._
89
import io.grpc.protobuf.services.ProtoReflectionService
9-
import io.grpc.{ ServerBuilder, Status, StatusException, StatusRuntimeException }
10-
import scalapb.zio_grpc.{ ScopedServer, ServiceList }
11-
import zio.{ Config => _, _ }
10+
import scalapb.zio_grpc.ServiceList
1211
import zio.stream.ZStream
12+
import zio.{ Config => _, _ }
13+
14+
import java.util.concurrent.TimeUnit
1315

1416
abstract class GrpcShardingService(sharding: Sharding, timeout: Duration) extends ShardingService {
1517
def assignShards(request: AssignShardsRequest): ZIO[Any, StatusException, AssignShardsResponse] =
@@ -56,19 +58,29 @@ object GrpcShardingService {
5658
val live: ZLayer[Config with Sharding with GrpcConfig, Throwable, Unit] =
5759
ZLayer.scoped[Config with Sharding with GrpcConfig] {
5860
for {
59-
config <- ZIO.service[Config]
60-
grpcConfig <- ZIO.service[GrpcConfig]
61-
sharding <- ZIO.service[Sharding]
62-
builder = grpcConfig.executor match {
63-
case Some(executor) =>
64-
ServerBuilder
65-
.forPort(config.shardingPort)
66-
.executor(executor)
67-
case None =>
68-
ServerBuilder.forPort(config.shardingPort)
69-
}
70-
services = ServiceList.add(new GrpcShardingService(sharding, config.sendTimeout) {})
71-
_ <- ScopedServer.fromServiceList(builder.addService(ProtoReflectionService.newInstance()), services)
61+
config <- ZIO.service[Config]
62+
grpcConfig <- ZIO.service[GrpcConfig]
63+
sharding <- ZIO.service[Sharding]
64+
builder = grpcConfig.executor match {
65+
case Some(executor) =>
66+
ServerBuilder
67+
.forPort(config.shardingPort)
68+
.executor(executor)
69+
case None =>
70+
ServerBuilder.forPort(config.shardingPort)
71+
}
72+
services <- ServiceList.add(new GrpcShardingService(sharding, config.sendTimeout) {}).bindAll
73+
server: Server = services
74+
.foldLeft(builder) { case (builder0, service) => builder0.addService(service) }
75+
.addService(ProtoReflectionService.newInstance())
76+
.build()
77+
_ <- ZIO.acquireRelease(ZIO.attempt(server.start()))(server =>
78+
ZIO.attemptBlocking {
79+
server.shutdown()
80+
server.awaitTermination(grpcConfig.shutdownTimeout.toMillis, TimeUnit.MILLISECONDS)
81+
server.shutdownNow()
82+
}.ignore
83+
)
7284
} yield ()
7385
}
7486
}

0 commit comments

Comments
 (0)