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