-
-
Notifications
You must be signed in to change notification settings - Fork 89
Re-instate minimum connections after failed connection #612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adam-fowler
wants to merge
9
commits into
vapor:main
Choose a base branch
from
adam-fowler:fill-connection-pool-after-fail
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+239
−34
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
11c0c2f
handleAvailableConnection will return create multiple connections if …
adam-fowler dbc0b76
Update testBackingOffRequests
adam-fowler e72ed8b
Remove minimumConnectionCount parameter
adam-fowler 1f27837
Add stats check to tests
adam-fowler d883b9b
Add additional connection for every call to handleAvailableConnection…
adam-fowler 4c10e15
Changes from review
adam-fowler 296dc67
Don't request new connections on lease if concurrent requests is abov…
adam-fowler 1a6a8f5
Add test verifying concurrent requests limit
adam-fowler d7bf5f9
Use connection hard limit in handleAvailableConnection
adam-fowler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,9 @@ struct PoolConfiguration: Sendable { | |
|
|
||
| @usableFromInline | ||
| var idleTimeoutDuration: Duration = .seconds(30) | ||
|
|
||
| @usableFromInline | ||
| var maximumConcurrentConnectionRequests: Int = 20 | ||
| } | ||
|
|
||
| @usableFromInline | ||
|
|
@@ -90,6 +93,7 @@ struct PoolStateMachine< | |
|
|
||
| case scheduleTimers(Max2Sequence<Timer>) | ||
| case makeConnection(ConnectionRequest, TinyFastSequence<TimerCancellationToken>) | ||
| case makeConnectionsCancelAndScheduleTimers(TinyFastSequence<ConnectionRequest>, TinyFastSequence<TimerCancellationToken>, Max2Sequence<Timer>) | ||
| case runKeepAlive(Connection, TimerCancellationToken?) | ||
| case cancelTimers(TinyFastSequence<TimerCancellationToken>) | ||
| case closeConnection(Connection, Max2Sequence<TimerCancellationToken>) | ||
|
|
@@ -312,6 +316,12 @@ struct PoolStateMachine< | |
| request: requestAction, | ||
| connection: .none | ||
| ) | ||
| } else if self.connections.stats.connecting >= self.configuration.maximumConcurrentConnectionRequests { | ||
| // We have too many connection requests, lets delay creating any new connections | ||
| return .init( | ||
| request: requestAction, | ||
| connection: .none | ||
| ) | ||
| } else if let request = self.connections.createNewDemandConnectionIfPossible() { | ||
| // Can we create a demand connection | ||
| return .init( | ||
|
|
@@ -673,9 +683,20 @@ struct PoolStateMachine< | |
| let requests = self.requestQueue.pop(max: availableContext.info.availableStreams) | ||
| if !requests.isEmpty { | ||
| let leaseResult = self.connections.leaseConnection(at: index, streams: UInt16(requests.count)) | ||
| let connectionsRequired: Int | ||
| if self.requestQueue.count <= self.connections.stats.availableStreams + self.connections.stats.leasedStreams { | ||
| connectionsRequired = self.configuration.minimumConnectionCount - Int(self.connections.stats.active) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does active include already connecting?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
| } else { | ||
| connectionsRequired = 1 | ||
| } | ||
| let connectionAction = self.createMultipleConnectionsAction( | ||
| connectionsRequired, | ||
| cancelledTimers: .init(leaseResult.timersToCancel), | ||
| scheduledTimers: [] | ||
| ) ?? .cancelTimers(.init(leaseResult.timersToCancel)) | ||
| return .init( | ||
| request: .leaseConnection(requests, leaseResult.connection), | ||
| connection: .cancelTimers(.init(leaseResult.timersToCancel)) | ||
| connection: connectionAction | ||
| ) | ||
| } | ||
|
|
||
|
|
@@ -704,10 +725,13 @@ struct PoolStateMachine< | |
| } | ||
| let timers = self.connections.parkConnection(at: index, hasBecomeIdle: newIdle).map(self.mapTimers) | ||
|
|
||
| return .init( | ||
| request: .none, | ||
| connection: .scheduleTimers(timers) | ||
| ) | ||
| let connectionsRequired = self.configuration.minimumConnectionCount - Int(self.connections.stats.active) | ||
| let connectionAction = self.createMultipleConnectionsAction( | ||
| connectionsRequired, | ||
| cancelledTimers: [], | ||
| scheduledTimers: timers | ||
| ) ?? .scheduleTimers(timers) | ||
| return .init(request: .none, connection: connectionAction) | ||
| } | ||
|
|
||
| case .overflow: | ||
|
|
@@ -723,6 +747,31 @@ struct PoolStateMachine< | |
|
|
||
| } | ||
|
|
||
| @inlinable | ||
| /* private */ mutating func createMultipleConnectionsAction( | ||
| _ connectionCount: Int, | ||
| cancelledTimers: TinyFastSequence<TimerCancellationToken>, | ||
| scheduledTimers: Max2Sequence<Timer> | ||
| ) -> ConnectionAction? { | ||
| let connectionCountLimitedByNumberOfRequests = min( | ||
| connectionCount, | ||
| self.configuration.maximumConcurrentConnectionRequests - Int(self.connections.stats.connecting) | ||
| ) | ||
| let connectionCountLimitedByHardLimit = min( | ||
| connectionCountLimitedByNumberOfRequests, | ||
| self.configuration.maximumConnectionHardLimit - Int(self.connections.stats.active) | ||
| ) | ||
| guard connectionCountLimitedByHardLimit > 0 else { return nil } | ||
|
|
||
| var connectionRequests = TinyFastSequence<ConnectionRequest>() | ||
| connectionRequests.reserveCapacity(connectionCountLimitedByHardLimit) | ||
| for _ in 0..<connectionCountLimitedByHardLimit { | ||
| connectionRequests.append(self.connections.createNewConnection()) | ||
| } | ||
| return .makeConnectionsCancelAndScheduleTimers(connectionRequests, cancelledTimers, scheduledTimers) | ||
|
|
||
| } | ||
|
|
||
| @inlinable | ||
| /* private */ func mapTimers(_ connectionTimer: ConnectionTimer) -> Timer { | ||
| switch connectionTimer.usecase { | ||
|
|
@@ -796,9 +845,6 @@ extension PoolStateMachine { | |
| @available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) | ||
| extension PoolStateMachine.Action: Equatable where TimerCancellationToken: Equatable, Request: Equatable {} | ||
|
|
||
| @available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) | ||
| //extension PoolStateMachine.PoolState: Equatable {} | ||
|
|
||
| @available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) | ||
| extension PoolStateMachine.ConnectionAction: Equatable where TimerCancellationToken: Equatable { | ||
| @usableFromInline | ||
|
|
@@ -808,6 +854,9 @@ extension PoolStateMachine.ConnectionAction: Equatable where TimerCancellationTo | |
| return lhs == rhs | ||
| case (.makeConnection(let lhsRequest, let lhsToken), .makeConnection(let rhsRequest, let rhsToken)): | ||
| return lhsRequest == rhsRequest && lhsToken == rhsToken | ||
| case (.makeConnectionsCancelAndScheduleTimers(let lhsRequests, let lhsTokens, let lhsTimers), | ||
| .makeConnectionsCancelAndScheduleTimers(let rhsRequests, let rhsTokens, let rhsTimers)): | ||
| return lhsRequests == rhsRequests && lhsTokens == rhsTokens && lhsTimers == rhsTimers | ||
| case (.runKeepAlive(let lhsConn, let lhsToken), .runKeepAlive(let rhsConn, let rhsToken)): | ||
| return lhsConn === rhsConn && lhsToken == rhsToken | ||
| case (.closeConnection(let lhsConn, let lhsTimers), .closeConnection(let rhsConn, let rhsTimers)): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.