-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBreezeEngineProxy.cs
254 lines (243 loc) · 8.83 KB
/
DBreezeEngineProxy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System.Collections.Concurrent;
using DBreeze.Transactions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace DBreeze.AspNet;
public record ProxyState<T>(T Instance, DateTimeOffset Created, TimeSpan Timeout);
public class DBreezeEngineProxy : IDBreezeEngineProxy, IDBreezeEngineProxyChildFactory
{
private readonly ILogger<DBreezeEngineProxy> _logger;
private readonly DBreezeConfiguration _configuration;
private volatile DBreezeEngine? _engine;
private readonly ManualResetEvent _manualReset = new(true);
private bool _disposed = false;
public ulong EngineCalls => _engineCalls;
private ulong _engineCalls = 0U;
public ulong EngineCreated => _engineCreated;
private ulong _engineCreated = 0U;
public ulong Transactions => _transactions;
private ulong _transactions = 0U;
private bool _disposeEngineIfIdle = false;
private readonly ConcurrentDictionary<int, ProxyState<IScheme>> _nonDisposedSchemes = new();
private readonly ConcurrentDictionary<int, ProxyState<IDBreezeEngineProxy>> _nonDisposedEngineProxies = new();
public DBreezeEngineProxy(IOptions<DBreezeConfiguration> options, ILogger<DBreezeEngineProxy> logger)
{
_logger = logger;
_configuration = options.Value;
}
private DBreezeEngine GetEngine(CancellationToken? ctx = null)
{
ThrowIfDisposed();
using var source = ctx is null ? new CancellationTokenSource() : CancellationTokenSource.CreateLinkedTokenSource(ctx.Value);
source.CancelAfter(TimeSpan.FromSeconds(15));
var token = source.Token;
try
{
WaitHandle.WaitAny(new []{token.WaitHandle, _manualReset});
Interlocked.Increment(ref _engineCalls);
_logger.LogDebug("Database Called {EngineCalls}", EngineCalls);
token.ThrowIfCancellationRequested();
if (_engine is null || !Open)
{
RemoveStaleProxies();
_engine = new DBreezeEngine(_configuration);
Interlocked.Increment(ref _engineCreated);
_logger.LogDebug("Created new Database Connection {EngineCreated}", EngineCreated);
}
_manualReset.Set();
return _engine;
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception e)
{
_logger.LogError(e, "The Database Has an Error during Fetching");
Thread.Sleep(100);
return GetEngine(token);
}
}
public Transaction GetTransaction(eTransactionTablesLockTypes tablesLockType,
params string[] tables)
{
ThrowIfDisposed();
Interlocked.Increment(ref _transactions);
_logger.LogDebug("Creating Transaction {Transactions}", Transactions);
return GetEngine().GetTransaction(tablesLockType, tables);
}
private Scheme GetScheme() => GetEngine().Scheme;
public IScheme Scheme()
{
ThrowIfDisposed();
lock (_nonDisposedSchemes)
{
var proxy = new SchemeProxy(this, GetScheme());
_nonDisposedSchemes.TryAdd(proxy.GetHashCode(), new ProxyState<IScheme>(proxy, DateTimeOffset.Now, TimeSpan.FromMinutes(1)));
return proxy;
}
}
public void Dispose()
{
if (_disposed) return;
_manualReset.WaitOne();
_disposed = true;
_engine?.Dispose();
RemoveStaleProxies();
_manualReset.Dispose();
}
public void Dispose(IScheme proxy)
{
lock (_nonDisposedSchemes)
{
var hashCode = proxy.GetHashCode();
_nonDisposedSchemes.TryRemove(hashCode, out var result);
LogDisposeResults(result, hashCode);
DisposeEngineIfNotUsed();
}
}
public void Dispose(IDBreezeEngineProxy proxy)
{
lock (_nonDisposedEngineProxies)
{
var hashCode = proxy.GetHashCode();
_nonDisposedEngineProxies.TryRemove(hashCode, out var result);
LogDisposeResults(result, hashCode);
DisposeEngineIfNotUsed();
}
}
private void LogDisposeResults<T>(ProxyState<T>? result, int hashCode)
{
if (result != null)
{
var msTime = (DateTimeOffset.Now - result.Created).TotalMilliseconds;
_logger.LogDebug("Removed Proxy: {HashCode} - Time Running: {MsTime}", hashCode, msTime);
}
else
{
_logger.LogWarning("Removed untracked Proxy: {HashCode}", hashCode);
}
}
private void DisposeEngineIfNotUsed()
{
if (_nonDisposedSchemes.IsEmpty && _nonDisposedEngineProxies.IsEmpty && _disposeEngineIfIdle)
{
_logger.LogDebug("Everything is Disposed, Closing Engine!");
_engine?.Dispose();
}
else
{
var schemesNum = _nonDisposedSchemes.Count;
var enginesNum = _nonDisposedEngineProxies.Count;
_logger.LogDebug("Engine is still used, Engine Keeps Running - Schemes: {SchemesNum}, Engines: {EnginesNum}", schemesNum, enginesNum);
}
}
private void ThrowIfDisposed()
{
if (_disposed) throw new ObjectDisposedException("This Proxy is disposed");
}
public IDBreezeEngineProxy CreateDisposableChild()
{
ThrowIfDisposed();
lock (_nonDisposedEngineProxies)
{
var proxy = new ProxyChild(this);
_nonDisposedEngineProxies.TryAdd(proxy.GetHashCode(), new ProxyState<IDBreezeEngineProxy>(proxy, DateTimeOffset.Now, TimeSpan.FromMinutes(1)));
return proxy;
}
}
/// <summary>
/// Check if Engine is Open for Operations
/// </summary>
/// <remarks>
/// NOT THREAD SAFE
/// </remarks>
public bool Open => _engine is { Disposed: false, DBisOperable: true };
public void RemoveStaleProxies()
{
lock (_nonDisposedSchemes)
{
if (Open)
{
var entries = FindStaleProxies(_nonDisposedSchemes);
foreach (var entry in entries)
{
_logger.LogWarning("Removed Overdue Proxy! Check For indisposed SchemeProxies in your Code!!");
_nonDisposedSchemes.TryRemove(entry.Key, out _);
}
}
else if(!_nonDisposedSchemes.IsEmpty)
{
_logger.LogWarning("Engine Is Not Open For Operations, All Proxies Are Stale! Check For indisposed SchemeProxies in your Code!!");
_nonDisposedSchemes.Clear();
}
}
lock (_nonDisposedEngineProxies)
{
if (Open)
{
var entries = FindStaleProxies(_nonDisposedEngineProxies);
foreach (var entry in entries)
{
_nonDisposedEngineProxies.TryRemove(entry.Key, out _);
_logger.LogWarning("Removed Overdue Proxy! Check For indisposed EngineProxies in your Code!!");
}
}
else if(!_nonDisposedEngineProxies.IsEmpty)
{
_logger.LogWarning("Engine Is Not Open For Operations, All Proxies Are Stale! Check For indisposed EngineProxies in your Code!!");
_nonDisposedEngineProxies.Clear();
}
}
DisposeEngineIfNotUsed();
}
static IEnumerable<KeyValuePair<int, ProxyState<T>>> FindStaleProxies<T>(ConcurrentDictionary<int, ProxyState<T>> proxies)
where T : IDisposable
{
var currentTime = DateTimeOffset.Now;
return from entry in proxies
let state = entry.Value
let processTime = currentTime - state.Created
where processTime > state.Timeout
select entry;
}
private class ProxyChild : IDBreezeEngineProxy
{
private readonly DBreezeEngineProxy _proxy;
private readonly IScheme _scheme;
private bool _disposed = false;
public ProxyChild(DBreezeEngineProxy proxy)
{
_proxy = proxy;
_scheme = _proxy.Scheme();
}
~ProxyChild()
{
if (!_disposed)
{
_proxy.Dispose(this);
}
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
_scheme.Dispose();
_proxy.Dispose(this);
}
private void ThrowIfDisposed()
{
if (_disposed) throw new ObjectDisposedException("This Proxy is disposed");
}
public Transaction GetTransaction(eTransactionTablesLockTypes tablesLockType, params string[] tables)
{
ThrowIfDisposed();
return _proxy.GetTransaction(tablesLockType, tables);
}
public IScheme Scheme()
{
ThrowIfDisposed();
return _scheme;
}
}
}