Skip to content

Commit 5e8afd5

Browse files
authored
Adopt Swift structured concurrency (#7)
* Adopt Swift structured concurrency - Change public API to async/await - Additional c-ares options and errors - Fix bug in A and AAAA queries * Remove usage of Duration * Address feedback comment
1 parent 3fbcc27 commit 5e8afd5

File tree

9 files changed

+611
-537
lines changed

9 files changed

+611
-537
lines changed

Package.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ do {
2121

2222
let package = Package(
2323
name: "swift-async-dns-resolver",
24+
platforms: [
25+
.macOS("13.0"),
26+
],
2427
products: [
2528
.library(name: "AsyncDNSResolver", targets: ["AsyncDNSResolver"]),
2629
],

Sources/AsyncDNSResolver/AsyncDNSResolver.swift

Lines changed: 302 additions & 224 deletions
Large diffs are not rendered by default.

Sources/AsyncDNSResolver/Channel.swift

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftAsyncDNSResolver open source project
44
//
5-
// Copyright (c) 2020 Apple Inc. and the SwiftAsyncDNSResolver project authors
5+
// Copyright (c) 2020-2023 Apple Inc. and the SwiftAsyncDNSResolver project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -13,22 +13,22 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import CAsyncDNSResolver
16-
import Dispatch
1716

1817
// MARK: - ares_channel
1918

20-
class AresChannel {
21-
let pointer: UnsafeMutablePointer<ares_channel?>?
19+
actor AresChannel {
20+
let pointer: UnsafeMutablePointer<ares_channel?>
2221

23-
private var _ares_channel: ares_channel? {
24-
self.pointer?.pointee
22+
private var underlying: ares_channel? {
23+
self.pointer.pointee
2524
}
2625

27-
private let semaphore = DispatchSemaphore(value: 1)
28-
2926
deinit {
30-
ares_destroy(self._ares_channel)
31-
ares_library_cleanup()
27+
Task { [pointer] in
28+
ares_destroy(pointer.pointee)
29+
pointer.deallocate()
30+
ares_library_cleanup()
31+
}
3232
}
3333

3434
init(options: AresOptions) throws {
@@ -52,20 +52,16 @@ class AresChannel {
5252
}
5353

5454
func withChannel(_ body: (ares_channel) -> Void) {
55-
self.semaphore.wait()
56-
defer { self.semaphore.signal() }
57-
58-
guard let _ares_channel = self._ares_channel else {
55+
guard let underlying = self.underlying else {
5956
fatalError("ares_channel not initialized")
6057
}
61-
62-
body(_ares_channel)
58+
body(underlying)
6359
}
6460
}
6561

6662
private func checkAresResult(body: () -> Int32) throws {
6763
let result = body()
6864
guard result == ARES_SUCCESS else {
69-
throw AsyncDNSResolver.Error(code: result, "Failed to initialize channel")
65+
throw AsyncDNSResolver.Error(code: result, "failed to initialize channel")
7066
}
7167
}

Sources/AsyncDNSResolver/Error.swift

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftAsyncDNSResolver open source project
44
//
5-
// Copyright (c) 2020 Apple Inc. and the SwiftAsyncDNSResolver project authors
5+
// Copyright (c) 2020-2023 Apple Inc. and the SwiftAsyncDNSResolver project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -39,61 +39,64 @@ extension AsyncDNSResolver {
3939
case badFlags(String?)
4040
case noName(String?)
4141
case badHints(String?)
42-
case service(String?)
4342
case notInitialized(String?)
43+
case initError(String?)
4444
case cancelled(String?)
45+
case service(String?)
4546
case other(code: Int32, String?)
4647

4748
var description: String {
4849
switch self {
4950
case .noData(let description):
50-
return "No data: \(description ?? "")"
51+
return "no data: \(description ?? "")"
5152
case .invalidQuery(let description):
52-
return "Invalid query: \(description ?? "")"
53+
return "invalid query: \(description ?? "")"
5354
case .serverFailure(let description):
54-
return "Server failure: \(description ?? "")"
55+
return "server failure: \(description ?? "")"
5556
case .notFound(let description):
56-
return "Not found: \(description ?? "")"
57+
return "not found: \(description ?? "")"
5758
case .notImplemented(let description):
58-
return "Not implemented: \(description ?? "")"
59+
return "not implemented: \(description ?? "")"
5960
case .serverRefused(let description):
60-
return "Server refused: \(description ?? "")"
61+
return "server refused: \(description ?? "")"
6162
case .badQuery(let description):
62-
return "Bad query: \(description ?? "")"
63+
return "bad query: \(description ?? "")"
6364
case .badName(let description):
64-
return "Bad name: \(description ?? "")"
65+
return "bad name: \(description ?? "")"
6566
case .badFamily(let description):
66-
return "Bad family: \(description ?? "")"
67+
return "bad family: \(description ?? "")"
6768
case .badResponse(let description):
68-
return "Bad response: \(description ?? "")"
69+
return "bad response: \(description ?? "")"
6970
case .connectionRefused(let description):
70-
return "Connection refused: \(description ?? "")"
71+
return "connection refused: \(description ?? "")"
7172
case .timeout(let description):
72-
return "Timeout: \(description ?? "")"
73+
return "timeout: \(description ?? "")"
7374
case .eof(let description):
7475
return "EOF: \(description ?? "")"
7576
case .fileIO(let description):
76-
return "File IO: \(description ?? "")"
77+
return "file IO: \(description ?? "")"
7778
case .noMemory(let description):
78-
return "No memory: \(description ?? "")"
79+
return "no memory: \(description ?? "")"
7980
case .destruction(let description):
80-
return "Destruction: \(description ?? "")"
81+
return "destruction: \(description ?? "")"
8182
case .badString(let description):
82-
return "Bad string: \(description ?? "")"
83+
return "bad string: \(description ?? "")"
8384
case .badFlags(let description):
84-
return "Bad flags: \(description ?? "")"
85+
return "bad flags: \(description ?? "")"
8586
case .noName(let description):
86-
return "No name: \(description ?? "")"
87+
return "no name: \(description ?? "")"
8788
case .badHints(let description):
88-
return "Bad hints: \(description ?? "")"
89-
case .service(let description):
90-
return "Service: \(description ?? "")"
89+
return "bad hints: \(description ?? "")"
9190
case .notInitialized(let description):
92-
return "Not initialized: \(description ?? "")"
91+
return "not initialized: \(description ?? "")"
92+
case .initError(let description):
93+
return "initialization error: \(description ?? "")"
9394
case .cancelled(let description):
94-
return "Cancelled: \(description ?? "")"
95+
return "cancelled: \(description ?? "")"
96+
case .service(let description):
97+
return "service: \(description ?? "")"
9598
case .other(let code, let description):
96-
return "Other code [\(code)]: \(description ?? "")"
99+
return "other [\(code)]: \(description ?? "")"
97100
}
98101
}
99102
}
@@ -146,12 +149,14 @@ extension AsyncDNSResolver {
146149
self = .noName(description)
147150
case ARES_EBADHINTS:
148151
self = .badHints(description)
149-
case ARES_ESERVICE:
150-
self = .service(description)
151152
case ARES_ENOTINITIALIZED:
152153
self = .notInitialized(description)
154+
case ARES_ELOADIPHLPAPI, ARES_EADDRGETNETWORKPARAMS:
155+
self = .initError(description)
153156
case ARES_ECANCELLED:
154157
self = .cancelled(description)
158+
case ARES_ESERVICE:
159+
self = .service(description)
155160
default:
156161
self = .other(code: code, description)
157162
}
@@ -241,18 +246,22 @@ extension AsyncDNSResolver {
241246
.init(code: .badHints(description))
242247
}
243248

244-
public static func service(_ description: String? = nil) -> Error {
245-
.init(code: .service(description))
246-
}
247-
248249
public static func notInitialized(_ description: String? = nil) -> Error {
249250
.init(code: .notInitialized(description))
250251
}
251252

253+
public static func initError(_ description: String? = nil) -> Error {
254+
.init(code: .initError(description))
255+
}
256+
252257
public static func cancelled(_ description: String? = nil) -> Error {
253258
.init(code: .cancelled(description))
254259
}
255260

261+
public static func service(_ description: String? = nil) -> Error {
262+
.init(code: .service(description))
263+
}
264+
256265
public static func other(code: Int32, _ description: String? = nil) -> Error {
257266
.init(code: .other(code: code, description))
258267
}

0 commit comments

Comments
 (0)