Skip to content

Commit

Permalink
Fix LiteDb connections management, update version to 1.0.25
Browse files Browse the repository at this point in the history
  • Loading branch information
matsakiv committed May 25, 2023
1 parent 7c15715 commit 6418515
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Beacon.Sdk/Beacon.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Authors>Mikhail Tatarenko</Authors>
<Product>Beacon.Sdk</Product>
<Description>Beacon .NET SDK for Tezos wallet / dApps developers.</Description>
<Version>1.0.24</Version>
<Version>1.0.25</Version>
<Copyright>Copyright © Baking Bad 2019-2022</Copyright>
<Nullable>enable</Nullable>
<TargetFramework>netstandard2.1</TargetFramework>
Expand Down
1 change: 1 addition & 0 deletions Beacon.Sdk/BeaconClients/Abstract/IBaseBeaconClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IBaseBeaconClient
{
event EventHandler<BeaconMessageEventArgs> OnBeaconMessageReceived;
event EventHandler<ConnectedClientsListChangedEventArgs?> OnConnectedClientsListChanged;
event Action OnDisconnected;

Task InitAsync();
void Connect();
Expand Down
3 changes: 3 additions & 0 deletions Beacon.Sdk/BeaconClients/BaseBeaconClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public abstract class BaseBeaconClient : IBaseBeaconClient
private readonly KeyPairService _keyPairService;
public event EventHandler<BeaconMessageEventArgs> OnBeaconMessageReceived;
public event EventHandler<ConnectedClientsListChangedEventArgs?> OnConnectedClientsListChanged;
public event Action OnDisconnected;

protected void RaiseOnBeaconMessageReceived(BeaconMessageEventArgs e)
{
Expand Down Expand Up @@ -122,6 +123,8 @@ public void Disconnect()
P2PCommunicationService.OnP2PMessagesReceived -= OnP2PMessagesReceived;
SerializeMessageHandler.OnPermissionsCreated -= ClientPermissionsCreatedHandler;
Connected = P2PCommunicationService.Syncing;

OnDisconnected?.Invoke();
}

private void ClientPermissionsCreatedHandler(object sender, ConnectedClientsListChangedEventArgs e)
Expand Down
21 changes: 18 additions & 3 deletions Beacon.Sdk/BeaconClients/BeaconClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Beacon.Sdk.BeaconClients
{
using Abstract;
using Core.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

Expand All @@ -9,7 +10,6 @@ public static class BeaconClientFactory
public static T Create<T>(
BeaconOptions? options,
ILoggerProvider? loggerProvider = null) where T : notnull

{
var beaconServices = new ServiceCollection();

Expand All @@ -19,8 +19,23 @@ public static T Create<T>(
if (typeof(T) == typeof(IDappBeaconClient))
beaconServices.AddBeaconDappClient(options, loggerProvider);

ServiceProvider? beaconServicesProvider = beaconServices.BuildServiceProvider();
return beaconServicesProvider.GetRequiredService<T>();
var beaconServicesProvider = beaconServices.BuildServiceProvider();
var service = beaconServicesProvider.GetRequiredService<T>();

if (service is IBaseBeaconClient client)
{
var connectionPool = beaconServicesProvider.GetRequiredService<ILiteDbConnectionPool>();

if (connectionPool != null)
{
client.OnDisconnected += () =>
{
connectionPool.CloseAllConnections(); // close all LiteDb connections after disconnected event
};
}
}

return service;
}
}
}
31 changes: 21 additions & 10 deletions Beacon.Sdk/Core/Infrastructure/Repositories/BaseLiteDbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@ namespace Beacon.Sdk.Core.Infrastructure.Repositories
public abstract class BaseLiteDbRepository<T>
{
private readonly ILogger<BaseLiteDbRepository<T>> _logger;
private readonly ILiteDatabase _db;
private readonly ILiteDbConnectionPool _connectionPool;
private readonly RepositorySettings _settings;

protected BaseLiteDbRepository(ILiteDbConnectionPool connectionPool, ILogger<BaseLiteDbRepository<T>> logger, RepositorySettings settings)
protected BaseLiteDbRepository(
ILiteDbConnectionPool connectionPool,
ILogger<BaseLiteDbRepository<T>> logger,
RepositorySettings settings)
{
_connectionPool = connectionPool ?? throw new ArgumentNullException(nameof(connectionPool));
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
_logger = logger;
_db = connectionPool.OpenConnection(new ConnectionString(settings.ConnectionString));
}

protected Task InConnectionAction(string collectionName, Action<ILiteCollection<T>> func)
{
try
{
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
var col = db.GetCollection<T>(collectionName);
func(col);
}
catch (Exception e)
Expand All @@ -36,7 +42,8 @@ protected Task<T> InConnection(string collectionName, Func<ILiteCollection<T>, T
{
try
{
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
var col = db.GetCollection<T>(collectionName);
return func(col);
}
catch (Exception e)
Expand All @@ -50,8 +57,9 @@ protected Task InConnection(string collectionName, Action<ILiteDatabase, ILiteCo
{
try
{
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
func(_db, col);
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
var col = db.GetCollection<T>(collectionName);
func(db, col);
}
catch (Exception e)
{
Expand All @@ -65,7 +73,8 @@ protected Task InConnection(string collectionName, Action<ILiteDatabase, ILiteCo
{
try
{
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
var col = db.GetCollection<T>(collectionName);
return func(col);

}
Expand All @@ -81,7 +90,8 @@ protected Task InConnection(string collectionName, Action<ILiteDatabase, ILiteCo
{
try
{
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
var col = db.GetCollection<T>(collectionName);
return func(col);

}
Expand All @@ -97,7 +107,8 @@ protected Task<List<T>> InConnection(string collectionName, Func<ILiteCollection
{
try
{
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
var col = db.GetCollection<T>(collectionName);
return func(col);
}
catch (Exception e)
Expand Down

0 comments on commit 6418515

Please sign in to comment.