From 209c62f2f2b1eb9af6dcfb4f3d6983aad01af9dc Mon Sep 17 00:00:00 2001 From: Kyle Browning Date: Tue, 20 Aug 2024 11:28:29 -0700 Subject: [PATCH] updating for strict concurrency --- Package.swift | 23 ++++++++++++++++++++--- Sources/APNS/APNSClient.swift | 7 +------ Sources/APNSCore/APNSClient.swift | 3 +++ Sources/APNSExample/Program.swift | 29 ++++++++++++++++++++--------- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/Package.swift b/Package.swift index 27acfe6e..bb0f66a7 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.7 +// swift-tools-version:5.9 import PackageDescription let package = Package( @@ -31,17 +31,25 @@ let package = Package( dependencies: [ .target(name: "APNSCore"), .target(name: "APNS"), - ]), + ] + ), .testTarget( name: "APNSTests", dependencies: [ .target(name: "APNSCore"), .target(name: "APNS"), - ]), + ], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") + ] + ), .target( name: "APNSCore", dependencies: [ .product(name: "Crypto", package: "swift-crypto"), + ], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") ] ), .target( @@ -50,6 +58,9 @@ let package = Package( .product(name: "Crypto", package: "swift-crypto"), .product(name: "AsyncHTTPClient", package: "async-http-client"), .target(name: "APNSCore"), + ], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") ] ), .target( @@ -62,12 +73,18 @@ let package = Package( .product(name: "NIOSSL", package: "swift-nio-ssl"), .product(name: "NIOHTTP1", package: "swift-nio"), .product(name: "NIOHTTP2", package: "swift-nio-http2"), + ], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") ] ), .target( name: "APNSURLSession", dependencies: [ .target(name: "APNSCore"), + ], + swiftSettings: [ + .enableExperimentalFeature("StrictConcurrency") ] ), ] diff --git a/Sources/APNS/APNSClient.swift b/Sources/APNS/APNSClient.swift index 2e648ede..7c318f96 100644 --- a/Sources/APNS/APNSClient.swift +++ b/Sources/APNS/APNSClient.swift @@ -125,14 +125,9 @@ public final class APNSClient Void) { + public func shutdown(queue: DispatchQueue = .global(), callback: @Sendable @escaping (Error?) -> Void) { self.httpClient.shutdown(callback) } - - /// Shuts down the client and `EventLoopGroup` if it was created by the client. - public func syncShutdown() throws { - try self.httpClient.syncShutdown() - } } extension APNSClient: Sendable where Decoder: Sendable, Encoder: Sendable {} diff --git a/Sources/APNSCore/APNSClient.swift b/Sources/APNSCore/APNSClient.swift index 8c1b5a2e..4efe6916 100644 --- a/Sources/APNSCore/APNSClient.swift +++ b/Sources/APNSCore/APNSClient.swift @@ -12,6 +12,9 @@ // //===----------------------------------------------------------------------===// +import Dispatch + public protocol APNSClientProtocol { func send(_ request: APNSRequest) async throws -> APNSResponse + func shutdown(queue: DispatchQueue, callback: @Sendable @escaping (Error?) -> Void) } diff --git a/Sources/APNSExample/Program.swift b/Sources/APNSExample/Program.swift index 6c1c335c..b4406ff2 100644 --- a/Sources/APNSExample/Program.swift +++ b/Sources/APNSExample/Program.swift @@ -14,6 +14,7 @@ import APNSCore import APNS +import OSLog import Foundation @available(macOS 11.0, *) @@ -30,6 +31,7 @@ struct Main { static let teamIdentifier = "" static func main() async throws { + let logger = Logger(subsystem: "apns", category: "apns-main") let client = APNSClient( configuration: .init( authenticationMethod: .jwt( @@ -43,15 +45,24 @@ struct Main { responseDecoder: JSONDecoder(), requestEncoder: JSONEncoder() ) - - try await Self.sendSimpleAlert(with: client) - try await Self.sendLocalizedAlert(with: client) - try await Self.sendThreadedAlert(with: client) - try await Self.sendCustomCategoryAlert(with: client) - try await Self.sendMutableContentAlert(with: client) - try await Self.sendBackground(with: client) - try await Self.sendVoIP(with: client) - try await Self.sendFileProvider(with: client) + do { + try await Self.sendSimpleAlert(with: client) + try await Self.sendLocalizedAlert(with: client) + try await Self.sendThreadedAlert(with: client) + try await Self.sendCustomCategoryAlert(with: client) + try await Self.sendMutableContentAlert(with: client) + try await Self.sendBackground(with: client) + try await Self.sendVoIP(with: client) + try await Self.sendFileProvider(with: client) + } catch { + logger.warning("error sending push:\(error)") + } + + client.shutdown { error in + if let error = error { + logger.warning("error shutting down client: \(error.localizedDescription)") + } + } } }