Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to configure graceful shutdown timeout #2501

Merged
merged 5 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ jobs:
- name: Check out Vapor
uses: actions/checkout@v2
- name: Run tests with Thread Sanitizer
timeout-minutes: 20
timeout-minutes: 30
run: swift test --enable-test-discovery --sanitize=thread
19 changes: 13 additions & 6 deletions Sources/Vapor/HTTP/Server/HTTPServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public final class HTTPServer: Server {
/// Any uncaught server or responder errors will go here.
public var logger: Logger

/// A time limit to complete a graceful shutdown
public var shutdownTimeout: TimeAmount

public init(
hostname: String = Self.defaultHostname,
port: Int = Self.defaultPort,
Expand All @@ -159,7 +162,8 @@ public final class HTTPServer: Server {
supportVersions: Set<HTTPVersionMajor>? = nil,
tlsConfiguration: TLSConfiguration? = nil,
serverName: String? = nil,
logger: Logger? = nil
logger: Logger? = nil,
shutdownTimeout: TimeAmount = .seconds(10)
) {
self.init(
address: .hostname(hostname, port: port),
Expand All @@ -172,7 +176,8 @@ public final class HTTPServer: Server {
supportVersions: supportVersions,
tlsConfiguration: tlsConfiguration,
serverName: serverName,
logger: logger
logger: logger,
shutdownTimeout: shutdownTimeout
)
}

Expand All @@ -187,7 +192,8 @@ public final class HTTPServer: Server {
supportVersions: Set<HTTPVersionMajor>? = nil,
tlsConfiguration: TLSConfiguration? = nil,
serverName: String? = nil,
logger: Logger? = nil
logger: Logger? = nil,
shutdownTimeout: TimeAmount = .seconds(10)
) {
self.address = address
self.backlog = backlog
Expand All @@ -204,6 +210,7 @@ public final class HTTPServer: Server {
self.tlsConfiguration = tlsConfiguration
self.serverName = serverName
self.logger = logger ?? Logger(label: "codes.vapor.http-server")
self.shutdownTimeout = shutdownTimeout
}
}

Expand Down Expand Up @@ -279,7 +286,7 @@ public final class HTTPServer: Server {
}
self.configuration.logger.debug("Requesting HTTP server shutdown")
do {
try connection.close().wait()
try connection.close(timeout: self.configuration.shutdownTimeout).wait()
} catch {
self.configuration.logger.error("Could not stop HTTP server: \(error)")
}
Expand Down Expand Up @@ -395,9 +402,9 @@ private final class HTTPServerConnection {
self.quiesce = quiesce
}

func close() -> EventLoopFuture<Void> {
func close(timeout: TimeAmount) -> EventLoopFuture<Void> {
let promise = self.channel.eventLoop.makePromise(of: Void.self)
self.channel.eventLoop.scheduleTask(in: .seconds(10)) {
self.channel.eventLoop.scheduleTask(in: timeout) {
promise.fail(Abort(.internalServerError, reason: "Server stop took too long."))
}
self.quiesce.initiateShutdown(promise: promise)
Expand Down