Created
May 10, 2020 19:38
-
-
Save jchapuis/1449e2bd6fce3ca3f03479f087b78f26 to your computer and use it in GitHub Desktop.
Markdium-Orchestrating startup and shutdown in 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
object CoordinatedShutdownHttpHelpers { | |
implicit class FluentHttpCoordinatedShutdown(coordinatedShutdown: CoordinatedShutdown)( | |
implicit val system: ActorSystem | |
) { | |
private implicit val executionContext: ExecutionContext = system.dispatcher | |
def addHttpBinding( | |
binding: Http.ServerBinding, | |
pendingRequestsHardDeadline: FiniteDuration, | |
unbindingDelay: FiniteDuration | |
): CoordinatedShutdown = { | |
val addressAndPort = s"${binding.localAddress.getHostString}:${binding.localAddress.getPort}" | |
coordinatedShutdown | |
.serviceUnbind( | |
() => akka.pattern.after(unbindingDelay, system.scheduler)(binding.unbind()), | |
s"http-unbind-$addressAndPort" | |
) | |
.serviceRequestsDone( | |
() => binding.terminate(pendingRequestsHardDeadline).map(_ => Done), | |
s"http-graceful-terminate-$addressAndPort" | |
) | |
.serviceStop(() => Http().shutdownAllConnectionPools().map(_ => Done), s"http-done-$addressAndPort") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment