Merge pull request #547 from Qtoss-AI/master

minor fix
This commit is contained in:
C. Oceania 2024-07-17 07:09:49 -05:00 committed by GitHub
commit 2dee52d870
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 38 additions and 39 deletions

View file

@ -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>
@ -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>

View file

@ -1,6 +1,4 @@
using RedLockNet;
using RedLockNet.SERedis;
using RedLockNet.SERedis.Configuration;
using Medallion.Threading.Redis;
using StackExchange.Redis;
namespace BotSharp.Core.Infrastructures;
@ -8,42 +6,46 @@ 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;
var multiplexers = new List<RedLockMultiplexer>();
foreach (var x in "".Split(';'))
{
var option = new ConfigurationOptions
{
AbortOnConnectFail = false,
EndPoints = { x }
};
var _connMuliplexer = ConnectionMultiplexer.Connect(option);
multiplexers.Add(_connMuliplexer);
}
_lockFactory = RedLockFactory.Create(multiplexers);
_settings = settings;
}
public async Task Lock(string resource, Func<Task> action)
public async Task Lock(string resource, Func<Task> 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();
}
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.");
}
}
}
public async Task Lock(string resource, Action action, int timeoutInSeconds = 30)
{
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);
var connection = await ConnectionMultiplexer.ConnectAsync(_settings.Redis);
var @lock = new RedisDistributedLock(resource, connection.GetDatabase());
await using (var handle = await @lock.TryAcquireAsync(timeout))
{
if (handle != null)
{
action();
}
else
{
Serilog.Log.Logger.Error($"Acquire lock for {resource} failed due to after {timeout}s timeout.");
}
}
}

View file

@ -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 =>
{
@ -25,14 +28,8 @@ public class RepositoryPlugin : IBotSharpPlugin
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>();