@@ -15,9 +15,7 @@ public sealed class AgeConnectionManager : IAgeConnectionManager
1515{
1616 private readonly NpgsqlDataSource _dataSource ;
1717 private readonly ILogger < AgeConnectionManager > _logger ;
18- private readonly SemaphoreSlim _extensionLock = new ( 1 , 1 ) ;
19- private readonly SemaphoreSlim _poolSemaphore ;
20- private bool _extensionEnsured ;
18+ private volatile bool _extensionEnsured ;
2119 private bool _disposed ;
2220
2321 public AgeConnectionManager ( string connectionString , ILogger < AgeConnectionManager > ? logger = null , int maxPoolSize = 100 )
@@ -35,7 +33,6 @@ public AgeConnectionManager(string connectionString, ILogger<AgeConnectionManage
3533 } ;
3634
3735 ConnectionString = connectionBuilder . ConnectionString ;
38- _poolSemaphore = new SemaphoreSlim ( maxPoolSize , maxPoolSize ) ;
3936 _dataSource = NpgsqlDataSource . Create ( connectionBuilder . ConnectionString ) ;
4037 _logger = logger ?? NullLogger < AgeConnectionManager > . Instance ;
4138 }
@@ -46,43 +43,28 @@ public async Task<NpgsqlConnection> OpenConnectionAsync(CancellationToken cancel
4643 {
4744 ThrowIfDisposed ( ) ;
4845
49- await _poolSemaphore . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
50- try
51- {
52- await EnsureExtensionCreatedAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
53- var connection = await _dataSource . OpenConnectionAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
54- await LoadAgeAsync ( connection , cancellationToken ) . ConfigureAwait ( false ) ;
55- await SetSearchPathAsync ( connection , cancellationToken ) . ConfigureAwait ( false ) ;
56- return connection ;
57- }
58- catch
59- {
60- _poolSemaphore . Release ( ) ;
61- throw ;
62- }
46+ await EnsureExtensionCreatedAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
47+ var connection = await _dataSource . OpenConnectionAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
48+ await LoadAgeAsync ( connection , cancellationToken ) . ConfigureAwait ( false ) ;
49+ await SetSearchPathAsync ( connection , cancellationToken ) . ConfigureAwait ( false ) ;
50+ return connection ;
6351 }
6452
6553 public async Task ReturnConnectionAsync ( NpgsqlConnection connection , CancellationToken cancellationToken = default )
6654 {
6755 if ( connection is null )
6856 {
69- _poolSemaphore . Release ( ) ;
7057 return ;
7158 }
7259
73- try
74- {
75- if ( connection . FullState . HasFlag ( System . Data . ConnectionState . Open ) )
76- {
77- await connection . CloseAsync ( ) . ConfigureAwait ( false ) ;
78- }
60+ cancellationToken . ThrowIfCancellationRequested ( ) ;
7961
80- await connection . DisposeAsync ( ) . ConfigureAwait ( false ) ;
81- }
82- finally
62+ if ( connection . FullState . HasFlag ( System . Data . ConnectionState . Open ) )
8363 {
84- _poolSemaphore . Release ( ) ;
64+ await connection . CloseAsync ( ) . ConfigureAwait ( false ) ;
8565 }
66+
67+ await connection . DisposeAsync ( ) . ConfigureAwait ( false ) ;
8668 }
8769
8870 public void Dispose ( )
@@ -93,7 +75,6 @@ public void Dispose()
9375 }
9476
9577 _dataSource . Dispose ( ) ;
96- _poolSemaphore . Dispose ( ) ;
9778 _disposed = true ;
9879 GC . SuppressFinalize ( this ) ;
9980 }
@@ -106,7 +87,6 @@ public async ValueTask DisposeAsync()
10687 }
10788
10889 await _dataSource . DisposeAsync ( ) . ConfigureAwait ( false ) ;
109- _poolSemaphore . Dispose ( ) ;
11090 _disposed = true ;
11191 GC . SuppressFinalize ( this ) ;
11292 }
@@ -118,26 +98,13 @@ private async Task EnsureExtensionCreatedAsync(CancellationToken cancellationTok
11898 return ;
11999 }
120100
121- await _extensionLock . WaitAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
122- try
123- {
124- if ( _extensionEnsured )
125- {
126- return ;
127- }
128-
129- await using var connection = new NpgsqlConnection ( ConnectionString ) ;
130- await connection . OpenAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
131- await using var command = connection . CreateCommand ( ) ;
132- command . CommandText = "CREATE EXTENSION IF NOT EXISTS age;" ;
133- await command . ExecuteNonQueryAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
134- _extensionEnsured = true ;
135- LogMessages . ExtensionCreated ( _logger , ConnectionString ) ;
136- }
137- finally
138- {
139- _extensionLock . Release ( ) ;
140- }
101+ await using var connection = new NpgsqlConnection ( ConnectionString ) ;
102+ await connection . OpenAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
103+ await using var command = connection . CreateCommand ( ) ;
104+ command . CommandText = "CREATE EXTENSION IF NOT EXISTS age;" ;
105+ await command . ExecuteNonQueryAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
106+ _extensionEnsured = true ;
107+ LogMessages . ExtensionCreated ( _logger , ConnectionString ) ;
141108 }
142109
143110 private async Task LoadAgeAsync ( NpgsqlConnection connection , CancellationToken cancellationToken )
0 commit comments