-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a way to terminate unfinished requests after graceful shutdown (
#5941) Motivation: Unfinished requests even after graceful shutdown period are forcibly 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. ```java GracefulShutdown gracefulShutdown = GracefulShutdown .builder() .quietPeriod(Duration.ofSeconds(10)) .timeout(Duration.ofSeconds(15)) .shutdownErrorFunction((ctx, req) -> { return new ServerStopException(); }) .build(); Server .builder() .gracefulShutdown(gracefulShutdown); ```
- Loading branch information
Showing
23 changed files
with
741 additions
and
94 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
core/src/main/java/com/linecorp/armeria/common/ShuttingDownException.java
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,47 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.common; | ||
|
||
import com.linecorp.armeria.common.annotation.UnstableApi; | ||
import com.linecorp.armeria.common.util.Sampler; | ||
import com.linecorp.armeria.server.Server; | ||
|
||
/** | ||
* A {@link CancellationException} raised when a {@link Server} cannot handle a request because it's shutting | ||
* down. | ||
*/ | ||
@UnstableApi | ||
public final class ShuttingDownException extends CancellationException { | ||
private static final long serialVersionUID = -4963725400532294491L; | ||
|
||
private static final ShuttingDownException INSTANCE = new ShuttingDownException(false); | ||
|
||
/** | ||
* Returns a singleton {@link ShuttingDownException} or newly-created exception depending on | ||
* the result of {@link Sampler#isSampled(Object)} of {@link Flags#verboseExceptionSampler()}. | ||
*/ | ||
public static ShuttingDownException get() { | ||
return Flags.verboseExceptionSampler().isSampled(ShuttingDownException.class) ? | ||
new ShuttingDownException() : INSTANCE; | ||
} | ||
|
||
private ShuttingDownException() {} | ||
|
||
private ShuttingDownException(@SuppressWarnings("unused") boolean dummy) { | ||
super(null, null, false, false); | ||
} | ||
} |
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
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
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
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
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
82 changes: 82 additions & 0 deletions
82
core/src/main/java/com/linecorp/armeria/server/DefaultGracefulShutdown.java
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,82 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.server; | ||
|
||
import java.time.Duration; | ||
import java.util.Objects; | ||
import java.util.function.BiFunction; | ||
|
||
import com.google.common.base.MoreObjects; | ||
|
||
import com.linecorp.armeria.common.HttpRequest; | ||
|
||
final class DefaultGracefulShutdown implements GracefulShutdown { | ||
|
||
private final Duration quietPeriod; | ||
private final Duration timeout; | ||
private final BiFunction<ServiceRequestContext, HttpRequest, Throwable> toExceptionFunction; | ||
|
||
DefaultGracefulShutdown(Duration quietPeriod, Duration timeout, | ||
BiFunction<ServiceRequestContext, HttpRequest, Throwable> toExceptionFunction) { | ||
this.quietPeriod = quietPeriod; | ||
this.timeout = timeout; | ||
this.toExceptionFunction = toExceptionFunction; | ||
} | ||
|
||
@Override | ||
public Duration quietPeriod() { | ||
return quietPeriod; | ||
} | ||
|
||
@Override | ||
public Duration timeout() { | ||
return timeout; | ||
} | ||
|
||
@Override | ||
public Throwable toException(ServiceRequestContext ctx, HttpRequest request) { | ||
return toExceptionFunction.apply(ctx, request); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof DefaultGracefulShutdown)) { | ||
return false; | ||
} | ||
final DefaultGracefulShutdown that = (DefaultGracefulShutdown) o; | ||
return quietPeriod.equals(that.quietPeriod) && | ||
timeout.equals(that.timeout) && | ||
toExceptionFunction.equals(that.toExceptionFunction); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(quietPeriod, timeout, toExceptionFunction); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("quietPeriod", quietPeriod) | ||
.add("timeout", timeout) | ||
.add("toExceptionFunction", toExceptionFunction) | ||
.toString(); | ||
} | ||
} |
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
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
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
Oops, something went wrong.