[英]How to gracefully shutdown http4s
I'm using http4s BlazeServer 0.21, how can I graceful shutdown?我正在使用 http4s BlazeServer 0.21,如何正常关机? I want to reject all upcoming requests, and keep process unfinished requests and response back, within a hard shutdown time.我想拒绝所有即将到来的请求,并在硬关闭时间内保持处理未完成的请求和响应。
I tried starting server with serveWhile
and set a shutdownHook SignallingRef
.我尝试使用serveWhile
启动服务器并设置 shutdownHook SignallingRef
。 The server stream & middleware defer as expected (so our metrics & log middleware still log this response)服务器流和中间件按预期延迟(因此我们的指标和日志中间件仍记录此响应)
//serverStream
for {
signal <- fs2.Stream.eval(SignallingRef[F, Boolean](false))
exitCode <- fs2.Stream.eval(Ref[F].of(ExitCode.Success))
_ <- fs2.Stream.eval(shutdown(signal))
server <- BlazeServerBuilder[F]
.bindHttp(8080, "0.0.0.0")
.withHttpApp(httpApp)
.serveWhile(signal, exitCode)
} yield server
def shutdown[F[_]: Effect](interrupter: SignallingRef[F, Boolean]): F[Unit] = {
LiftIO[F].liftIO(IO {
sys.addShutdownHook {
...
interrupter.set(true)
}
})
}
object Server extends IOApp {
def run(args: List[String]): IO[ExitCode] =
serverStream[IO].compile.drain.as(ExitCode.Success)
}
but the http server doesn't work as I expect, seems like http4s's internal ServerChannel
has its own shutdownHook and cancel all the responses already.但是 http 服务器没有像我预期的那样工作,似乎 http4s 的内部ServerChannel
有自己的 shutdownHook 并且已经取消了所有响应。
any suggestion/workaround?任何建议/解决方法? or maybe just a way to hold and don't kill requests for x
seconds is also appreciated.或者也许只是一种在x
秒内保持并且不终止请求的方法也很受欢迎。
The server is hooked for SIGTERM as a matter of convenience.为方便起见,服务器被 SIGTERM 挂钩。
As a convenience, cats-effect provides an cats.effect.IOApp trait with an abstract run method that returns a IO[ExitCode].为方便起见,cats-effect 提供了一个带有抽象 run 方法的 cat.effect.IOApp trait,该方法返回一个 IO[ExitCode]。 An IOApp runs the process and adds a JVM shutdown hook to interrupt the infinite process and gracefully shut down your server when a SIGTERM is received. IOApp 运行该进程并添加 JVM 关闭挂钩以中断无限进程并在收到 SIGTERM 时正常关闭服务器。
And if you want to shutdown using an URL eg http://localhost:8080/ops/shutdown/true
, it works too.如果您想使用 URL 关闭,例如http://localhost:8080/ops/shutdown/true
,它也可以工作。
The code is pretty straight forward,代码很简单,
class SysOpsEndpoints[F[_]: Sync](signal: SignallingRef[F, Boolean]) extends Http4sDsl[F] {
private def shutdown: HttpRoutes[F] =
HttpRoutes.of[F] {
case GET -> Root / "shutdown" / shutdown =>
for {
_ <- signal.set(Try(shutdown.toBoolean).getOrElse(false))
result <- Ok(s"Shutdown: $shutdown")
} yield result
}
}
object SysOpsEndpoints {
def endpoints[F[_]: Sync](signal: SignallingRef[F, Boolean]): HttpRoutes[F] =
new SysOpsEndpoints(signal).shutdown
}
and the setup for the server for-comprehension is similar to yours,并且服务器的设置与您的相似,
for {
signal <- fs2.Stream.eval(SignallingRef[F, Boolean](false))
exitCode <- fs2.Stream.eval(Ref[F].of(ExitCode.Success))
httpApp = Router(
"/ops" -> SysOpsEndpoints.endpoints(signal)
).orNotFound
server <- BlazeServerBuilder[F](serverEc)
.bindHttp(8080, "0.0.0.0")
.withHttpApp(httpApp)
.serveWhile(signal, exitCode)
} yield server
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.