-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support graceful shutdown for gRPC server (#6)
* Use 'SmartLifecycle' to manage lifecycle of gRPC server * Make ktlint happy * Fix server properties can't autowire to service registrar
- Loading branch information
Showing
5 changed files
with
97 additions
and
36 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...grpc-server-starter/src/main/kotlin/com/bybutter/sisyphus/starter/grpc/ServerLifecycle.kt
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,54 @@ | ||
package com.bybutter.sisyphus.starter.grpc | ||
|
||
import io.grpc.Server | ||
import kotlin.concurrent.thread | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.boot.web.server.Shutdown | ||
import org.springframework.context.SmartLifecycle | ||
|
||
class ServerLifecycle(private val server: Server, private val shutdown: Shutdown) : SmartLifecycle { | ||
private var running = false | ||
|
||
override fun getPhase(): Int { | ||
return super.getPhase() - 10 | ||
} | ||
|
||
override fun isRunning(): Boolean { | ||
if (server.isTerminated) return false | ||
if (server.isShutdown) return false | ||
return running | ||
} | ||
|
||
override fun start() { | ||
server.start() | ||
running = true | ||
logger.info("Running gRPC server via netty on port: ${server.port}") | ||
} | ||
|
||
override fun stop() { | ||
server.shutdownNow() | ||
} | ||
|
||
override fun stop(callback: Runnable) { | ||
when (shutdown) { | ||
Shutdown.GRACEFUL -> { | ||
logger.info("Commencing graceful shutdown for gRPC server. Waiting for active requests to complete") | ||
|
||
server.shutdown() | ||
thread(name = "grpc-shutdown") { | ||
server.awaitTermination() | ||
logger.info("Graceful shutdown complete for gRPC server.") | ||
callback.run() | ||
} | ||
} | ||
else -> { | ||
stop() | ||
callback.run() | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private val logger = LoggerFactory.getLogger(ServerLifecycle::class.java) | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
...us-grpc-server-starter/src/main/kotlin/com/bybutter/sisyphus/starter/grpc/ServerRunner.kt
This file was deleted.
Oops, something went wrong.
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
18 changes: 18 additions & 0 deletions
18
...src/main/kotlin/com/bybutter/sisyphus/starter/webflux/AutoGracefulShutdownConfigurator.kt
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,18 @@ | ||
package com.bybutter.sisyphus.starter.webflux | ||
|
||
import java.util.Properties | ||
import org.springframework.boot.SpringApplication | ||
import org.springframework.boot.env.EnvironmentPostProcessor | ||
import org.springframework.boot.web.server.Shutdown | ||
import org.springframework.core.env.ConfigurableEnvironment | ||
import org.springframework.core.env.PropertiesPropertySource | ||
|
||
class AutoGracefulShutdownConfigurator : EnvironmentPostProcessor { | ||
override fun postProcessEnvironment(environment: ConfigurableEnvironment, application: SpringApplication) { | ||
if (environment.containsProperty("server.shutdown")) return | ||
|
||
environment.propertySources.addLast(PropertiesPropertySource("AutoGracefulShutdownConfiguration", Properties().apply { | ||
this["server.shutdown"] = Shutdown.GRACEFUL.name | ||
})) | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
starter/sisyphus-webflux-starter/src/main/resources/META-INF/spring.factories
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
com.bybutter.sisyphus.starter.webflux.SisyphusWebfluxAutoConfiguration | ||
com.bybutter.sisyphus.starter.webflux.SisyphusWebfluxAutoConfiguration | ||
org.springframework.boot.env.EnvironmentPostProcessor=\ | ||
com.bybutter.sisyphus.starter.webflux.AutoGracefulShutdownConfigurator |