-
Notifications
You must be signed in to change notification settings - Fork 15
Concurrency #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Concurrency #21
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a1d17b0
changed: swift concurrency
ZirgVoice 4f0f66a
added: Channel
ZirgVoice 39fb823
refactor: sped up tests
ZirgVoice fbc6b47
refactor
ZirgVoice b0faa08
fix: testMultipleRequestsWithMaxBatchSize
ZirgVoice e2e647e
Merge branch 'main' into concurrency
ZirgVoice 49b9d8c
Update Sources/DataLoader/DataLoader.swift
ZirgVoice 36242b7
changed: public to internal and added comment about Task sleep in tests
ZirgVoice 268dfb6
added: sleep constant int tests
ZirgVoice 3d0edae
changed: public actor to internal
ZirgVoice b2ceab5
Revert "changed: public actor to internal"
ZirgVoice 3ac1c75
changed: made a separate product library AsyncDataLoader
ZirgVoice 4db97c3
fix: testMultipleRequestsWithMaxBatchSize
ZirgVoice e6446b4
changed: swift-tools-version to 5.8
ZirgVoice 40bd24f
Revert "changed: swift-tools-version to 5.8"
ZirgVoice 5e5b3fd
changed: swift versions in workflows
ZirgVoice 0def900
fix: formatter
ZirgVoice 0af209a
fix: Package.resolved
ZirgVoice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
// swift-tools-version:5.0 | ||
// swift-tools-version:5.10 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "DataLoader", | ||
platforms: [.macOS(.v12), .iOS(.v15), .tvOS(.v15), .watchOS(.v8)], | ||
products: [ | ||
.library(name: "DataLoader", targets: ["DataLoader"]), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"), | ||
.package(url: "https://github.com/apple/swift-algorithms.git", from: "1.0.0"), | ||
.package(url: "https://github.com/adam-fowler/async-collections", from: "0.0.1"), | ||
], | ||
targets: [ | ||
.target(name: "DataLoader", dependencies: ["NIO", "NIOConcurrencyHelpers"]), | ||
.target( | ||
name: "DataLoader", | ||
dependencies: [ | ||
.product(name: "Algorithms", package: "swift-algorithms"), | ||
.product(name: "AsyncCollections", package: "async-collections"), | ||
] | ||
), | ||
.testTarget(name: "DataLoaderTests", dependencies: ["DataLoader"]), | ||
], | ||
swiftLanguageVersions: [.v5] | ||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
public actor Channel<Success: Sendable, Failure: Error>: Sendable { | ||
private var state = State<Success, Failure>() | ||
} | ||
|
||
public extension Channel { | ||
NeedleInAJayStack marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
@discardableResult | ||
func fulfill(_ value: Success) async -> Bool { | ||
if await state.result == nil { | ||
await state.setResult(result: value) | ||
|
||
for waiters in await state.waiters { | ||
waiters.resume(returning: value) | ||
} | ||
|
||
await state.removeAllWaiters() | ||
|
||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
@discardableResult | ||
func fail(_ failure: Failure) async -> Bool { | ||
if await state.failure == nil { | ||
await state.setFailure(failure: failure) | ||
|
||
for waiters in await state.waiters { | ||
waiters.resume(throwing: failure) | ||
} | ||
|
||
await state.removeAllWaiters() | ||
|
||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
var value: Success { | ||
get async throws { | ||
try await withCheckedThrowingContinuation { continuation in | ||
Task { | ||
if let result = await state.result { | ||
continuation.resume(returning: result) | ||
} else if let failure = await self.state.failure { | ||
continuation.resume(throwing: failure) | ||
} else { | ||
await state.appendWaiters(waiters: continuation) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
typealias Waiter<Success, Failure> = CheckedContinuation<Success, Error> | ||
|
||
actor State<Success, Failure> { | ||
var waiters = [Waiter<Success, Failure>]() | ||
var result: Success? | ||
var failure: Failure? | ||
} | ||
|
||
extension State { | ||
func setResult(result: Success) { | ||
self.result = result | ||
} | ||
|
||
func setFailure(failure: Failure) { | ||
self.failure = failure | ||
} | ||
|
||
func appendWaiters(waiters: Waiter<Success, Failure>...) { | ||
self.waiters.append(contentsOf: waiters) | ||
} | ||
|
||
func removeAllWaiters() { | ||
waiters.removeAll() | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.