Files
swift-nio-http2/Sources/NIOHTTP2/Timer.swift
T
aryan-25 b6571f3db4 Add a server connection manager (#532)
Motivation:

`NIOHTTP2Handler` currently does not support graceful shutdown because
it does not action upon a `ChannelShouldQuiesceEvent` event. See issue
https://github.com/apple/swift-nio-http2/issues/336.

Per the
[comment](https://github.com/apple/swift-nio-http2/issues/336#issuecomment-2252862397)
on that issue,
[`gRPC-swift-nio-transport`](https://github.com/grpc/grpc-swift-nio-transport)
implements this functionality in a separate channel handler, named
[`ServerConnectionManagementHandler`](https://github.com/grpc/grpc-swift-nio-transport/blob/main/Sources/GRPCNIOTransportCore/Server/Connection/ServerConnectionManagementHandler.swift).
We should introduce this to this repo too.

Modifications:

- Added `ServerConnectionManagementHandler` from
`gRPC-swift-nio-transport` with some slight modifications such as
removing gRPC-specific functionality like policing client pings.
- Added a new `configureAsyncHTTP2Pipeline` helper, which takes a
connection manager configuration as an argument and sets the
connection manager up alongside `NIOHTTP2Handler`.

Result:

Addresses issue https://github.com/apple/swift-nio-http2/issues/336.
2026-02-18 08:08:55 +00:00

68 lines
2.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2026 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
// swift-format-ignore: NoBlockComments
/*
* Copyright 2024, gRPC Authors All rights reserved.
*
* Licensed 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
*
* http://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.
*/
import NIOCore
/// A timer backed by `NIOScheduledCallback`.
struct Timer<Handler: NIOScheduledCallbackHandler>: Sendable where Handler: Sendable {
/// The event loop on which to run this timer.
private let eventLoop: any EventLoop
/// The duration of the timer.
private let duration: TimeAmount
/// The handler to call when the timer fires.
private let handler: Handler
/// The currently scheduled callback if the timer is running.
private var scheduledCallback: NIOScheduledCallback?
init(eventLoop: any EventLoop, duration: TimeAmount, handler: Handler) {
self.eventLoop = eventLoop
self.duration = duration
self.handler = handler
self.scheduledCallback = nil
}
/// Cancel the timer, if it is running.
func cancel() {
guard let scheduledCallback = self.scheduledCallback else { return }
scheduledCallback.cancel()
}
/// Start or restart the timer.
mutating func start() {
self.scheduledCallback?.cancel()
// Only throws if the event loop is shutting down, so we'll just swallow the error here.
self.scheduledCallback = try? self.eventLoop.scheduleCallback(in: self.duration, handler: self.handler)
}
}