mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-26 04:37:48 +00:00
#113 Define the first element of throttled sequences by not exceeding the rate (and fix a expectation misalignment of events) (#133)
* #113 Define the first element of throttled sequences by not exceeding the rate (and fix a expectation misalignment of events) * Update guide for throttle referencing the first element and duration measurement
This commit is contained in:
1 parent
6971c8fc67
commit
18749897a8
4 files changed
+15
-10
No files matched your search
@@ -77,6 +77,8 @@ extension AsyncThrottleSequence.Iterator: Sendable
|
||||
|
||||
The `AsyncThrottleSequence` and its `Iterator` are conditionally `Sendable` if the base types comprising it are `Sendable`.
|
||||
|
||||
The time in which events are measured are from the previous emission if present. If a duration has elapsed between the last emission and the point in time the throttle is measured then that duration is counted as elapsed. The first element is considered not throttled because no interval can be constructed from the start to the first element.
|
||||
|
||||
## Alternatives Considered
|
||||
|
||||
It was considered to only provide the "latest" style APIs, however the reduction version grants more flexibility and can act as a funnel to the implementations of `latest`.
|
||||
|
||||
@@ -64,6 +64,7 @@ extension AsyncThrottleSequence: AsyncSequence {
|
||||
/// The iterator for an `AsyncThrottleSequence` instance.
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
var base: Base.AsyncIterator
|
||||
var last: C.Instant?
|
||||
let interval: C.Instant.Duration
|
||||
let clock: C
|
||||
let reducing: @Sendable (Reduced?, Base.Element) async -> Reduced
|
||||
@@ -77,13 +78,15 @@ extension AsyncThrottleSequence: AsyncSequence {
|
||||
|
||||
public mutating func next() async rethrows -> Reduced? {
|
||||
var reduced: Reduced?
|
||||
let start = clock.now
|
||||
let start = last ?? clock.now
|
||||
repeat {
|
||||
guard let element = try await base.next() else {
|
||||
return nil
|
||||
}
|
||||
let reduction = await reducing(reduced, element)
|
||||
if start.duration(to: clock.now) >= interval {
|
||||
let now = clock.now
|
||||
if start.duration(to: now) >= interval || last == nil {
|
||||
last = now
|
||||
return reduction
|
||||
} else {
|
||||
reduced = reduction
|
||||
|
||||
@@ -42,7 +42,7 @@ extension AsyncSequenceValidationDiagram {
|
||||
}
|
||||
|
||||
func reconstitute<Theme: AsyncSequenceValidationTheme>(_ events: [Clock.Instant : [Result<String?, Error>]], theme: Theme, end: Clock.Instant) -> String {
|
||||
var now = Clock.Instant(when: .zero)
|
||||
var now = Clock.Instant(when: .steps(1)) // adjust for the offset index
|
||||
var reconstituted = ""
|
||||
while now <= end {
|
||||
if let results = events[now] {
|
||||
|
||||
@@ -49,7 +49,7 @@ final class TestThrottle: XCTestCase {
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
|
||||
"-b-d-f-h-j-|"
|
||||
"a-c-e-g-i-k|"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ final class TestThrottle: XCTestCase {
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
|
||||
"-a-c-e-g-i-|"
|
||||
"a-b-d-f-h-j|"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ final class TestThrottle: XCTestCase {
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
|
||||
"--c--f--i--|"
|
||||
"a--d--g--j-|"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ final class TestThrottle: XCTestCase {
|
||||
validate {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: false)
|
||||
"--a--d--g--|"
|
||||
"a--b--e--h-|"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ final class TestThrottle: XCTestCase {
|
||||
validate {
|
||||
"abcdef^hijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
|
||||
"-b-d-f^"
|
||||
"a-c-e-^"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ final class TestThrottle: XCTestCase {
|
||||
validate {
|
||||
"abcdef^hijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
|
||||
"-a-c-e^"
|
||||
"a-b-d-^"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ final class TestThrottle: XCTestCase {
|
||||
validate {
|
||||
"-a-b-c-d-e-f-g-h-i-j-k-|"
|
||||
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
|
||||
"---b---d---f---h---j---|"
|
||||
"-a---c---e---g---i---k-|"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user