@@ -64,7 +64,7 @@ private static class State
64
64
public const int Disposed = 2 ;
65
65
}
66
66
67
- private sealed class AcquireConnectionHelper
67
+ internal sealed class AcquireConnectionHelper
68
68
{
69
69
// private fields
70
70
private readonly ExclusiveConnectionPool _pool ;
@@ -106,8 +106,8 @@ public IConnectionHandle EnteredPool(bool enteredPool, CancellationToken cancell
106
106
107
107
if ( enteredPool )
108
108
{
109
- var timeSpentInWaitQueue = _stopwatch . Elapsed ;
110
- using ( var connectionCreator = new ConnectionCreator ( _pool , _pool . _settings . WaitQueueTimeout - timeSpentInWaitQueue ) )
109
+ var timeout = EnsureTimeout ( ) ;
110
+ using ( var connectionCreator = new ConnectionCreator ( _pool , timeout ) )
111
111
{
112
112
connection = connectionCreator . CreateOpenedOrReuse ( cancellationToken ) ;
113
113
}
@@ -123,8 +123,8 @@ public async Task<IConnectionHandle> EnteredPoolAsync(bool enteredPool, Cancella
123
123
124
124
if ( enteredPool )
125
125
{
126
- var timeSpentInWaitQueue = _stopwatch . Elapsed ;
127
- using ( var connectionCreator = new ConnectionCreator ( _pool , _pool . _settings . WaitQueueTimeout - timeSpentInWaitQueue ) )
126
+ var timeout = EnsureTimeout ( ) ;
127
+ using ( var connectionCreator = new ConnectionCreator ( _pool , timeout ) )
128
128
{
129
129
connection = await connectionCreator . CreateOpenedOrReuseAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
130
130
}
@@ -154,6 +154,19 @@ private AcquiredConnection FinalizePoolEnterance(PooledConnection pooledConnecti
154
154
}
155
155
}
156
156
157
+ private TimeSpan EnsureTimeout ( )
158
+ {
159
+ var timeSpentInWaitQueue = _stopwatch . Elapsed ;
160
+ var timeout = _pool . _settings . WaitQueueTimeout - timeSpentInWaitQueue ;
161
+
162
+ if ( timeout <= TimeSpan . Zero )
163
+ {
164
+ throw _pool . CreateTimeoutException ( _stopwatch , $ "Timed out waiting for a connection after { timeSpentInWaitQueue . TotalMilliseconds } ms.") ;
165
+ }
166
+
167
+ return timeout ;
168
+ }
169
+
157
170
public void Finally ( )
158
171
{
159
172
if ( _enteredWaitQueue )
0 commit comments