1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// A struct wrapping an ``EventLoop`` that ensures all calls to any method on this struct
/// are coming from the event loop.
@usableFromInline
struct IsolatedEventLoop {
@usableFromInline
let _wrapped: EventLoop
@inlinable
internal init(_ eventLoop: EventLoop) {
self._wrapped = eventLoop
}
/// Submit a given task to be executed by the `EventLoop`
@inlinable
func execute(_ task: @escaping () -> Void) {
self._wrapped.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(task)
self._wrapped.execute {
unsafeTransfer.wrappedValue()
}
}
/// Submit a given task to be executed by the `EventLoop`. Once the execution is complete the returned `EventLoopFuture` is notified.
///
/// - parameters:
/// - task: The closure that will be submitted to the `EventLoop` for execution.
/// - returns: `EventLoopFuture` that is notified once the task was executed.
@inlinable
func submit<T>(_ task: @escaping () throws -> T) -> EventLoopFuture<T> {
self._wrapped.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(task)
return self._wrapped.submit {
try unsafeTransfer.wrappedValue()
}
}
/// Schedule a `task` that is executed by this `EventLoop` at the given time.
///
/// - parameters:
/// - task: The synchronous task to run. As with everything that runs on the `EventLoop`, it must not block.
/// - returns: A `Scheduled` object which may be used to cancel the task if it has not yet run, or to wait
/// on the completion of the task.
///
/// - note: You can only cancel a task before it has started executing.
@discardableResult
@inlinable
func scheduleTask<T>(
deadline: NIODeadline,
_ task: @escaping () throws -> T
) -> Scheduled<T> {
self._wrapped.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(task)
return self._wrapped.scheduleTask(deadline: deadline) {
try unsafeTransfer.wrappedValue()
}
}
/// Schedule a `task` that is executed by this `EventLoop` after the given amount of time.
///
/// - parameters:
/// - task: The synchronous task to run. As with everything that runs on the `EventLoop`, it must not block.
/// - returns: A `Scheduled` object which may be used to cancel the task if it has not yet run, or to wait
/// on the completion of the task.
///
/// - note: You can only cancel a task before it has started executing.
/// - note: The `in` value is clamped to a maximum when running on a Darwin-kernel.
@discardableResult
@inlinable
func scheduleTask<T>(
in delay: TimeAmount,
_ task: @escaping () throws -> T
) -> Scheduled<T> {
self._wrapped.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(task)
return self._wrapped.scheduleTask(in: delay) {
try unsafeTransfer.wrappedValue()
}
}
/// Schedule a `task` that is executed by this `EventLoop` at the given time.
///
/// - Note: The `T` must be `Sendable` since the isolation domains of the event loop future returned from `task` and
/// this event loop might differ.
///
/// - parameters:
/// - task: The asynchronous task to run. As with everything that runs on the `EventLoop`, it must not block.
/// - returns: A `Scheduled` object which may be used to cancel the task if it has not yet run, or to wait
/// on the full execution of the task, including its returned `EventLoopFuture`.
///
/// - note: You can only cancel a task before it has started executing.
@discardableResult
@inlinable
func flatScheduleTask<T: Sendable>(
deadline: NIODeadline,
file: StaticString = #file,
line: UInt = #line,
_ task: @escaping () throws -> EventLoopFuture<T>
) -> Scheduled<T> {
self._wrapped.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(task)
return self._wrapped.flatScheduleTask(deadline: deadline, file: file, line: line) {
try unsafeTransfer.wrappedValue()
}
}
/// Returns the wrapped event loop.
@inlinable
func nonisolated() -> any EventLoop {
return self._wrapped
}
}
extension EventLoop {
/// Assumes the calling context is isolated to the event loop.
@usableFromInline
func assumeIsolated() -> IsolatedEventLoop {
IsolatedEventLoop(self)
}
}
extension EventLoopFuture {
/// A struct wrapping an ``EventLoopFuture`` that ensures all calls to any method on this struct
/// are coming from the event loop of the future.
@usableFromInline
struct Isolated {
@usableFromInline
let _wrapped: EventLoopFuture<Value>
/// When the current `EventLoopFuture<Value>` is fulfilled, run the provided callback,
/// which will provide a new `EventLoopFuture`.
///
/// This allows you to dynamically dispatch new asynchronous tasks as phases in a
/// longer series of processing steps. Note that you can use the results of the
/// current `EventLoopFuture<Value>` when determining how to dispatch the next operation.
///
/// This works well when you have APIs that already know how to return `EventLoopFuture`s.
/// You can do something with the result of one and just return the next future:
///
/// ```
/// let d1 = networkRequest(args).future()
/// let d2 = d1.flatMap { t -> EventLoopFuture<NewValue> in
/// . . . something with t . . .
/// return netWorkRequest(args)
/// }
/// d2.whenSuccess { u in
/// NSLog("Result of second request: \(u)")
/// }
/// ```
///
/// Note: In a sense, the `EventLoopFuture<NewValue>` is returned before it's created.
///
/// - parameters:
/// - callback: Function that will receive the value of this `EventLoopFuture` and return
/// a new `EventLoopFuture`.
/// - returns: A future that will receive the eventual value.
@inlinable
func flatMap<NewValue: Sendable>(
_ callback: @escaping (Value) -> EventLoopFuture<NewValue>
) -> EventLoopFuture<NewValue>.Isolated {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(callback)
return self._wrapped.flatMap {
unsafeTransfer.wrappedValue($0)
}.assumeIsolated()
}
/// When the current `EventLoopFuture<Value>` is fulfilled, run the provided callback, which
/// performs a synchronous computation and returns a new value of type `NewValue`. The provided
/// callback may optionally `throw`.
///
/// Operations performed in `flatMapThrowing` should not block, or they will block the entire
/// event loop. `flatMapThrowing` is intended for use when you have a data-driven function that
/// performs a simple data transformation that can potentially error.
///
/// If your callback function throws, the returned `EventLoopFuture` will error.
///
/// - parameters:
/// - callback: Function that will receive the value of this `EventLoopFuture` and return
/// a new value lifted into a new `EventLoopFuture`.
/// - returns: A future that will receive the eventual value.
@inlinable
func flatMapThrowing<NewValue>(
_ callback: @escaping (Value) throws -> NewValue
) -> EventLoopFuture<NewValue>.Isolated {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(callback)
return self._wrapped.flatMapThrowing {
try unsafeTransfer.wrappedValue($0)
}.assumeIsolated()
}
/// When the current `EventLoopFuture<Value>` is in an error state, run the provided callback, which
/// may recover from the error and returns a new value of type `Value`. The provided callback may optionally `throw`,
/// in which case the `EventLoopFuture` will be in a failed state with the new thrown error.
///
/// Operations performed in `flatMapErrorThrowing` should not block, or they will block the entire
/// event loop. `flatMapErrorThrowing` is intended for use when you have the ability to synchronously
/// recover from errors.
///
/// If your callback function throws, the returned `EventLoopFuture` will error.
///
/// - parameters:
/// - callback: Function that will receive the error value of this `EventLoopFuture` and return
/// a new value lifted into a new `EventLoopFuture`.
/// - returns: A future that will receive the eventual value or a rethrown error.
@inlinable
func flatMapErrorThrowing(
_ callback: @escaping (Error) throws -> Value
) -> EventLoopFuture<Value>.Isolated {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(callback)
return self._wrapped.flatMapErrorThrowing {
try unsafeTransfer.wrappedValue($0)
}.assumeIsolated()
}
/// When the current `EventLoopFuture<Value>` is fulfilled, run the provided callback, which
/// performs a synchronous computation and returns a new value of type `NewValue`.
///
/// Operations performed in `map` should not block, or they will block the entire event
/// loop. `map` is intended for use when you have a data-driven function that performs
/// a simple data transformation that cannot error.
///
/// If you have a data-driven function that can throw, you should use `flatMapThrowing`
/// instead.
///
/// ```
/// let future1 = eventually()
/// let future2 = future1.map { T -> U in
/// ... stuff ...
/// return u
/// }
/// let future3 = future2.map { U -> V in
/// ... stuff ...
/// return v
/// }
/// ```
///
/// - parameters:
/// - callback: Function that will receive the value of this `EventLoopFuture` and return
/// a new value lifted into a new `EventLoopFuture`.
/// - returns: A future that will receive the eventual value.
@inlinable
func map<NewValue>(
_ callback: @escaping (Value) -> (NewValue)
) -> EventLoopFuture<NewValue>.Isolated {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(callback)
return self._wrapped.map {
unsafeTransfer.wrappedValue($0)
}.assumeIsolated()
}
/// When the current `EventLoopFuture<Value>` is in an error state, run the provided callback, which
/// may recover from the error by returning an `EventLoopFuture<NewValue>`. The callback is intended to potentially
/// recover from the error by returning a new `EventLoopFuture` that will eventually contain the recovered
/// result.
///
/// If the callback cannot recover it should return a failed `EventLoopFuture`.
///
/// - Note: The `Value` must be `Sendable` since the isolation domains of this future and the future returned from the callback
/// might differ i.e. they might be bound to different event loops.
///
/// - parameters:
/// - callback: Function that will receive the error value of this `EventLoopFuture` and return
/// a new value lifted into a new `EventLoopFuture`.
/// - returns: A future that will receive the recovered value.
@inlinable
func flatMapError(
_ callback: @escaping (Error) -> EventLoopFuture<Value>
) -> EventLoopFuture<Value>.Isolated where Value: Sendable {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(callback)
return self._wrapped.flatMapError {
unsafeTransfer.wrappedValue($0)
}.assumeIsolated()
}
/// When the current `EventLoopFuture<Value>` is fulfilled, run the provided callback, which
/// performs a synchronous computation and returns either a new value (of type `NewValue`) or
/// an error depending on the `Result` returned by the closure.
///
/// Operations performed in `flatMapResult` should not block, or they will block the entire
/// event loop. `flatMapResult` is intended for use when you have a data-driven function that
/// performs a simple data transformation that can potentially error.
///
///
/// - parameters:
/// - body: Function that will receive the value of this `EventLoopFuture` and return
/// a new value or error lifted into a new `EventLoopFuture`.
/// - returns: A future that will receive the eventual value.
@inlinable
func flatMapResult<NewValue, SomeError: Error>(
_ body: @escaping (Value) -> Result<NewValue, SomeError>
) -> EventLoopFuture<NewValue>.Isolated {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(body)
return self._wrapped.flatMapResult {
unsafeTransfer.wrappedValue($0)
}.assumeIsolated()
}
/// When the current `EventLoopFuture<Value>` is in an error state, run the provided callback, which
/// can recover from the error and return a new value of type `Value`. The provided callback may not `throw`,
/// so this function should be used when the error is always recoverable.
///
/// Operations performed in `recover` should not block, or they will block the entire
/// event loop. `recover` is intended for use when you have the ability to synchronously
/// recover from errors.
///
/// - parameters:
/// - callback: Function that will receive the error value of this `EventLoopFuture` and return
/// a new value lifted into a new `EventLoopFuture`.
/// - returns: A future that will receive the recovered value.
@inlinable
func recover(
_ callback: @escaping (Error) -> Value
) -> EventLoopFuture<Value>.Isolated {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(callback)
return self._wrapped.recover {
unsafeTransfer.wrappedValue($0)
}.assumeIsolated()
}
/// Adds an observer callback to this `EventLoopFuture` that is called when the
/// `EventLoopFuture` has a success result.
///
/// An observer callback cannot return a value, meaning that this function cannot be chained
/// from. If you are attempting to create a computation pipeline, consider `map` or `flatMap`.
/// If you find yourself passing the results from this `EventLoopFuture` to a new `EventLoopPromise`
/// in the body of this function, consider using `cascade` instead.
///
/// - parameters:
/// - callback: The callback that is called with the successful result of the `EventLoopFuture`.
@inlinable
func whenSuccess(_ callback: @escaping (Value) -> Void) {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(callback)
return self._wrapped.whenSuccess {
unsafeTransfer.wrappedValue($0)
}
}
/// Adds an observer callback to this `EventLoopFuture` that is called when the
/// `EventLoopFuture` has a failure result.
///
/// An observer callback cannot return a value, meaning that this function cannot be chained
/// from. If you are attempting to create a computation pipeline, consider `recover` or `flatMapError`.
/// If you find yourself passing the results from this `EventLoopFuture` to a new `EventLoopPromise`
/// in the body of this function, consider using `cascade` instead.
///
/// - parameters:
/// - callback: The callback that is called with the failed result of the `EventLoopFuture`.
@inlinable
func whenFailure(_ callback: @escaping (Error) -> Void) {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(callback)
return self._wrapped.whenFailure {
unsafeTransfer.wrappedValue($0)
}
}
/// Adds an observer callback to this `EventLoopFuture` that is called when the
/// `EventLoopFuture` has any result.
///
/// - parameters:
/// - callback: The callback that is called when the `EventLoopFuture` is fulfilled.
@inlinable
func whenComplete(
_ callback: @escaping (Result<Value, Error>) -> Void
) {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(callback)
return self._wrapped.whenComplete {
unsafeTransfer.wrappedValue($0)
}
}
/// Adds an observer callback to this `EventLoopFuture` that is called when the
/// `EventLoopFuture` has any result.
///
/// - parameters:
/// - callback: the callback that is called when the `EventLoopFuture` is fulfilled.
/// - returns: the current `EventLoopFuture`
@inlinable
func always(
_ callback: @escaping (Result<Value, Error>) -> Void
) -> EventLoopFuture<Value> {
self._wrapped.eventLoop.assertInEventLoop()
let unsafeTransfer = UnsafeTransfer(callback)
return self._wrapped.always {
unsafeTransfer.wrappedValue($0)
}
}
/// Unwrap an `EventLoopFuture` where its type parameter is an `Optional`.
///
/// Unwraps a future returning a new `EventLoopFuture` with either: the value passed in the `orReplace`
/// parameter when the future resolved with value Optional.none, or the same value otherwise. For example:
/// ```
/// promise.futureResult.unwrap(orReplace: 42).wait()
/// ```
///
/// - parameters:
/// - orReplace: the value of the returned `EventLoopFuture` when then resolved future's value is `Optional.some()`.
/// - returns: an new `EventLoopFuture` with new type parameter `NewValue` and the value passed in the `orReplace` parameter.
@inlinable
func unwrap<NewValue>(
orReplace replacement: NewValue
) -> EventLoopFuture<NewValue>.Isolated where Value == Optional<NewValue> {
return self.map { (value) -> NewValue in
guard let value = value else {
return replacement
}
return value
}
}
/// Unwrap an `EventLoopFuture` where its type parameter is an `Optional`.
///
/// Unwraps a future returning a new `EventLoopFuture` with either: the value returned by the closure passed in
/// the `orElse` parameter when the future resolved with value Optional.none, or the same value otherwise. For example:
/// ```
/// var x = 2
/// promise.futureResult.unwrap(orElse: { x * 2 }).wait()
/// ```
///
/// - parameters:
/// - orElse: a closure that returns the value of the returned `EventLoopFuture` when then resolved future's value
/// is `Optional.some()`.
/// - returns: an new `EventLoopFuture` with new type parameter `NewValue` and with the value returned by the closure
/// passed in the `orElse` parameter.
@inlinable
func unwrap<NewValue>(
orElse callback: @escaping () -> NewValue
) -> EventLoopFuture<NewValue>.Isolated where Value == Optional<NewValue> {
return self.map { (value) -> NewValue in
guard let value = value else {
return callback()
}
return value
}
}
/// Returns the wrapped event loop future.
@inlinable
func nonisolated() -> EventLoopFuture<Value> {
return self._wrapped
}
}
/// Assumes the calling context is isolated to the future's event loop.
@usableFromInline
func assumeIsolated() -> Isolated {
self.eventLoop.assertInEventLoop()
return Isolated(_wrapped: self)
}
}
extension EventLoopPromise {
/// A struct wrapping an ``EventLoopPromise`` that ensures all calls to any method on this struct
/// are coming from the event loop of the promise.
@usableFromInline
struct Isolated {
@usableFromInline
let _wrapped: EventLoopPromise<Value>
/// Deliver a successful result to the associated `EventLoopFuture<Value>` object.
///
/// - parameters:
/// - value: The successful result of the operation.
@inlinable
func succeed(_ value: Value) {
self._wrapped.futureResult.eventLoop.assertInEventLoop()
self._wrapped._setValue(value: .success(value))._run()
}
/// Complete the promise with the passed in `Result<Value, Error>`.
///
/// This method is equivalent to invoking:
/// ```
/// switch result {
/// case .success(let value):
/// promise.succeed(value)
/// case .failure(let error):
/// promise.fail(error)
/// }
/// ```
///
/// - parameters:
/// - result: The result which will be used to succeed or fail this promise.
@inlinable
func completeWith(_ result: Result<Value, Error>) {
self._wrapped.futureResult.eventLoop.assertInEventLoop()
self._wrapped._setValue(value: result)._run()
}
/// Returns the wrapped event loop promise.
@inlinable
func nonisolated() -> EventLoopPromise<Value> {
return self._wrapped
}
}
/// Assumes the calling context is isolated to the promise's event loop.
@usableFromInline
func assumeIsolated() -> Isolated {
self.futureResult.eventLoop.assertInEventLoop()
return Isolated(_wrapped: self)
}
}
|