Allow to disable EventPriority
This commit is contained in:
parent
11ceb8e013
commit
597e26a1e0
|
|
@ -10,7 +10,7 @@ public interface IEventPublisher
|
|||
/// <returns></returns>
|
||||
Task BroadcastAsync(string channel, string message);
|
||||
|
||||
Task PublishAsync(string channel, string message, EventPriority priority = EventPriority.Medium);
|
||||
Task PublishAsync(string channel, string message, EventPriority? priority = null);
|
||||
|
||||
Task ReDispatchAsync(string channel, int count = 10, string order = "asc");
|
||||
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ public interface IEventSubscriber
|
|||
{
|
||||
Task SubscribeAsync(string channel, Func<string, string, Task> received);
|
||||
|
||||
Task SubscribeAsync(string channel, string group, Func<string, string, Task> received);
|
||||
Task SubscribeAsync(string channel, string group, bool priorityEnabled, Func<string, string, Task> received);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,12 +21,15 @@ public class RedisPublisher : IEventPublisher
|
|||
await _subscriber.PublishAsync(channel, message);
|
||||
}
|
||||
|
||||
public async Task PublishAsync(string channel, string message, EventPriority priority = EventPriority.Medium)
|
||||
public async Task PublishAsync(string channel, string message, EventPriority? priority = null)
|
||||
{
|
||||
var db = _redis.GetDatabase();
|
||||
|
||||
// convert to apporiate channel by priority
|
||||
channel = $"{channel}-{priority}";
|
||||
if (priority != null)
|
||||
{
|
||||
channel = $"{channel}-{priority}";
|
||||
}
|
||||
|
||||
if (CheckMessageExists(db, channel, "message", message))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -25,27 +25,41 @@ public class RedisSubscriber : IEventSubscriber
|
|||
});
|
||||
}
|
||||
|
||||
public async Task SubscribeAsync(string channel, string group, Func<string, string, Task> received)
|
||||
public async Task SubscribeAsync(string channel, string group, bool priorityEnabled, Func<string, string, Task> received)
|
||||
{
|
||||
var db = _redis.GetDatabase();
|
||||
|
||||
await CreateConsumerGroup(db, $"{channel}-{EventPriority.Low}", group);
|
||||
await CreateConsumerGroup(db, $"{channel}-{EventPriority.Medium}", group);
|
||||
await CreateConsumerGroup(db, $"{channel}-{EventPriority.High}", group);
|
||||
if (priorityEnabled)
|
||||
{
|
||||
await CreateConsumerGroup(db, $"{channel}-{EventPriority.Low}", group);
|
||||
await CreateConsumerGroup(db, $"{channel}-{EventPriority.Medium}", group);
|
||||
await CreateConsumerGroup(db, $"{channel}-{EventPriority.High}", group);
|
||||
}
|
||||
else
|
||||
{
|
||||
await CreateConsumerGroup(db, channel, group);
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0)
|
||||
if (priorityEnabled)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, received) > 0)
|
||||
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, received) > 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, received);
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
await HandleGroupMessage(db, channel, group, received);
|
||||
}
|
||||
|
||||
await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, received);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue