Skip to content

Commit

Permalink
Avoid error for Redis.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oceania2018 committed Jan 2, 2025
1 parent ac78e6d commit b959508
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<BotSharpVersion>3.0.0</BotSharpVersion>
<BotSharpVersion>4.0.0</BotSharpVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ It's written in C# running on .Net Core that is full cross-platform framework, t
* Built-in multi-agents and conversation with state management.
* Support multiple LLM Planning approaches to handle different tasks from simple to complex.
* Built-in RAG related interfaces, Memory based vector searching.
* Support multiple AI platforms (ChatGPT 3.5 / 4.0, PaLM 2, LLaMA 3, Claude Sonnet 3.5, HuggingFace).
* Support multiple AI platforms (ChatGPT 3.5/ 4o/ o1, Gemini 2, LLaMA 3, Claude Sonnet 3.5, HuggingFace).
* Allow multiple agents with different responsibilities cooperate to complete complex tasks.
* Build, test, evaluate and audit your LLM agent in one place.
* Build-in `BotSharp UI` written in [SvelteKit](https://kit.svelte.dev/).
Expand Down
5 changes: 5 additions & 0 deletions src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ private static void AddRedisEvents(IServiceCollection services, IConfiguration c
var dbSettings = new BotSharpDatabaseSettings();
config.Bind("Database", dbSettings);

if (string.IsNullOrEmpty(dbSettings.Redis))
{
return;
}

services.AddSingleton<IConnectionMultiplexer>(ConnectionMultiplexer.Connect(dbSettings.Redis));
services.AddSingleton<IEventPublisher, RedisPublisher>();
services.AddSingleton<IEventSubscriber, RedisSubscriber>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ namespace BotSharp.Core.Infrastructures;

public class DistributedLocker : IDistributedLocker
{
private readonly IConnectionMultiplexer _redis;
private readonly IServiceProvider _services;
private readonly ILogger _logger;

public DistributedLocker(IConnectionMultiplexer redis, ILogger<DistributedLocker> logger)
public DistributedLocker(IServiceProvider services, ILogger<DistributedLocker> logger)
{
_redis = redis;
_services = services;
_logger = logger;
}

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

var @lock = new RedisDistributedLock(resource, _redis.GetDatabase());
var redis = _services.GetRequiredService<IConnectionMultiplexer>();
var @lock = new RedisDistributedLock(resource, redis.GetDatabase());
await using (var handle = await @lock.TryAcquireAsync(timeout))
{
if (handle == null)
Expand All @@ -37,7 +38,8 @@ public bool Lock(string resource, Action action, int timeoutInSeconds = 30)
{
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);

var @lock = new RedisDistributedLock(resource, _redis.GetDatabase());
var redis = _services.GetRequiredService<IConnectionMultiplexer>();
var @lock = new RedisDistributedLock(resource, redis.GetDatabase());
using (var handle = @lock.TryAcquire(timeout))
{
if (handle == null)
Expand Down

0 comments on commit b959508

Please sign in to comment.