mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-25 04:27:48 +00:00
implement a sequence for throttling incoming rates to a minimum spaced duration (#41)
* implement a sequence for throttling incoming rates to a minimum spaced duration * Add delayed emission tests for throttle
This commit is contained in:
1 parent
d96606e7f7
commit
46b3bb088b
2 files changed
+219
No files matched your search
@@ -0,0 +1,92 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2021 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension AsyncSequence {
|
||||
public func throttle<C: Clock, Reduced>(for interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> AsyncThrottleSequence<Self, C, Reduced> {
|
||||
AsyncThrottleSequence(self, interval: interval, clock: clock, reducing: reducing)
|
||||
}
|
||||
|
||||
/*
|
||||
public func throttle<Reduced>(for interval: Duration, reducing: @Sendable @escaping (Reduced?, Element) async -> Reduced) -> AsyncThrottleSequence<Self, ContinuousClock, Reduced> {
|
||||
throttle(for: interval, clock: .continuous, reducing: reducing)
|
||||
}
|
||||
*/
|
||||
|
||||
public func throttle<C: Clock>(for interval: C.Instant.Duration, clock: C, latest: Bool = true) -> AsyncThrottleSequence<Self, C, Element> {
|
||||
throttle(for: interval, clock: clock) { previous, element in
|
||||
if latest {
|
||||
return element
|
||||
} else {
|
||||
return previous ?? element
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
public func throttle(for interval: Duration, latest: Bool = true) -> AsyncThrottleSequence<Self, ContinuousClock, Element> {
|
||||
throttle(for: interval, clock: .continuous, latest: latest)
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public struct AsyncThrottleSequence<Base: AsyncSequence, C: Clock, Reduced> {
|
||||
let base: Base
|
||||
let interval: C.Instant.Duration
|
||||
let clock: C
|
||||
let reducing: @Sendable (Reduced?, Base.Element) async -> Reduced
|
||||
|
||||
init(_ base: Base, interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Base.Element) async -> Reduced) {
|
||||
self.base = base
|
||||
self.interval = interval
|
||||
self.clock = clock
|
||||
self.reducing = reducing
|
||||
}
|
||||
}
|
||||
|
||||
extension AsyncThrottleSequence: Sendable where Base: Sendable, Element: Sendable { }
|
||||
|
||||
extension AsyncThrottleSequence: AsyncSequence {
|
||||
public typealias Element = Reduced
|
||||
|
||||
public struct Iterator: AsyncIteratorProtocol {
|
||||
var base: Base.AsyncIterator
|
||||
let interval: C.Instant.Duration
|
||||
let clock: C
|
||||
let reducing: @Sendable (Reduced?, Base.Element) async -> Reduced
|
||||
|
||||
init(_ base: Base.AsyncIterator, interval: C.Instant.Duration, clock: C, reducing: @Sendable @escaping (Reduced?, Base.Element) async -> Reduced) {
|
||||
self.base = base
|
||||
self.interval = interval
|
||||
self.clock = clock
|
||||
self.reducing = reducing
|
||||
}
|
||||
|
||||
public mutating func next() async rethrows -> Reduced? {
|
||||
var reduced: Reduced?
|
||||
let start = clock.now
|
||||
repeat {
|
||||
guard let element = try await base.next() else {
|
||||
return nil
|
||||
}
|
||||
let reduction = await reducing(reduced, element)
|
||||
if clock.now - start >= interval {
|
||||
return reduction
|
||||
} else {
|
||||
reduced = reduction
|
||||
}
|
||||
} while true
|
||||
}
|
||||
}
|
||||
|
||||
public func makeAsyncIterator() -> Iterator {
|
||||
Iterator(base.makeAsyncIterator(), interval: interval, clock: clock, reducing: reducing)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Async Algorithms open source project
|
||||
//
|
||||
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import AsyncAlgorithms
|
||||
|
||||
final class TestThrottle: XCTestCase {
|
||||
func test_rate_0() {
|
||||
marbleDiagram {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(0), clock: $0.clock)
|
||||
"abcdefghijk|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_rate_0_leading_edge() {
|
||||
marbleDiagram {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(0), clock: $0.clock, latest: false)
|
||||
"abcdefghijk|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_rate_1() {
|
||||
marbleDiagram {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock)
|
||||
"abcdefghijk|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_rate_1_leading_edge() {
|
||||
marbleDiagram {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock, latest: false)
|
||||
"abcdefghijk|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_rate_2() {
|
||||
marbleDiagram {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
|
||||
"-b-d-f-h-j-|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_rate_2_leading_edge() {
|
||||
marbleDiagram {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
|
||||
"-a-c-e-g-i-|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_rate_3() {
|
||||
marbleDiagram {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock)
|
||||
"--c--f--i--|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_rate_3_leading_edge() {
|
||||
marbleDiagram {
|
||||
"abcdefghijk|"
|
||||
$0.inputs[0].throttle(for: .steps(3), clock: $0.clock, latest: false)
|
||||
"--a--d--g--|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_throwing() {
|
||||
marbleDiagram {
|
||||
"abcdef^hijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
|
||||
"-b-d-f^"
|
||||
}
|
||||
}
|
||||
|
||||
func test_throwing_leading_edge() {
|
||||
marbleDiagram {
|
||||
"abcdef^hijk|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock, latest: false)
|
||||
"-a-c-e^"
|
||||
}
|
||||
}
|
||||
|
||||
func test_emission_2_rate_1() {
|
||||
marbleDiagram {
|
||||
"-a-b-c-d-e-f-g-h-i-j-k-|"
|
||||
$0.inputs[0].throttle(for: .steps(1), clock: $0.clock)
|
||||
"-a-b-c-d-e-f-g-h-i-j-k-|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_emission_2_rate_2() {
|
||||
marbleDiagram {
|
||||
"-a-b-c-d-e-f-g-h-i-j-k-|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
|
||||
"-a-b-c-d-e-f-g-h-i-j-k-|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_emission_3_rate_2() {
|
||||
marbleDiagram {
|
||||
"--a--b--c--d--e--f--g|"
|
||||
$0.inputs[0].throttle(for: .steps(2), clock: $0.clock)
|
||||
"--a--b--c--d--e--f--g|"
|
||||
}
|
||||
}
|
||||
|
||||
func test_emission_2_rate_3() {
|
||||
marbleDiagram {
|
||||
"-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---|"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user