diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs index e04c2488..465644ad 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs @@ -6,7 +6,7 @@ public interface IEventSubscriber { Task SubscribeAsync(string channel, Func received); - Task SubscribeAsync(string channel, string group, bool priorityEnabled, + Task SubscribeAsync(string channel, string group, int? port, bool priorityEnabled, Func received, CancellationToken? stoppingToken = null); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs index b3c26033..19f5b141 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs @@ -2,18 +2,19 @@ namespace BotSharp.Abstraction.Repositories; public class BotSharpDatabaseSettings : DatabaseBasicSettings { - public string[] Assemblies { get; set; } - public string FileRepository { get; set; } - public string BotSharpMongoDb { get; set; } - public string TablePrefix { get; set; } - public DbConnectionSetting BotSharp { get; set; } - public string Redis { get; set; } + public string[] Assemblies { get; set; } = []; + public string FileRepository { get; set; } = string.Empty; + public string BotSharpMongoDb { get; set; } = string.Empty; + public string TablePrefix { get; set; } = string.Empty; + public DbConnectionSetting BotSharp { get; set; } = new(); + public string Redis { get; set; } = string.Empty; + public bool EnableReplica { get; set; } = true; } public class DatabaseBasicSettings { - public string Default { get; set; } - public DbConnectionSetting DefaultConnection { get; set; } + public string Default { get; set; } = string.Empty; + public DbConnectionSetting DefaultConnection { get; set; } = new(); public bool EnableSqlLog { get; set; } public bool EnableSensitiveDataLogging { get; set; } public bool EnableRetryOnFailure { get; set; } @@ -23,9 +24,11 @@ public class DbConnectionSetting { public string Master { get; set; } public string[] Slavers { get; set; } + public int ConnectionTimeout { get; set; } = 30; + public int ExecutionTimeout { get; set; } = 30; public DbConnectionSetting() { - Slavers = new string[0]; + Slavers = []; } } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/MathExt.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/MathExt.cs new file mode 100644 index 00000000..4ea18179 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/MathExt.cs @@ -0,0 +1,14 @@ +namespace BotSharp.Abstraction.Utilities; + +public static class MathExt +{ + public static int Max(int a, int b, int c) + { + return Math.Max(Math.Max(a, b), c); + } + + public static long Max(long a, long b, long c) + { + return Math.Max(Math.Max(a, b), c); + } +} diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 1c82c84f..1c9955aa 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -190,7 +190,7 @@ - + diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs index 34ff0079..9a3c9982 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs @@ -25,7 +25,7 @@ public class RedisSubscriber : IEventSubscriber }); } - public async Task SubscribeAsync(string channel, string group, bool priorityEnabled, + public async Task SubscribeAsync(string channel, string group, int? port, bool priorityEnabled, Func received, CancellationToken? stoppingToken = null) { @@ -42,6 +42,12 @@ public class RedisSubscriber : IEventSubscriber await CreateConsumerGroup(db, channel, group); } + var consumer = Environment.MachineName; + if (port.HasValue) + { + consumer += $"-{port}"; + } + while (true) { await Task.Delay(100); @@ -54,28 +60,28 @@ public class RedisSubscriber : IEventSubscriber if (priorityEnabled) { - if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0) + if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, consumer, received) > 0) { continue; } - if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, received) > 0) + if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, consumer, received) > 0) { continue; } - await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, received); + await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, consumer, received); } else { - await HandleGroupMessage(db, channel, group, received); + await HandleGroupMessage(db, channel, group, consumer, received); } } } - private async Task HandleGroupMessage(IDatabase db, string channel, string group, Func received) + private async Task HandleGroupMessage(IDatabase db, string channel, string group, string consumer, Func received) { - var entries = await db.StreamReadGroupAsync(channel, group, Environment.MachineName, count: 1); + var entries = await db.StreamReadGroupAsync(channel, group, consumer, count: 1); foreach (var entry in entries) { _logger.LogInformation($"Consumer {Environment.MachineName} received: {channel} {entry.Values[0].Value}"); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj index ab14d864..88709238 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj @@ -11,8 +11,7 @@ - - +