Skip to content

Commit

Permalink
Workaround DiscardingTaskGroup non-conformance with nightly compilers (
Browse files Browse the repository at this point in the history
  • Loading branch information
weissi authored May 29, 2024
1 parent e62cc88 commit d379584
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
20 changes: 14 additions & 6 deletions Sources/ConnectionPoolModule/ConnectionPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public final class ConnectionPool<

@inlinable
/*private*/ func makeConnection(for request: StateMachine.ConnectionRequest, in taskGroup: inout some TaskGroupProtocol) {
taskGroup.addTask {
taskGroup.addTask_ {
self.observabilityDelegate.startedConnecting(id: request.connectionID)

do {
Expand Down Expand Up @@ -468,7 +468,7 @@ public final class ConnectionPool<
/*private*/ func runKeepAlive(_ connection: Connection, in taskGroup: inout some TaskGroupProtocol) {
self.observabilityDelegate.keepAliveTriggered(id: connection.id)

taskGroup.addTask {
taskGroup.addTask_ {
do {
try await self.keepAliveBehavior.runKeepAlive(for: connection)

Expand Down Expand Up @@ -503,7 +503,7 @@ public final class ConnectionPool<

@inlinable
/*private*/ func runTimer(_ timer: StateMachine.Timer, in poolGroup: inout some TaskGroupProtocol) {
poolGroup.addTask { () async -> () in
poolGroup.addTask_ { () async -> () in
await withTaskGroup(of: TimerRunResult.self, returning: Void.self) { taskGroup in
taskGroup.addTask {
do {
Expand Down Expand Up @@ -587,17 +587,25 @@ extension AsyncStream {

@usableFromInline
protocol TaskGroupProtocol {
mutating func addTask(operation: @escaping @Sendable () async -> Void)
// We need to call this `addTask_` because some Swift versions define this
// under exactly this name and others have different attributes. So let's pick
// a name that doesn't clash anywhere and implement it using the standard `addTask`.
mutating func addTask_(operation: @escaping @Sendable () async -> Void)
}

#if swift(>=5.8) && os(Linux) || swift(>=5.9)
@available(macOS 14.0, iOS 17.0, tvOS 17.0, watchOS 10.0, *)
extension DiscardingTaskGroup: TaskGroupProtocol {}
extension DiscardingTaskGroup: TaskGroupProtocol {
@inlinable
mutating func addTask_(operation: @escaping @Sendable () async -> Void) {
self.addTask(priority: nil, operation: operation)
}
}
#endif

extension TaskGroup<Void>: TaskGroupProtocol {
@inlinable
mutating func addTask(operation: @escaping @Sendable () async -> Void) {
mutating func addTask_(operation: @escaping @Sendable () async -> Void) {
self.addTask(priority: nil, operation: operation)
}
}
14 changes: 7 additions & 7 deletions Tests/ConnectionPoolModuleTests/ConnectionPoolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ConnectionPoolTests: XCTestCase {
// the same connection is reused 1000 times

await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask {
taskGroup.addTask_ {
await pool.run()
}

Expand Down Expand Up @@ -82,14 +82,14 @@ final class ConnectionPoolTests: XCTestCase {
}

await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask {
taskGroup.addTask_ {
await pool.run()
}

let (blockCancelStream, blockCancelContinuation) = AsyncStream.makeStream(of: Void.self)
let (blockConnCreationStream, blockConnCreationContinuation) = AsyncStream.makeStream(of: Void.self)

taskGroup.addTask {
taskGroup.addTask_ {
_ = try? await factory.nextConnectAttempt { _ in
blockCancelContinuation.yield()
var iterator = blockConnCreationStream.makeAsyncIterator()
Expand Down Expand Up @@ -127,7 +127,7 @@ final class ConnectionPoolTests: XCTestCase {
}

await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask {
taskGroup.addTask_ {
await pool.run()
}

Expand Down Expand Up @@ -170,12 +170,12 @@ final class ConnectionPoolTests: XCTestCase {
// the same connection is reused 1000 times

await withTaskGroup(of: Void.self) { taskGroup in
taskGroup.addTask {
taskGroup.addTask_ {
await pool.run()
XCTAssertFalse(hasFinished.compareExchange(expected: false, desired: true, ordering: .relaxed).original)
}

taskGroup.addTask {
taskGroup.addTask_ {
var usedConnectionIDs = Set<Int>()
for _ in 0..<config.maximumConnectionHardLimit {
await factory.nextConnectAttempt { connectionID in
Expand All @@ -192,7 +192,7 @@ final class ConnectionPoolTests: XCTestCase {
let (stream, continuation) = AsyncStream.makeStream(of: Void.self)

for _ in 0..<iterations {
taskGroup.addTask {
taskGroup.addTask_ {
do {
let leasedConnection = try await pool.leaseConnection()
pool.releaseConnection(leasedConnection)
Expand Down

0 comments on commit d379584

Please sign in to comment.