armeria icon indicating copy to clipboard operation
armeria copied to clipboard

Provide a way to terminate unfinished requests after graceful shutdown

Open ikhoon opened this issue 1 year ago • 1 comments

Motivation:

Unfinished requests even after graceful shutdown period are forcivily closed with ClosedSessionException. As ClosedSessionException indicates that the connection was unexpectedly disconnected, ClosedSessionException is not suitable for graceful shutdown.

In this PR, I propose to add ShuttingDownException to terminate unfinished requests when a server stops.

Modifications:

  • Introduce GracefulShutdown to customize graceful shutdown behavior.
    • Users can specify a error function to create an exception to unfinished terminate requests.
  • Fixed HttpServerHandler to send error responses using the error function of GracefulShutdown
  • Fixed Server to send error respones first and then close the connnections.
  • Deprecation) ServerConfig.gracefulShutdownQuietPeriod() and ServerConfig.gracefulShutdownTimeout() have been deprecated in favor of ServerConfig.gracefulShutdown().

Result:

You can now use GracefulShutdown to terminate unfinished requests when a server stops.

GracefulShutdown gracefulShutdown =
  GracefulShutdown
    .builder()
    .quietPeriod(Duration.ofSeconds(10))
    .timeout(Duration.ofSeconds(15))
    .shutdownErrorFunction((ctx, req) -> {
        return new ServerStopException();
    })
    .build();

Server
  .builder()
  .gracefulShutdown(gracefulShutdown);

ikhoon avatar Oct 16 '24 06:10 ikhoon

🔍 Build Scan® (commit: f3f9b1f717fce5b6b0dae9049ec1de54b3662e3a)

Job name Status Build Scan®
build-ubicloud-standard-8-jdk-8 https://ge.armeria.dev/s/ytcuyvikygk56
build-ubicloud-standard-8-jdk-21-snapshot-blockhound https://ge.armeria.dev/s/aps7ijzua3p2g
build-ubicloud-standard-8-jdk-17-min-java-17-coverage https://ge.armeria.dev/s/yeml4gvwm77ru
build-ubicloud-standard-8-jdk-17-min-java-11 https://ge.armeria.dev/s/voyhpybbh5sj4
build-ubicloud-standard-8-jdk-17-leak https://ge.armeria.dev/s/dynijahxidow2
build-ubicloud-standard-8-jdk-11 ❌ (failure) https://ge.armeria.dev/s/ldqtccbuke3sk
build-macos-latest-jdk-21 https://ge.armeria.dev/s/ieamd53bc3tz6

github-actions[bot] avatar Oct 16 '24 06:10 github-actions[bot]

Changed the target milestone to 1.32.0. I need some time to figure out how to implement the API that Trustin suggested.

ikhoon avatar Nov 07 '24 08:11 ikhoon