LazyEventLoopPromise.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import NIO
  17. import NIOConcurrencyHelpers
  18. extension EventLoop {
  19. internal func makeLazyPromise<Value>(of: Value.Type = Value.self) -> LazyEventLoopPromise<Value> {
  20. return LazyEventLoopPromise(on: self)
  21. }
  22. }
  23. /// A `LazyEventLoopPromise` is similar to an `EventLoopPromise` except that the underlying
  24. /// `EventLoopPromise` promise is only created if it is required. That is, when the future result
  25. /// has been requested and the promise has not yet been completed.
  26. ///
  27. /// Note that all methods **must** be called from its `eventLoop`.
  28. internal struct LazyEventLoopPromise<Value> {
  29. private enum State {
  30. // No future has been requested, no result has been delivered.
  31. case idle
  32. // No future has been requested, but this result have been delivered.
  33. case resolvedResult(Result<Value, Error>)
  34. // A future has been request; the promise may or may not contain a result.
  35. case unresolvedPromise(EventLoopPromise<Value>)
  36. // A future was requested, it's also been resolved.
  37. case resolvedFuture(EventLoopFuture<Value>)
  38. }
  39. private var state: State
  40. private let eventLoop: EventLoop
  41. fileprivate init(on eventLoop: EventLoop) {
  42. self.state = .idle
  43. self.eventLoop = eventLoop
  44. }
  45. /// Get the future result of this promise.
  46. internal mutating func getFutureResult() -> EventLoopFuture<Value> {
  47. self.eventLoop.preconditionInEventLoop()
  48. switch self.state {
  49. case .idle:
  50. let promise = self.eventLoop.makePromise(of: Value.self)
  51. self.state = .unresolvedPromise(promise)
  52. return promise.futureResult
  53. case let .resolvedResult(result):
  54. let future: EventLoopFuture<Value>
  55. switch result {
  56. case let .success(value):
  57. future = self.eventLoop.makeSucceededFuture(value)
  58. case let .failure(error):
  59. future = self.eventLoop.makeFailedFuture(error)
  60. }
  61. self.state = .resolvedFuture(future)
  62. return future
  63. case let .unresolvedPromise(promise):
  64. return promise.futureResult
  65. case let .resolvedFuture(future):
  66. return future
  67. }
  68. }
  69. /// Succeed the promise with the given value.
  70. internal mutating func succeed(_ value: Value) {
  71. self.completeWith(.success(value))
  72. }
  73. /// Fail the promise with the given error.
  74. internal mutating func fail(_ error: Error) {
  75. self.completeWith(.failure(error))
  76. }
  77. /// Complete the promise with the given result.
  78. internal mutating func completeWith(_ result: Result<Value, Error>) {
  79. self.eventLoop.preconditionInEventLoop()
  80. switch self.state {
  81. case .idle:
  82. self.state = .resolvedResult(result)
  83. case let .unresolvedPromise(promise):
  84. promise.completeWith(result)
  85. self.state = .resolvedFuture(promise.futureResult)
  86. case .resolvedResult, .resolvedFuture:
  87. ()
  88. }
  89. }
  90. }