From 35db3811c23d6267e6bd75c6602e8e75777c6840 Mon Sep 17 00:00:00 2001 From: ser <122270051+ser-0xff@users.noreply.github.com> Date: Tue, 11 Feb 2025 15:44:05 +0300 Subject: [PATCH] fix(major): [sc-13510] Adopt for Swift 6. (#16) * chore(patch): remove ExtrasJSON and use new JSON from Foundation * remove extra copy * more withUnsafeReadableBytes * fix(major): [sc-13510] Adopt for Swift 6. --------- Co-authored-by: ordo-ci <104988168+ordo-ci@users.noreply.github.com> --- Package.swift | 2 +- Sources/ConsulServiceDiscovery/Consul.swift | 33 +++++++++++--------- Sources/ConsulServiceDiscovery/Service.swift | 5 ++- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Package.swift b/Package.swift index 2ec9b5c..dd94d80 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.8 +// swift-tools-version: 6.0 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription diff --git a/Sources/ConsulServiceDiscovery/Consul.swift b/Sources/ConsulServiceDiscovery/Consul.swift index db7d6f9..3f19ba5 100644 --- a/Sources/ConsulServiceDiscovery/Consul.swift +++ b/Sources/ConsulServiceDiscovery/Consul.swift @@ -370,8 +370,8 @@ public final class Consul: Sendable { } do { - let values = try buffer.withUnsafeReadableBytes { bytes -> [Value] in - return try JSONDecoder().decode([Value].self, from: Data(bytes)) + let values = try buffer.withUnsafeReadableBytes { + try JSONDecoder().decode([Value].self, from: Data($0)) } if values.count > 0 { let value = values[0] @@ -653,22 +653,19 @@ public final class Consul: Sendable { public let kv: KeyValueEndpoint public let session: SessionEndpoint - public var logLevel: Logger.Level { - get { impl.logger.logLevel } - set { impl.logger.logLevel = newValue } - } - final class Impl: Sendable { let serverHost: String let serverPort: Int let eventLoopGroup: EventLoopGroup - var logger: Logger + let logger: Logger - init(_ serverHost: String, _ serverPort: Int, _ eventLoopGroup: EventLoopGroup) { + init(_ serverHost: String, _ serverPort: Int, _ logLevel: Logger.Level, _ eventLoopGroup: EventLoopGroup) { self.serverHost = serverHost self.serverPort = serverPort self.eventLoopGroup = eventLoopGroup - self.logger = Logger(label: "consul") + var logger = Logger(label: "consul") + logger.logLevel = logLevel + self.logger = logger } func request(method requestMethod: HTTPMethod, uri requestURI: String, body requestBody: ByteBuffer?, handler: some ConsulResponseHandler) { @@ -742,7 +739,7 @@ public final class Consul: Sendable { } } - public init(host: String = defaultHost, port: Int = defaultPort) { + public init(host: String = defaultHost, port: Int = defaultPort, logLevel: Logger.Level = .info) { // We use EventLoopFuture<> as a result for most calls, // the problem here is the 'future' is tied to particular event loop, // and from SwiftNIO point of view it is an error if we fill the 'future' @@ -752,7 +749,7 @@ public final class Consul: Sendable { // The only way to workaround that issue now is to use an event loop group // with only one event loop. let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) - impl = Impl(host, port, eventLoopGroup) + impl = Impl(host, port, logLevel, eventLoopGroup) agent = AgentEndpoint(impl) catalog = CatalogEndpoint(impl) kv = KeyValueEndpoint(impl) @@ -764,7 +761,7 @@ public final class Consul: Sendable { } } -private class HTTPHandler: ChannelInboundHandler { +private final class HTTPHandler: @unchecked Sendable, ChannelInboundHandler { typealias InboundIn = HTTPClientResponsePart typealias OutboundOut = HTTPClientRequestPart @@ -778,7 +775,15 @@ private class HTTPHandler: ChannelInboundHandler { private var consulIndex: Int? private let logger: Logger - init(_ serverHost: String, _ serverPort: Int, requestMethod: HTTPMethod, requestURI: String, requestBody: ByteBuffer?, handler: any ConsulResponseHandler, _ logger: Logger) { + init( + _ serverHost: String, + _ serverPort: Int, + requestMethod: HTTPMethod, + requestURI: String, + requestBody: ByteBuffer?, + handler: any ConsulResponseHandler, + _ logger: Logger + ) { self.serverHost = serverHost self.serverPort = serverPort self.requestMethod = requestMethod diff --git a/Sources/ConsulServiceDiscovery/Service.swift b/Sources/ConsulServiceDiscovery/Service.swift index 8643c5f..51c0971 100644 --- a/Sources/ConsulServiceDiscovery/Service.swift +++ b/Sources/ConsulServiceDiscovery/Service.swift @@ -1,6 +1,5 @@ -import Foundation -public struct Service: Codable { +public struct Service: Codable, Sendable { /// Specifies the address of the service. let address: String? /// The list of Consul checks for the service. Cannot be specified with @@ -38,7 +37,7 @@ public struct Service: Codable { } } -public struct Check: Codable { +public struct Check: Codable, Sendable { /// Specifies a body that should be sent with HTTP checks. let body: String? /// The unique ID for this check on the node. Defaults to the check `name`.