2
0

PoolManagerStateMachine+PerPoolState.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 NIOCore
  17. extension PoolManagerStateMachine.ActiveState {
  18. @usableFromInline
  19. internal struct PerPoolState {
  20. /// The index of the connection pool associated with this state.
  21. @usableFromInline
  22. internal var poolIndex: PoolManager.ConnectionPoolIndex
  23. /// The number of streams reserved in the pool.
  24. @usableFromInline
  25. internal private(set) var reservedStreams: Int
  26. /// The total number of streams which may be available in the pool.
  27. @usableFromInline
  28. internal var maxAvailableStreams: Int
  29. /// The number of available streams.
  30. @usableFromInline
  31. internal var availableStreams: Int {
  32. return self.maxAvailableStreams - self.reservedStreams
  33. }
  34. @usableFromInline
  35. init(poolIndex: PoolManager.ConnectionPoolIndex, assumedMaxAvailableStreams: Int) {
  36. self.poolIndex = poolIndex
  37. self.reservedStreams = 0
  38. self.maxAvailableStreams = assumedMaxAvailableStreams
  39. }
  40. /// Reserve a stream and return the pool.
  41. @usableFromInline
  42. internal mutating func reserveStream() -> PoolManager.ConnectionPoolIndex {
  43. self.reservedStreams += 1
  44. return self.poolIndex
  45. }
  46. /// Return a reserved stream.
  47. @usableFromInline
  48. internal mutating func returnReservedStreams(_ count: Int) {
  49. self.reservedStreams -= count
  50. assert(self.reservedStreams >= 0)
  51. }
  52. }
  53. }
  54. extension PoolManager {
  55. @usableFromInline
  56. internal struct ConnectionPoolIndex: Hashable {
  57. @usableFromInline
  58. var value: Int
  59. @usableFromInline
  60. init(_ value: Int) {
  61. self.value = value
  62. }
  63. }
  64. @usableFromInline
  65. internal struct ConnectionPoolKey: Hashable {
  66. /// The index of the connection pool.
  67. @usableFromInline
  68. var index: ConnectionPoolIndex
  69. /// The ID of the`EventLoop` the connection pool uses.
  70. @usableFromInline
  71. var eventLoopID: EventLoopID
  72. }
  73. }
  74. @usableFromInline
  75. internal struct EventLoopID: Hashable, CustomStringConvertible {
  76. @usableFromInline
  77. internal let _id: ObjectIdentifier
  78. @usableFromInline
  79. internal init(_ eventLoop: EventLoop) {
  80. self._id = ObjectIdentifier(eventLoop)
  81. }
  82. @usableFromInline
  83. internal var description: String {
  84. return String(describing: self._id)
  85. }
  86. }
  87. extension EventLoop {
  88. @usableFromInline
  89. internal var id: EventLoopID {
  90. return EventLoopID(self)
  91. }
  92. }