TokenBucket
Type modeled after a “token bucket” pattern, which is similar to a semaphore, but is built with Swift Concurrency primitives.
actor TokenBucket
Type modeled after a “token bucket” pattern, which is similar to a semaphore, but is built with Swift Concurrency primitives.
actor TokenBucket
import Basics
protocol Actor : AnyObject, Sendable
Common protocol to which all actors conform.
protocol Sendable
init(tokens: Int)
func withToken<ReturnType>(_ body: () async throws -> ReturnType) async rethrows -> ReturnType where ReturnType : Sendable
Executes an async
closure immediately when a token is available. Only the same number of closures will be executed concurrently as the number of tokens
passed to init(tokens:)
, all subsequent invocations of withToken
will suspend until a “free” token is available.
@backDeployed(before: macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0)
nonisolated func assertIsolated(_ message: @autoclosure () -> String = String(), file: StaticString = #fileID, line: UInt = #line)
Stops program execution if the current task is not executing on this actor’s serial executor.
nonisolated func assumeIsolated<T>(_ operation: (isolated Self) throws -> T, file: StaticString = #fileID, line: UInt = #line) rethrows -> T where T : Sendable
Assume that the current task is executing on this actor’s serial executor, or stop program execution otherwise.
@backDeployed(before: macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0)
nonisolated func preconditionIsolated(_ message: @autoclosure () -> String = String(), file: StaticString = #fileID, line: UInt = #line)
Stops program execution if the current task is not executing on this actor’s serial executor.