Skip to content

Document and constrain WorkQueue.maxConcurrency - #120

Merged
trasch merged 1 commit into
mainfrom
fix/workqueue-concurrency-api
Nov 2, 2026
Merged

trasch merged 1 commit into
mainfrom
fix/workqueue-concurrency-api

Conversation

@trasch

@trasch trasch commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Summary

WorkQueue.maxConcurrency had two ways to set it, an invisible clamp, and undocumented mid-run behaviour. This addresses all three.

Changes

The clamp is now discoverable

public static var concurrencyRange: ClosedRange<Int> { 1 ... 256 }

Callers can validate their own input instead of discovering the clamp after the fact. The property setter and the initializer now share one clamp helper rather than duplicating max(1, min(256, ...)) in two places.

The clamping and mid-run behaviour are documented

/// The value is clamped to ``concurrencyRange``: assigning `1000` stores
/// `256`, assigning `0` stores `1`.
///
/// Changing this while ``start()`` is running is supported. The queue
/// re-reads it each time it refills, so a raised limit takes effect as soon
/// as an in-flight item finishes. Lowering it does not interrupt running
/// `processWork` calls, so the number of concurrent items can briefly
/// exceed the new limit until they complete.

That last paragraph was previously undocumented, and it's the non-obvious part: raising works promptly, lowering is not retroactive.

setMaxConcurrency kept, but justified

I initially removed it, then found it is not actually redundant:

error: actor-isolated property 'maxConcurrency' can not be mutated
       from a nonisolated context

Swift only permits mutating actor state from inside the actor, so a caller outside the actor cannot write queue.maxConcurrency = 8 — the method is the supported path. Its documentation now says so instead of reading as a duplicate:

/// Sets ``maxConcurrency`` from outside the actor.
///
/// The property itself is readable from any context, but Swift only permits
/// mutating actor state from inside the actor, so this method is the
/// supported way to change the limit.

Testing

  • Clamping is asserted against concurrencyRange rather than hard-coded 256/1.
  • New test that raising the limit mid-run actually increases observed parallelism, which is the behaviour the docs now promise. It starts with maxConcurrency: 1, raises to 8 after 50 ms, and asserts the peak concurrent processWork count exceeded 1.

Verified:

init 1000 -> 256
set 0     -> 1
set 8     -> 8
range     = 1...256

194 tests pass, swiftformat --lint clean, DocC builds with --warnings-as-errors.

Breaking changes

None — setMaxConcurrency is retained and concurrencyRange is additive.

Closes #99

- The clamping range is public as WorkQueue.concurrencyRange, so callers can
  validate their own input instead of discovering the clamp after the fact.
- The property and the initializer share one clamp helper instead of
  duplicating `max(1, min(256, ...))`.
- maxConcurrency's documentation now states the clamping and documents the
  previously undocumented mid-run behaviour: raising the limit takes effect
  on the next refill, lowering it does not interrupt running processWork
  calls, so concurrency can briefly exceed the new limit.
- setMaxConcurrency stays, but its documentation explains why it exists
  rather than reading as a redundant duplicate: Swift only permits mutating
  actor state from inside the actor, so it is the supported way to change
  the limit from outside.

Tests
- Clamping is asserted against concurrencyRange instead of hard-coded
  numbers.
- New test that raising the limit mid-run actually increases observed
  parallelism, which is the behaviour the docs now promise.

Fixes #99
@trasch trasch self-assigned this Nov 2, 2026
@trasch trasch added the enhancement New feature or request label Nov 2, 2026
@trasch
trasch merged commit edb2126 into main Nov 2, 2026
5 checks passed
@trasch
trasch deleted the fix/workqueue-concurrency-api branch November 2, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WorkQueue.maxConcurrency has a redundant setter and silently clamps

1 participant