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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
import DequeModule
import NIOConcurrencyHelpers
/// This is an `AsyncSequence` that supports a unicast `AsyncIterator`.
///
/// The goal of this sequence is to produce a stream of elements from the _synchronous_ world
/// (e.g. elements from a ``Channel`` pipeline) and vend it to the _asynchronous_ world for consumption.
/// Furthermore, it provides facilities to implement a back-pressure strategy which
/// observes the number of elements that are yielded and consumed. This allows to signal the source to request more data.
///
/// - Note: It is recommended to never directly expose this type from APIs, but rather wrap it. This is due to the fact that
/// this type has three generic parameters where at least two should be known statically and it is really awkward to spell out this type.
/// Moreover, having a wrapping type allows to optimize this to specialized calls if all generic types are known.
///
/// - Important: This sequence is a unicast sequence that only supports a single ``NIOThrowingAsyncSequenceProducer/AsyncIterator``.
/// If you try to create more than one iterator it will result in a `fatalError`.
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public struct NIOThrowingAsyncSequenceProducer<
Element: Sendable,
Failure: Error,
Strategy: NIOAsyncSequenceProducerBackPressureStrategy,
Delegate: NIOAsyncSequenceProducerDelegate
>: Sendable {
/// Simple struct for the return type of ``NIOThrowingAsyncSequenceProducer/makeSequence(elementType:failureType:backPressureStrategy:delegate:)-8qauq``.
///
/// This struct contains two properties:
/// 1. The ``source`` which should be retained by the producer and is used
/// to yield new elements to the sequence.
/// 2. The ``sequence`` which is the actual `AsyncSequence` and
/// should be passed to the consumer.
public struct NewSequence {
/// The source of the ``NIOThrowingAsyncSequenceProducer`` used to yield and finish.
public let source: Source
/// The actual sequence which should be passed to the consumer.
public let sequence: NIOThrowingAsyncSequenceProducer
@usableFromInline
/* fileprivate */ internal init(
source: Source,
sequence: NIOThrowingAsyncSequenceProducer
) {
self.source = source
self.sequence = sequence
}
}
/// This class is needed to hook the deinit to observe once all references to the ``NIOThrowingAsyncSequenceProducer`` are dropped.
///
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
@usableFromInline
/* fileprivate */ internal final class InternalClass: Sendable {
@usableFromInline
internal let _storage: Storage
@inlinable
init(storage: Storage) {
self._storage = storage
}
@inlinable
deinit {
_storage.sequenceDeinitialized()
}
}
@usableFromInline
/* private */ internal let _internalClass: InternalClass
@usableFromInline
/* private */ internal var _storage: Storage {
self._internalClass._storage
}
/// Initializes a new ``NIOThrowingAsyncSequenceProducer`` and a ``NIOThrowingAsyncSequenceProducer/Source``.
///
/// - Important: This method returns a struct containing a ``NIOThrowingAsyncSequenceProducer/Source`` and
/// a ``NIOThrowingAsyncSequenceProducer``. The source MUST be held by the caller and
/// used to signal new elements or finish. The sequence MUST be passed to the actual consumer and MUST NOT be held by the
/// caller. This is due to the fact that deiniting the sequence is used as part of a trigger to terminate the underlying source.
///
/// - Parameters:
/// - elementType: The element type of the sequence.
/// - failureType: The failure type of the sequence. Must be `Swift.Error`
/// - backPressureStrategy: The back-pressure strategy of the sequence.
/// - finishOnDeinit: Indicates if ``NIOThrowingAsyncSequenceProducer/Source/finish()`` should be called on deinit of the.
/// We do not recommend to rely on deinit based resource tear down.
/// - delegate: The delegate of the sequence
/// - Returns: A ``NIOThrowingAsyncSequenceProducer/Source`` and a ``NIOThrowingAsyncSequenceProducer``.
@inlinable
public static func makeSequence(
elementType: Element.Type = Element.self,
failureType: Failure.Type = Error.self,
backPressureStrategy: Strategy,
finishOnDeinit: Bool,
delegate: Delegate
) -> NewSequence where Failure == Error {
let sequence = Self(
backPressureStrategy: backPressureStrategy,
delegate: delegate
)
let source = Source(storage: sequence._storage, finishOnDeinit: finishOnDeinit)
return .init(source: source, sequence: sequence)
}
/// Initializes a new ``NIOThrowingAsyncSequenceProducer`` and a ``NIOThrowingAsyncSequenceProducer/Source``.
///
/// - Important: This method returns a struct containing a ``NIOThrowingAsyncSequenceProducer/Source`` and
/// a ``NIOThrowingAsyncSequenceProducer``. The source MUST be held by the caller and
/// used to signal new elements or finish. The sequence MUST be passed to the actual consumer and MUST NOT be held by the
/// caller. This is due to the fact that deiniting the sequence is used as part of a trigger to terminate the underlying source.
///
/// - Parameters:
/// - elementType: The element type of the sequence.
/// - failureType: The failure type of the sequence.
/// - backPressureStrategy: The back-pressure strategy of the sequence.
/// - delegate: The delegate of the sequence
/// - Returns: A ``NIOThrowingAsyncSequenceProducer/Source`` and a ``NIOThrowingAsyncSequenceProducer``.
@available(*, deprecated, message: "Support for a generic Failure type is deprecated. Failure type must be `any Swift.Error`.")
@inlinable
public static func makeSequence(
elementType: Element.Type = Element.self,
failureType: Failure.Type = Failure.self,
backPressureStrategy: Strategy,
delegate: Delegate
) -> NewSequence {
let sequence = Self(
backPressureStrategy: backPressureStrategy,
delegate: delegate
)
let source = Source(storage: sequence._storage, finishOnDeinit: true)
return .init(source: source, sequence: sequence)
}
/// Initializes a new ``NIOThrowingAsyncSequenceProducer`` and a ``NIOThrowingAsyncSequenceProducer/Source``.
///
/// - Important: This method returns a struct containing a ``NIOThrowingAsyncSequenceProducer/Source`` and
/// a ``NIOThrowingAsyncSequenceProducer``. The source MUST be held by the caller and
/// used to signal new elements or finish. The sequence MUST be passed to the actual consumer and MUST NOT be held by the
/// caller. This is due to the fact that deiniting the sequence is used as part of a trigger to terminate the underlying source.
///
/// - Parameters:
/// - elementType: The element type of the sequence.
/// - failureType: The failure type of the sequence. Must be `Swift.Error`
/// - backPressureStrategy: The back-pressure strategy of the sequence.
/// - delegate: The delegate of the sequence
/// - Returns: A ``NIOThrowingAsyncSequenceProducer/Source`` and a ``NIOThrowingAsyncSequenceProducer``.
@inlinable
@available(*, deprecated, renamed: "makeSequence(elementType:failureType:backPressureStrategy:finishOnDeinit:delegate:)", message: "This method has been deprecated since it defaults to deinit based resource teardown")
public static func makeSequence(
elementType: Element.Type = Element.self,
failureType: Failure.Type = Error.self,
backPressureStrategy: Strategy,
delegate: Delegate
) -> NewSequence where Failure == Error {
let sequence = Self(
backPressureStrategy: backPressureStrategy,
delegate: delegate
)
let source = Source(storage: sequence._storage, finishOnDeinit: true)
return .init(source: source, sequence: sequence)
}
/// only used internally by``NIOAsyncSequenceProducer`` to reuse most of the code
@inlinable
internal static func makeNonThrowingSequence(
elementType: Element.Type = Element.self,
backPressureStrategy: Strategy,
finishOnDeinit: Bool,
delegate: Delegate
) -> NewSequence where Failure == Never {
let sequence = Self(
backPressureStrategy: backPressureStrategy,
delegate: delegate
)
let source = Source(storage: sequence._storage, finishOnDeinit: finishOnDeinit)
return .init(source: source, sequence: sequence)
}
@inlinable
/* private */ internal init(
backPressureStrategy: Strategy,
delegate: Delegate
) {
let storage = Storage(
backPressureStrategy: backPressureStrategy,
delegate: delegate
)
self._internalClass = .init(storage: storage)
}
}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension NIOThrowingAsyncSequenceProducer: AsyncSequence {
public func makeAsyncIterator() -> AsyncIterator {
AsyncIterator(storage: self._internalClass._storage)
}
}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension NIOThrowingAsyncSequenceProducer {
public struct AsyncIterator: AsyncIteratorProtocol {
/// This class is needed to hook the deinit to observe once all references to an instance of the ``AsyncIterator`` are dropped.
///
/// If we get move-only types we should be able to drop this class and use the `deinit` of the ``AsyncIterator`` struct itself.
@usableFromInline
/* private */ internal final class InternalClass: Sendable {
@usableFromInline
/* private */ internal let _storage: Storage
fileprivate init(storage: Storage) {
self._storage = storage
self._storage.iteratorInitialized()
}
@inlinable
deinit {
self._storage.iteratorDeinitialized()
}
@inlinable
/* fileprivate */ internal func next() async throws -> Element? {
try await self._storage.next()
}
}
@usableFromInline
/* private */ internal let _internalClass: InternalClass
fileprivate init(storage: Storage) {
self._internalClass = InternalClass(storage: storage)
}
@inlinable
public func next() async throws -> Element? {
try await self._internalClass.next()
}
}
}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension NIOThrowingAsyncSequenceProducer {
/// A struct to interface between the synchronous code of the producer and the asynchronous consumer.
/// This type allows the producer to synchronously `yield` new elements to the ``NIOThrowingAsyncSequenceProducer``
/// and to `finish` the sequence.
///
/// - Note: This struct has reference semantics. Once all copies of a source have been dropped ``NIOThrowingAsyncSequenceProducer/Source/finish()``.
/// This will resume any suspended continuation.
public struct Source {
/// This class is needed to hook the deinit to observe once all references to the ``NIOThrowingAsyncSequenceProducer/Source`` are dropped.
///
/// - Important: This is safe to be unchecked ``Sendable`` since the `storage` is ``Sendable`` and `immutable`.
@usableFromInline
/* fileprivate */ internal final class InternalClass: Sendable {
@usableFromInline
internal let _storage: Storage
@usableFromInline
internal let _finishOnDeinit: Bool
@inlinable
init(storage: Storage, finishOnDeinit: Bool) {
self._storage = storage
self._finishOnDeinit = finishOnDeinit
}
@inlinable
deinit {
if !self._finishOnDeinit && !self._storage.isFinished {
preconditionFailure("Deinited NIOAsyncSequenceProducer.Source without calling source.finish()")
} else {
// We need to call finish here to resume any suspended continuation.
self._storage.finish(nil)
}
}
}
@usableFromInline
/* private */ internal let _internalClass: InternalClass
@usableFromInline
/* private */ internal var _storage: Storage {
self._internalClass._storage
}
@usableFromInline
/* fileprivate */ internal init(storage: Storage, finishOnDeinit: Bool) {
self._internalClass = .init(storage: storage, finishOnDeinit: finishOnDeinit)
}
/// The result of a call to ``NIOThrowingAsyncSequenceProducer/Source/yield(_:)``.
public enum YieldResult: Hashable {
/// Indicates that the caller should produce more elements.
case produceMore
/// Indicates that the caller should stop producing elements.
case stopProducing
/// Indicates that the yielded elements have been dropped because the sequence already terminated.
case dropped
}
/// Yields a sequence of new elements to the ``NIOThrowingAsyncSequenceProducer``.
///
/// If there is an ``NIOThrowingAsyncSequenceProducer/AsyncIterator`` awaiting the next element, it will get resumed right away.
/// Otherwise, the element will get buffered.
///
/// If the ``NIOThrowingAsyncSequenceProducer`` is terminated this will drop the elements
/// and return ``YieldResult/dropped``.
///
/// This can be called more than once and returns to the caller immediately
/// without blocking for any awaiting consumption from the iteration.
///
/// - Parameter contentsOf: The sequence to yield.
/// - Returns: A ``NIOThrowingAsyncSequenceProducer/Source/YieldResult`` that indicates if the yield was successful
/// and if more elements should be produced.
@inlinable
public func yield<S: Sequence>(contentsOf sequence: S) -> YieldResult where S.Element == Element {
self._storage.yield(sequence)
}
/// Yields a new elements to the ``NIOThrowingAsyncSequenceProducer``.
///
/// If there is an ``NIOThrowingAsyncSequenceProducer/AsyncIterator`` awaiting the next element, it will get resumed right away.
/// Otherwise, the element will get buffered.
///
/// If the ``NIOThrowingAsyncSequenceProducer`` is terminated this will drop the elements
/// and return ``YieldResult/dropped``.
///
/// This can be called more than once and returns to the caller immediately
/// without blocking for any awaiting consumption from the iteration.
///
/// - Parameter element: The element to yield.
/// - Returns: A ``NIOThrowingAsyncSequenceProducer/Source/YieldResult`` that indicates if the yield was successful
/// and if more elements should be produced.
@inlinable
public func yield(_ element: Element) -> YieldResult {
self.yield(contentsOf: CollectionOfOne(element))
}
/// Finishes the sequence.
///
/// Calling this function signals the sequence that there won't be any subsequent elements yielded.
///
/// If there are still buffered elements and there is an ``NIOThrowingAsyncSequenceProducer/AsyncIterator`` consuming the sequence,
/// then termination of the sequence only happens once all elements have been consumed by the ``NIOThrowingAsyncSequenceProducer/AsyncIterator``.
/// Otherwise, the buffered elements will be dropped.
///
/// - Note: Calling this function more than once has no effect.
@inlinable
public func finish() {
self._storage.finish(nil)
}
/// Finishes the sequence with the given `Failure`.
///
/// Calling this function signals the sequence that there won't be any subsequent elements yielded.
///
/// If there are still buffered elements and there is an ``NIOThrowingAsyncSequenceProducer/AsyncIterator`` consuming the sequence,
/// then termination of the sequence only happens once all elements have been consumed by the ``NIOThrowingAsyncSequenceProducer/AsyncIterator``.
/// Otherwise, the buffered elements will be dropped.
///
/// - Note: Calling this function more than once has no effect.
/// - Parameter failure: The failure why the sequence finished.
@inlinable
public func finish(_ failure: Failure) {
self._storage.finish(failure)
}
}
}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension NIOThrowingAsyncSequenceProducer {
/// This is the underlying storage of the sequence. The goal of this is to synchronize the access to all state.
@usableFromInline
/* fileprivate */ internal final class Storage: @unchecked Sendable {
/// The lock that protects our state.
@usableFromInline
/* private */ internal let _lock = NIOLock()
/// The state machine.
@usableFromInline
/* private */ internal var _stateMachine: StateMachine
/// The delegate.
@usableFromInline
/* private */ internal var _delegate: Delegate?
/// Hook used in testing.
@usableFromInline
internal var _didSuspend: (() -> Void)?
@inlinable
var isFinished: Bool {
self._lock.withLock { self._stateMachine.isFinished }
}
@usableFromInline
/* fileprivate */ internal init(
backPressureStrategy: Strategy,
delegate: Delegate
) {
self._stateMachine = .init(backPressureStrategy: backPressureStrategy)
self._delegate = delegate
}
@inlinable
/* fileprivate */ internal func sequenceDeinitialized() {
let delegate: Delegate? = self._lock.withLock {
let action = self._stateMachine.sequenceDeinitialized()
switch action {
case .callDidTerminate:
let delegate = self._delegate
self._delegate = nil
return delegate
case .none:
return nil
}
}
delegate?.didTerminate()
}
@inlinable
/* fileprivate */ internal func iteratorInitialized() {
self._lock.withLock {
self._stateMachine.iteratorInitialized()
}
}
@inlinable
/* fileprivate */ internal func iteratorDeinitialized() {
let delegate: Delegate? = self._lock.withLock {
let action = self._stateMachine.iteratorDeinitialized()
switch action {
case .callDidTerminate:
let delegate = self._delegate
self._delegate = nil
return delegate
case .none:
return nil
}
}
delegate?.didTerminate()
}
@inlinable
/* fileprivate */ internal func yield<S: Sequence>(_ sequence: S) -> Source.YieldResult where S.Element == Element {
// We must not resume the continuation while holding the lock
// because it can deadlock in combination with the underlying ulock
// in cases where we race with a cancellation handler
let action = self._lock.withLock {
self._stateMachine.yield(sequence)
}
switch action {
case .returnProduceMore:
return .produceMore
case .returnStopProducing:
return .stopProducing
case .returnDropped:
return .dropped
case .resumeContinuationAndReturnProduceMore(let continuation, let element):
continuation.resume(returning: element)
return .produceMore
case .resumeContinuationAndReturnStopProducing(let continuation, let element):
continuation.resume(returning: element)
return .stopProducing
}
}
@inlinable
/* fileprivate */ internal func finish(_ failure: Failure?) {
// We must not resume the continuation while holding the lock
// because it can deadlock in combination with the underlying ulock
// in cases where we race with a cancellation handler
let (delegate, action): (Delegate?, NIOThrowingAsyncSequenceProducer.StateMachine.FinishAction) = self._lock.withLock {
let action = self._stateMachine.finish(failure)
switch action {
case .resumeContinuationWithFailureAndCallDidTerminate:
let delegate = self._delegate
self._delegate = nil
return (delegate, action)
case .none:
return (nil, action)
}
}
switch action {
case .resumeContinuationWithFailureAndCallDidTerminate(let continuation, let failure):
switch failure {
case .some(let error):
continuation.resume(throwing: error)
case .none:
continuation.resume(returning: nil)
}
case .none:
break
}
delegate?.didTerminate()
}
@inlinable
/* fileprivate */ internal func next() async throws -> Element? {
try await withTaskCancellationHandler {
self._lock.lock()
let action = self._stateMachine.next()
switch action {
case .returnElement(let element):
self._lock.unlock()
return element
case .returnElementAndCallProduceMore(let element):
let delegate = self._delegate
self._lock.unlock()
delegate?.produceMore()
return element
case .returnFailureAndCallDidTerminate(let failure):
let delegate = self._delegate
self._delegate = nil
self._lock.unlock()
delegate?.didTerminate()
switch failure {
case .some(let error):
throw error
case .none:
return nil
}
case .returnCancellationError:
self._lock.unlock()
// We have deprecated the generic Failure type in the public API and Failure should
// now be `Swift.Error`. However, if users have not migrated to the new API they could
// still use a custom generic Error type and this cast might fail.
// In addition, we use `NIOThrowingAsyncSequenceProducer` in the implementation of the
// non-throwing variant `NIOAsyncSequenceProducer` where `Failure` will be `Never` and
// this cast will fail as well.
// Everything is marked @inlinable and the Failure type is known at compile time,
// therefore this cast should be optimised away in release build.
if let error = CancellationError() as? Failure {
throw error
}
return nil
case .returnNil:
self._lock.unlock()
return nil
case .suspendTask:
// It is safe to hold the lock across this method
// since the closure is guaranteed to be run straight away
return try await withCheckedThrowingContinuation { continuation in
let action = self._stateMachine.next(for: continuation)
switch action {
case .callProduceMore:
let delegate = _delegate
self._lock.unlock()
delegate?.produceMore()
case .none:
self._lock.unlock()
}
self._didSuspend?()
}
}
} onCancel: {
// We must not resume the continuation while holding the lock
// because it can deadlock in combination with the underlying ulock
// in cases where we race with a cancellation handler
let (delegate, action): (Delegate?, NIOThrowingAsyncSequenceProducer.StateMachine.CancelledAction) = self._lock.withLock {
let action = self._stateMachine.cancelled()
switch action {
case .callDidTerminate:
let delegate = self._delegate
self._delegate = nil
return (delegate, action)
case .resumeContinuationWithCancellationErrorAndCallDidTerminate:
let delegate = self._delegate
self._delegate = nil
return (delegate, action)
case .none:
return (nil, action)
}
}
switch action {
case .callDidTerminate:
break
case .resumeContinuationWithCancellationErrorAndCallDidTerminate(let continuation):
// We have deprecated the generic Failure type in the public API and Failure should
// now be `Swift.Error`. However, if users have not migrated to the new API they could
// still use a custom generic Error type and this cast might fail.
// In addition, we use `NIOThrowingAsyncSequenceProducer` in the implementation of the
// non-throwing variant `NIOAsyncSequenceProducer` where `Failure` will be `Never` and
// this cast will fail as well.
// Everything is marked @inlinable and the Failure type is known at compile time,
// therefore this cast should be optimised away in release build.
if let failure = CancellationError() as? Failure {
continuation.resume(throwing: failure)
} else {
continuation.resume(returning: nil)
}
case .none:
break
}
delegate?.didTerminate()
}
}
}
}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension NIOThrowingAsyncSequenceProducer {
@usableFromInline
/* private */ internal struct StateMachine {
@usableFromInline
/* private */ internal enum State {
/// The initial state before either a call to `yield()` or a call to `next()` happened
case initial(
backPressureStrategy: Strategy,
iteratorInitialized: Bool
)
/// The state once either any element was yielded or `next()` was called.
case streaming(
backPressureStrategy: Strategy,
buffer: Deque<Element>,
continuation: CheckedContinuation<Element?, Error>?,
hasOutstandingDemand: Bool,
iteratorInitialized: Bool
)
/// The state once the underlying source signalled that it is finished.
case sourceFinished(
buffer: Deque<Element>,
iteratorInitialized: Bool,
failure: Failure?
)
/// The state once a call to next has been cancelled. Cancel the source when entering this state.
case cancelled(iteratorInitialized: Bool)
/// The state once there can be no outstanding demand. This can happen if:
/// 1. The ``NIOThrowingAsyncSequenceProducer/AsyncIterator`` was deinited
/// 2. The underlying source finished and all buffered elements have been consumed
case finished(iteratorInitialized: Bool)
/// Internal state to avoid CoW.
case modifying
}
/// The state machine's current state.
@usableFromInline
/* private */ internal var _state: State
@inlinable
var isFinished: Bool {
switch self._state {
case .initial, .streaming:
return false
case .cancelled, .sourceFinished, .finished:
return true
case .modifying:
preconditionFailure("Invalid state")
}
}
/// Initializes a new `StateMachine`.
///
/// We are passing and holding the back-pressure strategy here because
/// it is a customizable extension of the state machine.
///
/// - Parameter backPressureStrategy: The back-pressure strategy.
@inlinable
init(backPressureStrategy: Strategy) {
self._state = .initial(
backPressureStrategy: backPressureStrategy,
iteratorInitialized: false
)
}
/// Actions returned by `sequenceDeinitialized()`.
@usableFromInline
enum SequenceDeinitializedAction {
/// Indicates that ``NIOAsyncSequenceProducerDelegate/didTerminate()`` should be called.
case callDidTerminate
/// Indicates that nothing should be done.
case none
}
@inlinable
mutating func sequenceDeinitialized() -> SequenceDeinitializedAction {
switch self._state {
case .initial(_, iteratorInitialized: false),
.streaming(_, _, _, _, iteratorInitialized: false),
.sourceFinished(_, iteratorInitialized: false, _),
.cancelled(iteratorInitialized: false):
// No iterator was created so we can transition to finished right away.
self._state = .finished(iteratorInitialized: false)
return .callDidTerminate
case .initial(_, iteratorInitialized: true),
.streaming(_, _, _, _, iteratorInitialized: true),
.sourceFinished(_, iteratorInitialized: true, _),
.cancelled(iteratorInitialized: true):
// An iterator was created and we deinited the sequence.
// This is an expected pattern and we just continue on normal.
return .none
case .finished:
// We are already finished so there is nothing left to clean up.
// This is just the references dropping afterwards.
return .none
case .modifying:
preconditionFailure("Invalid state")
}
}
@inlinable
mutating func iteratorInitialized() {
switch self._state {
case .initial(_, iteratorInitialized: true),
.streaming(_, _, _, _, iteratorInitialized: true),
.sourceFinished(_, iteratorInitialized: true, _),
.cancelled(iteratorInitialized: true),
.finished(iteratorInitialized: true):
// Our sequence is a unicast sequence and does not support multiple AsyncIterator's
fatalError("NIOThrowingAsyncSequenceProducer allows only a single AsyncIterator to be created")
case .initial(let backPressureStrategy, iteratorInitialized: false):
// The first and only iterator was initialized.
self._state = .initial(
backPressureStrategy: backPressureStrategy,
iteratorInitialized: true
)
case .streaming(let backPressureStrategy, let buffer, let continuation, let hasOutstandingDemand, false):
// The first and only iterator was initialized.
self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: buffer,
continuation: continuation,
hasOutstandingDemand: hasOutstandingDemand,
iteratorInitialized: true
)
case .cancelled(iteratorInitialized: false):
// An iterator needs to be initialized before we can be cancelled.
preconditionFailure("Internal inconsistency")
case .sourceFinished(let buffer, false, let failure):
// The first and only iterator was initialized.
self._state = .sourceFinished(
buffer: buffer,
iteratorInitialized: true,
failure: failure
)
case .finished(iteratorInitialized: false):
// It is strange that an iterator is created after we are finished
// but it can definitely happen, e.g.
// Sequence.init -> source.finish -> sequence.makeAsyncIterator
self._state = .finished(iteratorInitialized: true)
case .modifying:
preconditionFailure("Invalid state")
}
}
/// Actions returned by `iteratorDeinitialized()`.
@usableFromInline
enum IteratorDeinitializedAction {
/// Indicates that ``NIOAsyncSequenceProducerDelegate/didTerminate()`` should be called.
case callDidTerminate
/// Indicates that nothing should be done.
case none
}
@inlinable
mutating func iteratorDeinitialized() -> IteratorDeinitializedAction {
switch self._state {
case .initial(_, iteratorInitialized: false),
.streaming(_, _, _, _, iteratorInitialized: false),
.sourceFinished(_, iteratorInitialized: false, _),
.cancelled(iteratorInitialized: false):
// An iterator needs to be initialized before it can be deinitialized.
preconditionFailure("Internal inconsistency")
case .initial(_, iteratorInitialized: true),
.streaming(_, _, _, _, iteratorInitialized: true),
.sourceFinished(_, iteratorInitialized: true, _),
.cancelled(iteratorInitialized: true):
// An iterator was created and deinited. Since we only support
// a single iterator we can now transition to finish and inform the delegate.
self._state = .finished(iteratorInitialized: true)
return .callDidTerminate
case .finished:
// We are already finished so there is nothing left to clean up.
// This is just the references dropping afterwards.
return .none
case .modifying:
preconditionFailure("Invalid state")
}
}
/// Actions returned by `yield()`.
@usableFromInline
enum YieldAction {
/// Indicates that ``NIOThrowingAsyncSequenceProducer/Source/YieldResult/produceMore`` should be returned.
case returnProduceMore
/// Indicates that ``NIOThrowingAsyncSequenceProducer/Source/YieldResult/stopProducing`` should be returned.
case returnStopProducing
/// Indicates that the continuation should be resumed and
/// ``NIOThrowingAsyncSequenceProducer/Source/YieldResult/produceMore`` should be returned.
case resumeContinuationAndReturnProduceMore(
continuation: CheckedContinuation<Element?, Error>,
element: Element
)
/// Indicates that the continuation should be resumed and
/// ``NIOThrowingAsyncSequenceProducer/Source/YieldResult/stopProducing`` should be returned.
case resumeContinuationAndReturnStopProducing(
continuation: CheckedContinuation<Element?, Error>,
element: Element
)
/// Indicates that the yielded elements have been dropped.
case returnDropped
@usableFromInline
init(shouldProduceMore: Bool, continuationAndElement: (CheckedContinuation<Element?, Error>, Element)? = nil) {
switch (shouldProduceMore, continuationAndElement) {
case (true, .none):
self = .returnProduceMore
case (false, .none):
self = .returnStopProducing
case (true, .some((let continuation, let element))):
self = .resumeContinuationAndReturnProduceMore(
continuation: continuation,
element: element
)
case (false, .some((let continuation, let element))):
self = .resumeContinuationAndReturnStopProducing(
continuation: continuation,
element: element
)
}
}
}
@inlinable
mutating func yield<S: Sequence>(_ sequence: S) -> YieldAction where S.Element == Element {
switch self._state {
case .initial(var backPressureStrategy, let iteratorInitialized):
let buffer = Deque<Element>(sequence)
let shouldProduceMore = backPressureStrategy.didYield(bufferDepth: buffer.count)
self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: buffer,
continuation: nil,
hasOutstandingDemand: shouldProduceMore,
iteratorInitialized: iteratorInitialized
)
return .init(shouldProduceMore: shouldProduceMore)
case .streaming(var backPressureStrategy, var buffer, .some(let continuation), let hasOutstandingDemand, let iteratorInitialized):
// The buffer should always be empty if we hold a continuation
precondition(buffer.isEmpty, "Expected an empty buffer")
self._state = .modifying
buffer.append(contentsOf: sequence)
guard let element = buffer.popFirst() else {
self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: buffer,
continuation: continuation,
hasOutstandingDemand: hasOutstandingDemand,
iteratorInitialized: iteratorInitialized
)
return .init(shouldProduceMore: hasOutstandingDemand)
}
// We have an element and can resume the continuation
let shouldProduceMore = backPressureStrategy.didYield(bufferDepth: buffer.count)
self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: buffer,
continuation: nil, // Setting this to nil since we are resuming the continuation
hasOutstandingDemand: shouldProduceMore,
iteratorInitialized: iteratorInitialized
)
return .init(shouldProduceMore: shouldProduceMore, continuationAndElement: (continuation, element))
case .streaming(var backPressureStrategy, var buffer, continuation: .none, _, let iteratorInitialized):
self._state = .modifying
buffer.append(contentsOf: sequence)
let shouldProduceMore = backPressureStrategy.didYield(bufferDepth: buffer.count)
self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: buffer,
continuation: nil,
hasOutstandingDemand: shouldProduceMore,
iteratorInitialized: iteratorInitialized
)
return .init(shouldProduceMore: shouldProduceMore)
case .cancelled, .sourceFinished, .finished:
// If the source has finished we are dropping the elements.
return .returnDropped
case .modifying:
preconditionFailure("Invalid state")
}
}
/// Actions returned by `finish()`.
@usableFromInline
enum FinishAction {
/// Indicates that the continuation should be resumed with `nil` and
/// that ``NIOAsyncSequenceProducerDelegate/didTerminate()`` should be called.
case resumeContinuationWithFailureAndCallDidTerminate(CheckedContinuation<Element?, Error>, Failure?)
/// Indicates that nothing should be done.
case none
}
@inlinable
mutating func finish(_ failure: Failure?) -> FinishAction {
switch self._state {
case .initial(_, let iteratorInitialized):
// Nothing was yielded nor did anybody call next
// This means we can transition to sourceFinished and store the failure
self._state = .sourceFinished(
buffer: .init(),
iteratorInitialized: iteratorInitialized,
failure: failure
)
return .none
case .streaming(_, let buffer, .some(let continuation), _, let iteratorInitialized):
// We have a continuation, this means our buffer must be empty
// Furthermore, we can now transition to finished
// and resume the continuation with the failure
precondition(buffer.isEmpty, "Expected an empty buffer")
self._state = .finished(iteratorInitialized: iteratorInitialized)
return .resumeContinuationWithFailureAndCallDidTerminate(continuation, failure)
case .streaming(_, let buffer, continuation: .none, _, let iteratorInitialized):
self._state = .sourceFinished(
buffer: buffer,
iteratorInitialized: iteratorInitialized,
failure: failure
)
return .none
case .cancelled, .sourceFinished, .finished:
// If the source has finished, finishing again has no effect.
return .none
case .modifying:
preconditionFailure("Invalid state")
}
}
/// Actions returned by `cancelled()`.
@usableFromInline
enum CancelledAction {
/// Indicates that ``NIOAsyncSequenceProducerDelegate/didTerminate()`` should be called.
case callDidTerminate
/// Indicates that the continuation should be resumed with a `CancellationError` and
/// that ``NIOAsyncSequenceProducerDelegate/didTerminate()`` should be called.
case resumeContinuationWithCancellationErrorAndCallDidTerminate(CheckedContinuation<Element?, Error>)
/// Indicates that nothing should be done.
case none
}
@inlinable
mutating func cancelled() -> CancelledAction {
switch self._state {
case .initial(_, let iteratorInitialized):
// This can happen if the `Task` that calls `next()` is already cancelled.
// We have deprecated the generic Failure type in the public API and Failure should
// now be `Swift.Error`. However, if users have not migrated to the new API they could
// still use a custom generic Error type and this cast might fail.
// In addition, we use `NIOThrowingAsyncSequenceProducer` in the implementation of the
// non-throwing variant `NIOAsyncSequenceProducer` where `Failure` will be `Never` and
// this cast will fail as well.
// Everything is marked @inlinable and the Failure type is known at compile time,
// therefore this cast should be optimised away in release build.
if let failure = CancellationError() as? Failure {
self._state = .sourceFinished(
buffer: .init(),
iteratorInitialized: iteratorInitialized,
failure: failure
)
} else {
self._state = .finished(iteratorInitialized: iteratorInitialized)
}
return .none
case .streaming(_, _, .some(let continuation), _, let iteratorInitialized):
// We have an outstanding continuation that needs to resumed
// and we can transition to finished here and inform the delegate
self._state = .finished(iteratorInitialized: iteratorInitialized)
return .resumeContinuationWithCancellationErrorAndCallDidTerminate(continuation)
case .streaming(_, _, continuation: .none, _, let iteratorInitialized):
// We may have elements in the buffer, which is why we have no continuation
// waiting. We must store the cancellation error to hand it out on the next
// next() call.
self._state = .cancelled(iteratorInitialized: iteratorInitialized)
return .callDidTerminate
case .cancelled, .sourceFinished, .finished:
// If the source has finished, finishing again has no effect.
return .none
case .modifying:
preconditionFailure("Invalid state")
}
}
/// Actions returned by `next()`.
@usableFromInline
enum NextAction {
/// Indicates that the element should be returned to the caller.
case returnElement(Element)
/// Indicates that the element should be returned to the caller and
/// that ``NIOAsyncSequenceProducerDelegate/produceMore()`` should be called.
case returnElementAndCallProduceMore(Element)
/// Indicates that the `Failure` should be returned to the caller and
/// that ``NIOAsyncSequenceProducerDelegate/didTerminate()`` should be called.
case returnFailureAndCallDidTerminate(Failure?)
/// Indicates that the next call to AsyncSequence got cancelled
case returnCancellationError
/// Indicates that the `nil` should be returned to the caller.
case returnNil
/// Indicates that the `Task` of the caller should be suspended.
case suspendTask
}
@inlinable
mutating func next() -> NextAction {
switch self._state {
case .initial(let backPressureStrategy, let iteratorInitialized):
// We are not interacting with the back-pressure strategy here because
// we are doing this inside `next(:)`
self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: Deque<Element>(),
continuation: nil,
hasOutstandingDemand: false,
iteratorInitialized: iteratorInitialized
)
return .suspendTask
case .streaming(_, _, .some, _, _):
// We have multiple AsyncIterators iterating the sequence
preconditionFailure("This should never happen since we only allow a single Iterator to be created")
case .streaming(var backPressureStrategy, var buffer, .none, let hasOutstandingDemand, let iteratorInitialized):
self._state = .modifying
if let element = buffer.popFirst() {
// We have an element to fulfil the demand right away.
let shouldProduceMore = backPressureStrategy.didConsume(bufferDepth: buffer.count)
self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: buffer,
continuation: nil,
hasOutstandingDemand: shouldProduceMore,
iteratorInitialized: iteratorInitialized
)
if shouldProduceMore && !hasOutstandingDemand {
// We didn't have any demand but now we do, so we need to inform the delegate.
return .returnElementAndCallProduceMore(element)
} else {
// We don't have any new demand, so we can just return the element.
return .returnElement(element)
}
} else {
// There is nothing in the buffer to fulfil the demand so we need to suspend.
// We are not interacting with the back-pressure strategy here because
// we are doing this inside `next(:)`
self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: buffer,
continuation: nil,
hasOutstandingDemand: hasOutstandingDemand,
iteratorInitialized: iteratorInitialized
)
return .suspendTask
}
case .sourceFinished(var buffer, let iteratorInitialized, let failure):
self._state = .modifying
// Check if we have an element left in the buffer and return it
if let element = buffer.popFirst() {
self._state = .sourceFinished(
buffer: buffer,
iteratorInitialized: iteratorInitialized,
failure: failure
)
return .returnElement(element)
} else {
// We are returning the queued failure now and can transition to finished
self._state = .finished(iteratorInitialized: iteratorInitialized)
return .returnFailureAndCallDidTerminate(failure)
}
case .cancelled(let iteratorInitialized):
self._state = .finished(iteratorInitialized: iteratorInitialized)
return .returnCancellationError
case .finished:
return .returnNil
case .modifying:
preconditionFailure("Invalid state")
}
}
/// Actions returned by `next(for:)`.
@usableFromInline
enum NextForContinuationAction {
/// Indicates that ``NIOAsyncSequenceProducerDelegate/produceMore()`` should be called.
case callProduceMore
/// Indicates that nothing should be done.
case none
}
@inlinable
mutating func next(for continuation: CheckedContinuation<Element?, Error>) -> NextForContinuationAction {
switch self._state {
case .initial:
// We are transitioning away from the initial state in `next()`
preconditionFailure("Invalid state")
case .streaming(var backPressureStrategy, let buffer, .none, let hasOutstandingDemand, let iteratorInitialized):
precondition(buffer.isEmpty, "Expected an empty buffer")
self._state = .modifying
let shouldProduceMore = backPressureStrategy.didConsume(bufferDepth: buffer.count)
self._state = .streaming(
backPressureStrategy: backPressureStrategy,
buffer: buffer,
continuation: continuation,
hasOutstandingDemand: shouldProduceMore,
iteratorInitialized: iteratorInitialized
)
if shouldProduceMore && !hasOutstandingDemand {
return .callProduceMore
} else {
return .none
}
case .streaming(_, _, .some(_), _, _), .sourceFinished, .finished, .cancelled:
preconditionFailure("This should have already been handled by `next()`")
case .modifying:
preconditionFailure("Invalid state")
}
}
}
}
/// The ``NIOThrowingAsyncSequenceProducer/AsyncIterator`` MUST NOT be shared across `Task`s. With marking this as
/// unavailable we are explicitly declaring this.
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
@available(*, unavailable)
extension NIOThrowingAsyncSequenceProducer.AsyncIterator: Sendable {}
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension NIOThrowingAsyncSequenceProducer.Source: Sendable {}
|