Skip to content

Commit

Permalink
fix(major): [sc-13510] Adopt for Swift 6. (#16)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ser-0xff and ordo-ci authored Feb 11, 2025
1 parent 6972b75 commit 35db381
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 19 additions & 14 deletions Sources/ConsulServiceDiscovery/Consul.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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'
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions Sources/ConsulServiceDiscovery/Service.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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`.
Expand Down

0 comments on commit 35db381

Please sign in to comment.