Skip to content

Commit 046cd44

Browse files
committed
Hide enum, use OID
1 parent 191e0fe commit 046cd44

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

Sources/DistributedActors/ClusterSystem.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,28 +1491,24 @@ public struct ClusterInvocationResultHandler: DistributedTargetInvocationResultH
14911491

14921492
case .remoteCall(let system, let callID, let channel, let recipient):
14931493
system.log.debug("Result handler, onThrow: \(error)", metadata: ["call/id": "\(callID)"])
1494+
1495+
let errorType = type(of: error as Any)
14941496
let reply: RemoteCallReply<_Done>
1497+
14951498
if let codableError = error as? (Error & Codable) {
1496-
switch system.settings.remoteCall.codableErrorAllowance {
1497-
case .custom(let allowedTypes) where errorTypeAllowed(error: error, allowedTypes: allowedTypes):
1499+
switch system.settings.remoteCall.codableErrorAllowance.underlying {
1500+
case .custom(let allowedTypeOIDs) where allowedTypeOIDs.contains(ObjectIdentifier(errorType)):
14981501
reply = .init(callID: callID, error: codableError)
14991502
case .all: // compiler gets confused if this is grouped together with above
15001503
reply = .init(callID: callID, error: codableError)
15011504
default:
1502-
reply = .init(callID: callID, error: GenericRemoteCallError(message: "Remote call error of [\(type(of: error as Any))] type occurred"))
1505+
reply = .init(callID: callID, error: GenericRemoteCallError(message: "Remote call error of [\(errorType)] type occurred"))
15031506
}
15041507
} else {
1505-
reply = .init(callID: callID, error: GenericRemoteCallError(message: "Remote call error of [\(type(of: error as Any))] type occurred"))
1508+
reply = .init(callID: callID, error: GenericRemoteCallError(message: "Remote call error of [\(errorType)] type occurred"))
15061509
}
15071510
try await channel.writeAndFlush(TransportEnvelope(envelope: Payload(payload: .message(reply)), recipient: recipient))
15081511
}
1509-
1510-
func errorTypeAllowed<Err>(error: Err, allowedTypes: [(Error & Codable).Type]) -> Bool {
1511-
func isErrorOfType<Err, T: Error & Codable>(_ error: Err, _: T.Type) -> Bool {
1512-
error is T // `is` and `as` don't work with stored type
1513-
}
1514-
return allowedTypes.contains { type in isErrorOfType(error, type) }
1515-
}
15161512
}
15171513
}
15181514

Sources/DistributedActors/ClusterSystemSettings.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,17 +461,33 @@ extension ClusterSystemSettings {
461461
/// Set to `.effectivelyInfinite` to avoid setting a timeout, although this is not recommended.
462462
public var defaultTimeout: Duration = .seconds(5)
463463

464-
public var codableErrorAllowance: CodableErrorAllowance = .all
464+
public var codableErrorAllowance: CodableErrorAllowanceSettings = .all
465+
466+
public struct CodableErrorAllowanceSettings {
467+
internal enum CodableErrorAllowance {
468+
case none
469+
case all
470+
// OIDs of allowed types
471+
case custom(Set<ObjectIdentifier>)
472+
}
473+
474+
internal let underlying: CodableErrorAllowance
475+
476+
internal init(allowance: CodableErrorAllowance) {
477+
self.underlying = allowance
478+
}
465479

466-
public enum CodableErrorAllowance {
467480
/// All ``Codable`` errors will be converted to ``GenericRemoteCallError``.
468-
case none
481+
public static let none: CodableErrorAllowanceSettings = .init(allowance: .none)
469482

470483
/// All ``Codable`` errors will be returned as-is.
471-
case all
484+
public static let all: CodableErrorAllowanceSettings = .init(allowance: .all)
472485

473486
/// Only the indicated ``Codable`` errors are allowed. Others are converted to ``GenericRemoteCallError``.
474-
case custom([(Error & Codable).Type])
487+
public static func custom(allowedTypes: [(Error & Codable).Type]) -> CodableErrorAllowanceSettings {
488+
let oids = allowedTypes.map { ObjectIdentifier($0) }
489+
return .init(allowance: .custom(Set(oids)))
490+
}
475491
}
476492
}
477493
}

Sources/DistributedActors/Plugins/ClusterSingleton/ClusterSingletonSettings.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ public struct ClusterSingletonSettings {
4141
/// Singleton node allocation strategies.
4242
public struct AllocationStrategySettings {
4343
private enum AllocationStrategy {
44-
/// Singletons will run on the cluster leader. *All* nodes are potential candidates.
4544
case byLeadership
4645
}
4746

48-
private var allocationStrategy: AllocationStrategy
47+
private let allocationStrategy: AllocationStrategy
4948

5049
private init(allocationStrategy: AllocationStrategy) {
5150
self.allocationStrategy = allocationStrategy
@@ -58,5 +57,6 @@ public struct AllocationStrategySettings {
5857
}
5958
}
6059

60+
/// Singletons will run on the cluster leader. *All* nodes are potential candidates.
6161
public static let byLeadership: AllocationStrategySettings = .init(allocationStrategy: .byLeadership)
6262
}

Tests/DistributedActorsTests/RemoteCallTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,12 @@ final class RemoteCallTests: ClusteredActorSystemsXCTestCase {
238238

239239
func test_remoteCall_customCodableErrorAllowList_errorInList() async throws {
240240
let local = await setUpNode("local") { settings in
241-
settings.remoteCall.codableErrorAllowance = .custom([GreeterCodableError.self, AnotherGreeterCodableError.self])
241+
settings.remoteCall.codableErrorAllowance = .custom(allowedTypes: [GreeterCodableError.self, AnotherGreeterCodableError.self])
242242
settings.serialization.registerInbound(GreeterCodableError.self)
243243
settings.serialization.registerInbound(AnotherGreeterCodableError.self)
244244
}
245245
let remote = await setUpNode("remote") { settings in
246-
settings.remoteCall.codableErrorAllowance = .custom([GreeterCodableError.self, AnotherGreeterCodableError.self])
246+
settings.remoteCall.codableErrorAllowance = .custom(allowedTypes: [GreeterCodableError.self, AnotherGreeterCodableError.self])
247247
settings.serialization.registerInbound(GreeterCodableError.self)
248248
settings.serialization.registerInbound(AnotherGreeterCodableError.self)
249249
}
@@ -262,12 +262,12 @@ final class RemoteCallTests: ClusteredActorSystemsXCTestCase {
262262

263263
func test_remoteCall_customCodableErrorAllowList_errorNotInList() async throws {
264264
let local = await setUpNode("local") { settings in
265-
settings.remoteCall.codableErrorAllowance = .custom([AnotherGreeterCodableError.self])
265+
settings.remoteCall.codableErrorAllowance = .custom(allowedTypes: [AnotherGreeterCodableError.self])
266266
settings.serialization.registerInbound(GreeterCodableError.self)
267267
settings.serialization.registerInbound(AnotherGreeterCodableError.self)
268268
}
269269
let remote = await setUpNode("remote") { settings in
270-
settings.remoteCall.codableErrorAllowance = .custom([AnotherGreeterCodableError.self])
270+
settings.remoteCall.codableErrorAllowance = .custom(allowedTypes: [AnotherGreeterCodableError.self])
271271
settings.serialization.registerInbound(GreeterCodableError.self)
272272
settings.serialization.registerInbound(AnotherGreeterCodableError.self)
273273
}

0 commit comments

Comments
 (0)