Simplify async scheduler

This commit is contained in:
Christoph Hagen 2023-11-09 13:12:32 +01:00
parent 39766467e6
commit b8c7256b9d
3 changed files with 6 additions and 30 deletions

View File

@ -8,8 +8,8 @@ let package = Package(
], ],
dependencies: [ dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"), .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
.package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.9.0"), .package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.13.0"),
.package(url: "https://github.com/christophhagen/ClairvoyantVapor", from: "0.2.0"), .package(url: "https://github.com/christophhagen/ClairvoyantVapor", from: "0.5.0"),
.package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"), .package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"),
], ],
targets: [ targets: [

View File

@ -1,34 +1,10 @@
import Foundation import Foundation
import Vapor import Vapor
import NIOCore
import Clairvoyant import Clairvoyant
final class EventLoopScheduler { extension MultiThreadedEventLoopGroup: AsyncScheduler {
private let backgroundGroup: EventLoopGroup public func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
_ = any().makeFutureWithTask(asyncJob)
init() {
backgroundGroup = MultiThreadedEventLoopGroup(numberOfThreads: 2)
}
func next() -> EventLoop {
backgroundGroup.next()
}
func provider() -> NIOEventLoopGroupProvider {
return .shared(backgroundGroup)
}
func shutdown() {
backgroundGroup.shutdownGracefully { _ in
}
}
}
extension EventLoopScheduler: AsyncScheduler {
func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
_ = backgroundGroup.any().makeFutureWithTask(asyncJob)
} }
} }

View File

@ -7,7 +7,7 @@ var deviceManager: DeviceManager!
private var provider: VaporMetricProvider! private var provider: VaporMetricProvider!
private var asyncScheduler = EventLoopScheduler() private var asyncScheduler: AsyncScheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
enum ServerError: Error { enum ServerError: Error {
case invalidAuthenticationFileContent case invalidAuthenticationFileContent