ConnectionPool+PerConnectionState.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright 2021, 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 NIOHTTP2
  17. extension ConnectionPool {
  18. internal struct PerConnectionState {
  19. /// The connection manager for this connection.
  20. internal var manager: ConnectionManager
  21. /// Stream availability for this connection, `nil` if the connection is not available.
  22. private var availability: StreamAvailability?
  23. private struct StreamAvailability {
  24. var multiplexer: HTTP2StreamMultiplexer
  25. /// Maximum number of available streams.
  26. var maxAvailable: Int
  27. /// Number of streams reserved.
  28. var reserved: Int = 0
  29. /// Number of available streams.
  30. var available: Int {
  31. return self.maxAvailable - self.reserved
  32. }
  33. /// Increment the reserved streams and return the multiplexer.
  34. mutating func reserve() -> HTTP2StreamMultiplexer {
  35. self.reserved += 1
  36. return self.multiplexer
  37. }
  38. /// Decrement the reserved streams by one.
  39. mutating func `return`() {
  40. self.reserved -= 1
  41. assert(self.reserved >= 0)
  42. }
  43. }
  44. init(manager: ConnectionManager) {
  45. self.manager = manager
  46. self.availability = nil
  47. }
  48. /// The number of reserved streams.
  49. internal var reservedStreams: Int {
  50. return self.availability?.reserved ?? 0
  51. }
  52. /// The number of streams available to reserve. If this value is greater than zero then it is
  53. /// safe to call `reserveStream()` and force unwrap the result.
  54. internal var availableStreams: Int {
  55. return self.availability?.available ?? 0
  56. }
  57. /// The maximum number of concurrent streams which may be available for the connection, if it
  58. /// is ready.
  59. internal var maxAvailableStreams: Int? {
  60. return self.availability?.maxAvailable
  61. }
  62. /// Reserve a stream and return the stream multiplexer. Returns `nil` if it is not possible
  63. /// to reserve a stream.
  64. ///
  65. /// The result may be safely unwrapped if `self.availableStreams > 0` when reserving a stream.
  66. internal mutating func reserveStream() -> HTTP2StreamMultiplexer? {
  67. return self.availability?.reserve()
  68. }
  69. /// Return a reserved stream to the connection.
  70. internal mutating func returnStream() {
  71. self.availability?.return()
  72. }
  73. /// Update the maximum concurrent streams available on the connection, marking it as available
  74. /// if it was not already.
  75. ///
  76. /// Returns the previous value for max concurrent streams if the connection was ready.
  77. internal mutating func updateMaxConcurrentStreams(_ maxConcurrentStreams: Int) -> Int? {
  78. if var availability = self.availability {
  79. var oldValue = maxConcurrentStreams
  80. swap(&availability.maxAvailable, &oldValue)
  81. self.availability = availability
  82. return oldValue
  83. } else {
  84. self.availability = self.manager.sync.multiplexer.map {
  85. StreamAvailability(multiplexer: $0, maxAvailable: maxConcurrentStreams)
  86. }
  87. return nil
  88. }
  89. }
  90. /// Mark the connection as unavailable returning the number of reserved streams.
  91. internal mutating func unavailable() -> Int {
  92. defer {
  93. self.availability = nil
  94. }
  95. return self.availability?.reserved ?? 0
  96. }
  97. }
  98. }