Skip to content

minor fix #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<PropertyGroup>
<Authors>Haiping Chen</Authors>
<Company>SciSharp STACK</Company>
<Product>LL Application Framework</Product>
<Product>AI Agent Application Framework</Product>
<Description>
Open source LLM application framework to build scalable, flexible and robust AI system.
</Description>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/SciSharp/BotSharp</RepositoryUrl>
<PackageTags>Chatbot, Bot, LLM, AI, ChatGPT, OpenAI</PackageTags>
<PackageTags>Chatbot, Agent, LLM, AI, ChatGPT, OpenAI, Semantic</PackageTags>
<PackageReleaseNotes>Support dialogue status tracking.</PackageReleaseNotes>
<Copyright>Since 2018 Haiping Chen</Copyright>
<PackageProjectUrl>https://github.com/SciSharp/BotSharp</PackageProjectUrl>
Expand Down Expand Up @@ -177,12 +177,12 @@

<ItemGroup>
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.4.2" />
<PackageReference Include="Fluid.Core" Version="2.8.0" />
<PackageReference Include="Fluid.Core" Version="2.11.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Nanoid" Version="3.0.0" />
<PackageReference Include="RedLock.net" Version="2.3.2" />
<PackageReference Include="Nanoid" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
using RedLockNet;
using RedLockNet.SERedis;
using RedLockNet.SERedis.Configuration;
using Medallion.Threading.Redis;
using StackExchange.Redis;

namespace BotSharp.Core.Infrastructures;

public class DistributedLocker
{
private readonly BotSharpDatabaseSettings _settings;
private readonly RedLockFactory _lockFactory;

public DistributedLocker(/*BotSharpDatabaseSettings settings*/)
public DistributedLocker(BotSharpDatabaseSettings settings)
{
// _settings = settings;
_settings = settings;
}

public async Task Lock(string resource, Func<Task> action, int timeoutInSeconds = 30)
{
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);

var multiplexers = new List<RedLockMultiplexer>();
foreach (var x in "".Split(';'))
var connection = await ConnectionMultiplexer.ConnectAsync(_settings.Redis);
var @lock = new RedisDistributedLock(resource, connection.GetDatabase());
await using (var handle = await @lock.TryAcquireAsync(timeout))
{
var option = new ConfigurationOptions
if (handle != null)
{
await action();
}
else
{
AbortOnConnectFail = false,
EndPoints = { x }
};
var _connMuliplexer = ConnectionMultiplexer.Connect(option);
multiplexers.Add(_connMuliplexer);
Serilog.Log.Logger.Error($"Acquire lock for {resource} failed due to after {timeout}s timeout.");
}
}

_lockFactory = RedLockFactory.Create(multiplexers);
}

public async Task Lock(string resource, Func<Task> action)
public async Task Lock(string resource, Action action, int timeoutInSeconds = 30)
{
var expiry = TimeSpan.FromSeconds(60);
var wait = TimeSpan.FromSeconds(30);
var retry = TimeSpan.FromSeconds(3);
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);

await using (var redLock = await _lockFactory.CreateLockAsync(resource, expiry, wait, retry))
var connection = await ConnectionMultiplexer.ConnectAsync(_settings.Redis);
var @lock = new RedisDistributedLock(resource, connection.GetDatabase());
await using (var handle = await @lock.TryAcquireAsync(timeout))
{
if (redLock.IsAcquired)
if (handle != null)
{
await action();
action();
}
else
{
Console.WriteLine($"Acquire locak failed due to {resource} after {wait}s timeout.");
Serilog.Log.Logger.Error($"Acquire lock for {resource} failed due to after {timeout}s timeout.");
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/Infrastructure/BotSharp.Core/Repository/RepositoryPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class RepositoryPlugin : IBotSharpPlugin

public void RegisterDI(IServiceCollection services, IConfiguration config)
{
var myDatabaseSettings = new BotSharpDatabaseSettings();
config.Bind("Database", myDatabaseSettings);

// In order to use EntityFramework.BootKit in other plugin
services.AddScoped(provider =>
{
Expand All @@ -25,14 +28,8 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
return settingService.Bind<DatabaseBasicSettings>("Database");
});

services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<BotSharpDatabaseSettings>("Database");
});
services.AddSingleton(provider => myDatabaseSettings);

var myDatabaseSettings = new BotSharpDatabaseSettings();
config.Bind("Database", myDatabaseSettings);
if (myDatabaseSettings.Default == RepositoryEnum.FileRepository)
{
services.AddScoped<IBotSharpRepository, FileRepository>();
Expand Down