40 lines
1.5 KiB
C#
40 lines
1.5 KiB
C#
using System.Threading.Channels;
|
||
|
||
namespace Service.Implement
|
||
{
|
||
/// <summary>
|
||
/// 告警通知事件(AlertId:告警消息Id;IsNewAlert:是否新告警,false 表示合并触发的重复告警)
|
||
/// </summary>
|
||
/// <param name="AlertId">告警消息 Id</param>
|
||
/// <param name="IsNewAlert">是否新告警</param>
|
||
public record AlertNotifyEvent(long AlertId, bool IsNewAlert);
|
||
|
||
/// <summary>
|
||
/// 告警通知内存总线(生产者:AlertCenterService 上报告警后入队;消费者:AlertNotifyWorker 后台推送)
|
||
/// 采用静态单例 Channel,与项目 SqlSugarContext.DbContext 静态单例风格一致;
|
||
/// 进程重启丢失队列可接受(告警已落库,页面仍可查询处理)
|
||
/// </summary>
|
||
public static class AlertNotifyBus
|
||
{
|
||
private static readonly Channel<AlertNotifyEvent> _channel =
|
||
Channel.CreateUnbounded<AlertNotifyEvent>(new UnboundedChannelOptions
|
||
{
|
||
SingleReader = true,
|
||
SingleWriter = false
|
||
});
|
||
|
||
/// <summary>
|
||
/// 队列读取端(供 AlertNotifyWorker 消费)
|
||
/// </summary>
|
||
public static ChannelReader<AlertNotifyEvent> Reader => _channel.Reader;
|
||
|
||
/// <summary>
|
||
/// 发布告警通知事件(非阻塞,队列满不会发生——无界队列)
|
||
/// </summary>
|
||
public static void Publish(long alertId, bool isNewAlert)
|
||
{
|
||
_channel.Writer.TryWrite(new AlertNotifyEvent(alertId, isNewAlert));
|
||
}
|
||
}
|
||
}
|