@@ -43,22 +43,51 @@ public actor WorkQueue<Work: Sendable, Result: Sendable> {
4343 private let processWork: @Sendable (Work) async throws -> Result?
4444 private let onResult: @Sendable (Work, Result?) async throws -> Void
4545
46+ /// The range ``maxConcurrency`` is clamped to.
47+ ///
48+ /// Values outside this range are clamped rather than rejected, so callers
49+ /// that need to validate their own input can check against it first.
50+ public static var concurrencyRange: ClosedRange<Int> {
51+ 1 ... 256
52+ }
53+
4654 private var _maxConcurrency: Int
4755 private var inFlight = 0
4856
49- /// The maximum number of concurrently processed work items (1...256).
57+ /// The maximum number of concurrently processed work items.
58+ ///
59+ /// The value is clamped to ``concurrencyRange``: assigning `1000` stores
60+ /// `256`, assigning `0` stores `1`. Check against ``concurrencyRange``
61+ /// before assigning if the exact value matters.
62+ ///
63+ /// Changing this while ``start()`` is running is supported. The queue
64+ /// re-reads it each time it refills, so a raised limit takes effect as
65+ /// soon as an in-flight item finishes. Lowering it does not interrupt
66+ /// running `processWork` calls, so the number of concurrent items can
67+ /// briefly exceed the new limit until they complete.
5068 public var maxConcurrency: Int {
5169 get { _maxConcurrency }
52- set { _maxConcurrency = max(1, min(256, newValue)) }
70+ set { _maxConcurrency = Self.clamp(newValue) }
71+ }
72+
73+ /// Clamps `value` into ``concurrencyRange``.
74+ private static func clamp(_ value: Int) -> Int {
75+ min(max(value, concurrencyRange.lowerBound), concurrencyRange.upperBound)
5376 }
5477
55- /// Sets the maximum concurrency, clamped to 1...256.
78+ /// Sets ``maxConcurrency`` from outside the actor.
79+ ///
80+ /// The property itself is readable from any context, but Swift only
81+ /// permits mutating actor state from inside the actor, so this method is
82+ /// the supported way to change the limit. It clamps to
83+ /// ``concurrencyRange`` exactly like the setter does.
5684 public func setMaxConcurrency(_ value: Int) {
5785 maxConcurrency = value
5886 }
5987
6088 /// - Parameters:
61- /// - maxConcurrency: Upper bound for parallel `processWork` calls.
89+ /// - maxConcurrency: Upper bound for parallel `processWork` calls,
90+ /// clamped to ``concurrencyRange``.
6291 /// - fetchWork: Produces the next work item, or `nil` when the queue
6392 /// should drain and finish. Called on the actor.
6493 /// - processWork: Transforms a work item into a result. Runs detached
@@ -72,7 +101,7 @@ public actor WorkQueue<Work: Sendable, Result: Sendable> {
72101 processWork: @escaping @Sendable (Work) async throws -> Result?,
73102 onResult: @escaping @Sendable (Work, Result?) async throws -> Void,
74103 ) {
75- self._maxConcurrency = max(1, min(256, maxConcurrency) )
104+ self._maxConcurrency = Self.clamp( maxConcurrency)
76105 self.fetchWork = fetchWork
77106 self.processWork = processWork
78107 self.onResult = onResult
0 commit comments