Browse Source

Merge pull request #26348 from abpframework/auto-merge-forward/rel-10.7-to-dev-108

Auto-merge forward rel-10.7 → dev
pull/26355/head
Volosoft Agent 3 weeks ago
committed by GitHub
parent
commit
4d8d52a9f7
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 36
      framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs
  2. 17
      framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs
  3. 36
      framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs
  4. 38
      framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs
  5. 36
      framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs
  6. 6
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs
  7. 103
      framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Concurrency_Test.cs

36
framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs

@ -119,13 +119,22 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen
{
var handlerFactories = GetOrCreateHandlerFactories(eventType);
if (factory.IsInFactories(handlerFactories))
var added = handlerFactories.Locking(factories =>
{
if (factory.IsInFactories(factories))
{
return false;
}
factories.Add(factory);
return true;
});
if (!added)
{
return NullDisposable.Instance;
}
handlerFactories.Add(factory);
return new EventHandlerFactoryUnregistrar(this, eventType, factory);
}
@ -134,13 +143,22 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen
{
var handlerFactories = GetOrCreateDynamicHandlerFactories(eventName);
if (handler.IsInFactories(handlerFactories))
var added = handlerFactories.Locking(factories =>
{
if (handler.IsInFactories(factories))
{
return false;
}
factories.Add(handler);
return true;
});
if (!added)
{
return NullDisposable.Instance;
}
handlerFactories.Add(handler);
return new DynamicEventHandlerFactoryUnregistrar(this, eventName, handler);
}
@ -396,12 +414,12 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen
foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key)))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value));
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value.Locking(factories => factories.ToList())));
}
foreach (var handlerFactory in DynamicHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key)))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value));
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList())));
}
return handlerFactoryList.ToArray();
@ -452,7 +470,7 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen
foreach (var handlerFactory in DynamicHandlerFactories.Where(hf => hf.Key == eventName))
{
result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value));
result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList())));
}
return result;

17
framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs

@ -69,13 +69,22 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend
{
var handlerFactories = GetOrCreateHandlerFactories(eventType);
if (factory.IsInFactories(handlerFactories))
var added = handlerFactories.Locking(factories =>
{
if (factory.IsInFactories(factories))
{
return false;
}
factories.Add(factory);
return true;
});
if (!added)
{
return NullDisposable.Instance;
}
handlerFactories.Add(factory);
return new EventHandlerFactoryUnregistrar(this, eventType, factory);
}
@ -273,7 +282,7 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend
foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key)))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value));
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value.Locking(factories => factories.ToList())));
}
return handlerFactoryList.ToArray();

36
framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs

@ -118,13 +118,22 @@ public class KafkaDistributedEventBus : DistributedEventBusBase, ISingletonDepen
{
var handlerFactories = GetOrCreateHandlerFactories(eventType);
if (factory.IsInFactories(handlerFactories))
var added = handlerFactories.Locking(factories =>
{
if (factory.IsInFactories(factories))
{
return false;
}
factories.Add(factory);
return true;
});
if (!added)
{
return NullDisposable.Instance;
}
handlerFactories.Add(factory);
return new EventHandlerFactoryUnregistrar(this, eventType, factory);
}
@ -133,13 +142,22 @@ public class KafkaDistributedEventBus : DistributedEventBusBase, ISingletonDepen
{
var handlerFactories = GetOrCreateDynamicHandlerFactories(eventName);
if (handler.IsInFactories(handlerFactories))
var added = handlerFactories.Locking(factories =>
{
if (handler.IsInFactories(factories))
{
return false;
}
factories.Add(handler);
return true;
});
if (!added)
{
return NullDisposable.Instance;
}
handlerFactories.Add(handler);
return new DynamicEventHandlerFactoryUnregistrar(this, eventName, handler);
}
@ -427,12 +445,12 @@ public class KafkaDistributedEventBus : DistributedEventBusBase, ISingletonDepen
foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key)))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value));
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value.Locking(factories => factories.ToList())));
}
foreach (var handlerFactory in DynamicHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key)))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value));
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList())));
}
return handlerFactoryList.ToArray();
@ -481,7 +499,7 @@ public class KafkaDistributedEventBus : DistributedEventBusBase, ISingletonDepen
foreach (var handlerFactory in DynamicHandlerFactories.Where(hf => hf.Key == eventName))
{
result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value));
result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList())));
}
return result;

38
framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs

@ -138,13 +138,22 @@ public class RabbitMqDistributedEventBus : DistributedEventBusBase, IRabbitMqDis
{
var handlerFactories = GetOrCreateHandlerFactories(eventType);
if (factory.IsInFactories(handlerFactories))
var added = handlerFactories.Locking(factories =>
{
if (factory.IsInFactories(factories))
{
return false;
}
factories.Add(factory);
return true;
});
if (!added)
{
return NullDisposable.Instance;
}
handlerFactories.Add(factory);
if (handlerFactories.Count == 1) //TODO: Multi-threading!
{
Consumer.BindAsync(EventNameAttribute.GetNameOrDefault(eventType));
@ -157,14 +166,23 @@ public class RabbitMqDistributedEventBus : DistributedEventBusBase, IRabbitMqDis
public override IDisposable Subscribe(string eventName, IEventHandlerFactory handler)
{
var handlerFactories = GetOrCreateDynamicHandlerFactories(eventName);
if (handler.IsInFactories(handlerFactories))
var added = handlerFactories.Locking(factories =>
{
if (handler.IsInFactories(factories))
{
return false;
}
factories.Add(handler);
return true;
});
if (!added)
{
return NullDisposable.Instance;
}
handlerFactories.Add(handler);
if (handlerFactories.Count == 1) //TODO: Multi-threading!
{
Consumer.BindAsync(eventName);
@ -494,12 +512,12 @@ public class RabbitMqDistributedEventBus : DistributedEventBusBase, IRabbitMqDis
foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key)))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value));
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value.Locking(factories => factories.ToList())));
}
foreach (var handlerFactory in DynamicHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key)))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value));
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList())));
}
return handlerFactoryList.ToArray();
@ -548,7 +566,7 @@ public class RabbitMqDistributedEventBus : DistributedEventBusBase, IRabbitMqDis
foreach (var handlerFactory in DynamicHandlerFactories.Where(hf => hf.Key == eventName))
{
result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value));
result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList())));
}
return result;

36
framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs

@ -109,13 +109,22 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
{
var handlerFactories = GetOrCreateHandlerFactories(eventType);
if (factory.IsInFactories(handlerFactories))
var added = handlerFactories.Locking(factories =>
{
if (factory.IsInFactories(factories))
{
return false;
}
factories.Add(factory);
return true;
});
if (!added)
{
return NullDisposable.Instance;
}
handlerFactories.Add(factory);
if (handlerFactories.Count == 1) //TODO: Multi-threading!
{
Rebus.Subscribe(eventType);
@ -129,12 +138,21 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
{
var handlerFactories = GetOrCreateDynamicHandlerFactories(eventName);
if (handler.IsInFactories(handlerFactories))
var added = handlerFactories.Locking(factories =>
{
if (handler.IsInFactories(factories))
{
return false;
}
factories.Add(handler);
return true;
});
if (!added)
{
return NullDisposable.Instance;
}
handlerFactories.Add(handler);
if (DynamicHandlerFactories.Count == 1) //TODO: Multi-threading!
{
@ -392,12 +410,12 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key)))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value));
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value.Locking(factories => factories.ToList())));
}
foreach (var handlerFactory in DynamicHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key)))
{
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value));
handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList())));
}
return handlerFactoryList.ToArray();
@ -446,7 +464,7 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen
foreach (var handlerFactory in DynamicHandlerFactories.Where(hf => hf.Key == eventName))
{
result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value));
result.Add(new EventTypeWithEventHandlerFactories(typeof(DynamicEventData), handlerFactory.Value.Locking(factories => factories.ToList())));
}
return result;

6
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs

@ -217,7 +217,7 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency
foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key)))
{
foreach (var factory in handlerFactory.Value)
foreach (var factory in handlerFactory.Value.Locking(factories => factories.ToArray()))
{
handlerFactoryList.Add(new Tuple<IEventHandlerFactory, Type, int>(
factory,
@ -228,7 +228,7 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency
foreach (var handlerFactory in DynamicEventHandlerFactories.Where(aehf => eventNames.Contains(aehf.Key)))
{
foreach (var factory in handlerFactory.Value)
foreach (var factory in handlerFactory.Value.Locking(factories => factories.ToArray()))
{
handlerFactoryList.Add(new Tuple<IEventHandlerFactory, Type, int>(
factory,
@ -252,7 +252,7 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency
foreach (var handlerFactory in DynamicEventHandlerFactories.Where(aehf => aehf.Key == eventName))
{
foreach (var factory in handlerFactory.Value)
foreach (var factory in handlerFactory.Value.Locking(factories => factories.ToArray()))
{
using var handler = factory.GetHandler();
var handlerType = handler.EventHandler.GetType();

103
framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/EventBus_Concurrency_Test.cs

@ -0,0 +1,103 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
namespace Volo.Abp.EventBus.Local;
public class EventBus_Concurrency_Test : EventBusTestBase
{
private const int PublishCount = 10_000;
[Fact]
public async Task Should_Publish_While_Other_Handlers_Subscribe_And_Unsubscribe()
{
var handledCount = 0;
using var subscription = LocalEventBus.Subscribe<MySimpleEventData>(_ =>
{
Interlocked.Increment(ref handledCount);
return Task.CompletedTask;
});
await PublishWhileChurningAsync(
() => LocalEventBus.Subscribe<MySimpleEventData>(_ => Task.CompletedTask).Dispose(),
() => LocalEventBus.PublishAsync(new MySimpleEventData(1), onUnitOfWorkComplete: false));
handledCount.ShouldBe(PublishCount);
}
[Fact]
public async Task Should_Publish_While_Dynamic_Handlers_Of_The_Same_Event_Subscribe_And_Unsubscribe()
{
var handledCount = 0;
using var subscription = LocalEventBus.Subscribe<MySimpleEventData>(_ =>
{
Interlocked.Increment(ref handledCount);
return Task.CompletedTask;
});
var eventName = EventNameAttribute.GetNameOrDefault<MySimpleEventData>();
await PublishWhileChurningAsync(
() => LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(
new ActionEventHandler<DynamicEventData>(_ => Task.CompletedTask))).Dispose(),
() => LocalEventBus.PublishAsync(new MySimpleEventData(1), onUnitOfWorkComplete: false));
handledCount.ShouldBe(PublishCount);
}
[Fact]
public async Task Should_Publish_Dynamic_Event_While_Other_Handlers_Subscribe_And_Unsubscribe()
{
const string eventName = "MyConcurrencyTestDynamicEvent";
var handledCount = 0;
using var subscription = LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(
new ActionEventHandler<DynamicEventData>(_ =>
{
Interlocked.Increment(ref handledCount);
return Task.CompletedTask;
})));
await PublishWhileChurningAsync(
() => LocalEventBus.Subscribe(eventName, new SingleInstanceHandlerFactory(
new ActionEventHandler<DynamicEventData>(_ => Task.CompletedTask))).Dispose(),
() => LocalEventBus.PublishAsync(eventName, new { Value = 1 }, onUnitOfWorkComplete: false));
handledCount.ShouldBe(PublishCount);
}
private static async Task PublishWhileChurningAsync(Action subscribeAndUnsubscribe, Func<Task> publish)
{
var churnStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var churnCount = 0;
using var cts = new CancellationTokenSource();
var churn = Task.Run(() =>
{
churnStarted.TrySetResult();
while (!cts.IsCancellationRequested)
{
subscribeAndUnsubscribe();
Interlocked.Increment(ref churnCount);
}
});
try
{
await churnStarted.Task.WaitAsync(TimeSpan.FromSeconds(10));
for (var i = 0; i < PublishCount; i++)
{
await publish();
}
}
finally
{
cts.Cancel();
await churn;
}
churnCount.ShouldBeGreaterThan(0);
}
}
Loading…
Cancel
Save