|
1 | 1 | using Microsoft.Extensions.DependencyInjection; |
2 | 2 | using Microsoft.Extensions.Logging; |
| 3 | +using Npgsql; |
3 | 4 |
|
4 | 5 | namespace GraphRag.Storage.Postgres.ApacheAge; |
5 | 6 |
|
6 | 7 | public interface IAgeClientFactory |
7 | 8 | { |
8 | | - AgeClient CreateClient(); |
| 9 | + IAgeClient CreateClient(); |
| 10 | + ValueTask<IAgeClientScope> CreateScopeAsync(CancellationToken cancellationToken = default); |
9 | 11 | } |
10 | 12 |
|
11 | 13 | internal sealed class AgeClientFactory([FromKeyedServices] IAgeConnectionManager connectionManager, ILoggerFactory loggerFactory) : IAgeClientFactory |
12 | 14 | { |
13 | 15 | private readonly IAgeConnectionManager _connectionManager = connectionManager ?? throw new ArgumentNullException(nameof(connectionManager)); |
14 | 16 | private readonly ILoggerFactory _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); |
| 17 | + private readonly AsyncLocal<AgeClientScopeState?> _currentScope = new(); |
15 | 18 |
|
16 | | - public AgeClient CreateClient() |
| 19 | + public IAgeClient CreateClient() |
| 20 | + { |
| 21 | + if (_currentScope.Value is { } scopeState) |
| 22 | + { |
| 23 | + return scopeState.CreateLease(); |
| 24 | + } |
| 25 | + |
| 26 | + return CreatePhysicalClient(); |
| 27 | + } |
| 28 | + |
| 29 | + public ValueTask<IAgeClientScope> CreateScopeAsync(CancellationToken cancellationToken = default) |
| 30 | + { |
| 31 | + cancellationToken.ThrowIfCancellationRequested(); |
| 32 | + |
| 33 | + if (_currentScope.Value is not null) |
| 34 | + { |
| 35 | + throw new InvalidOperationException("An AGE client scope is already active for this asynchronous context."); |
| 36 | + } |
| 37 | + |
| 38 | + var scopeClient = CreatePhysicalClient(); |
| 39 | + var scopeState = new AgeClientScopeState(scopeClient); |
| 40 | + _currentScope.Value = scopeState; |
| 41 | + |
| 42 | + return new ValueTask<IAgeClientScope>(new ActiveAgeClientScope(this, scopeState)); |
| 43 | + } |
| 44 | + |
| 45 | + private AgeClient CreatePhysicalClient() |
17 | 46 | { |
18 | 47 | var logger = _loggerFactory.CreateLogger<AgeClient>(); |
19 | 48 | return new AgeClient(_connectionManager, logger); |
20 | 49 | } |
| 50 | + |
| 51 | + private void ClearScope(AgeClientScopeState state) |
| 52 | + { |
| 53 | + if (!ReferenceEquals(_currentScope.Value, state)) |
| 54 | + { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + _currentScope.Value = null; |
| 59 | + } |
| 60 | + |
| 61 | + private sealed class ActiveAgeClientScope(AgeClientFactory factory, AgeClientFactory.AgeClientScopeState state) : IAgeClientScope |
| 62 | + { |
| 63 | + private readonly AgeClientFactory _factory = factory; |
| 64 | + private readonly AgeClientScopeState _state = state; |
| 65 | + private bool _disposed; |
| 66 | + |
| 67 | + public async ValueTask DisposeAsync() |
| 68 | + { |
| 69 | + if (_disposed) |
| 70 | + { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (_state.HasActiveLease) |
| 75 | + { |
| 76 | + throw new InvalidOperationException("Cannot dispose an AGE client scope while an operation is still running."); |
| 77 | + } |
| 78 | + |
| 79 | + _disposed = true; |
| 80 | + _factory.ClearScope(_state); |
| 81 | + await _state.Client.CloseConnectionAsync().ConfigureAwait(false); |
| 82 | + await _state.Client.DisposeAsync().ConfigureAwait(false); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private sealed class AgeClientScopeState(AgeClient client) |
| 87 | + { |
| 88 | + private int _leaseActive; |
| 89 | + |
| 90 | + public AgeClient Client { get; } = client; |
| 91 | + |
| 92 | + public IAgeClient CreateLease() |
| 93 | + { |
| 94 | + if (Interlocked.CompareExchange(ref _leaseActive, 1, 0) == 1) |
| 95 | + { |
| 96 | + throw new InvalidOperationException("An AGE client scope cannot be used concurrently. Await ongoing operations before issuing new ones within the same scope."); |
| 97 | + } |
| 98 | + |
| 99 | + return new ScopedAgeClient(this); |
| 100 | + } |
| 101 | + |
| 102 | + public void ReleaseLease() |
| 103 | + { |
| 104 | + Volatile.Write(ref _leaseActive, 0); |
| 105 | + } |
| 106 | + |
| 107 | + public bool HasActiveLease => Volatile.Read(ref _leaseActive) == 1; |
| 108 | + } |
| 109 | + |
| 110 | + private sealed class ScopedAgeClient(AgeClientFactory.AgeClientScopeState state) : IAgeClient |
| 111 | + { |
| 112 | + private readonly AgeClientScopeState _state = state; |
| 113 | + private bool _disposed; |
| 114 | + |
| 115 | + public bool IsConnected => _state.Client.IsConnected; |
| 116 | + |
| 117 | + public NpgsqlConnection Connection => _state.Client.Connection; |
| 118 | + |
| 119 | + public Task OpenConnectionAsync(CancellationToken cancellationToken = default) => |
| 120 | + _state.Client.OpenConnectionAsync(cancellationToken); |
| 121 | + |
| 122 | + public Task CreateGraphAsync(string graphName, CancellationToken cancellationToken = default) => |
| 123 | + _state.Client.CreateGraphAsync(graphName, cancellationToken); |
| 124 | + |
| 125 | + public Task DropGraphAsync(string graphName, bool cascade = false, CancellationToken cancellationToken = default) => |
| 126 | + _state.Client.DropGraphAsync(graphName, cascade, cancellationToken); |
| 127 | + |
| 128 | + public Task ExecuteCypherAsync(string graph, string cypher, CancellationToken cancellationToken = default) => |
| 129 | + _state.Client.ExecuteCypherAsync(graph, cypher, cancellationToken); |
| 130 | + |
| 131 | + public Task<AgeDataReader> ExecuteQueryAsync(string query, CancellationToken cancellationToken = default, params object?[] parameters) => |
| 132 | + _state.Client.ExecuteQueryAsync(query, cancellationToken, parameters); |
| 133 | + |
| 134 | + public Task CloseConnectionAsync(CancellationToken cancellationToken = default) => |
| 135 | + Task.CompletedTask; |
| 136 | + |
| 137 | + public Task<bool> GraphExistsAsync(string graphName, CancellationToken cancellationToken = default) => |
| 138 | + _state.Client.GraphExistsAsync(graphName, cancellationToken); |
| 139 | + |
| 140 | + public ValueTask DisposeAsync() |
| 141 | + { |
| 142 | + if (_disposed) |
| 143 | + { |
| 144 | + return ValueTask.CompletedTask; |
| 145 | + } |
| 146 | + |
| 147 | + _disposed = true; |
| 148 | + _state.ReleaseLease(); |
| 149 | + return ValueTask.CompletedTask; |
| 150 | + } |
| 151 | + } |
21 | 152 | } |
0 commit comments