@@ -64,7 +64,7 @@ private static class State
6464 public const int Disposed = 2 ;
6565 }
6666
67- private sealed class AcquireConnectionHelper
67+ internal sealed class AcquireConnectionHelper
6868 {
6969 // private fields
7070 private readonly ExclusiveConnectionPool _pool ;
@@ -106,8 +106,8 @@ public IConnectionHandle EnteredPool(bool enteredPool, CancellationToken cancell
106106
107107 if ( enteredPool )
108108 {
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 ) )
111111 {
112112 connection = connectionCreator . CreateOpenedOrReuse ( cancellationToken ) ;
113113 }
@@ -123,8 +123,8 @@ public async Task<IConnectionHandle> EnteredPoolAsync(bool enteredPool, Cancella
123123
124124 if ( enteredPool )
125125 {
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 ) )
128128 {
129129 connection = await connectionCreator . CreateOpenedOrReuseAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
130130 }
@@ -154,6 +154,19 @@ private AcquiredConnection FinalizePoolEnterance(PooledConnection pooledConnecti
154154 }
155155 }
156156
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+
157170 public void Finally ( )
158171 {
159172 if ( _enteredWaitQueue )
0 commit comments