我正在try 使用Pusher通道从我的服务器向我的用户发送消息.我的API收到一个用户列表,消息需要发送给列表中的所有用户.我不能将这些用户分组到单个频道中,每个用户都必须使用一个单独的频道.这会使我的API变慢,因为用户列表的大小可以在1到10000之间(future 可能更大),而推送器批处理事件只能接受大小为10的事件列表.
我正在使用.Net 6作为我的API
我try 使用batch events来try 提高性能;我的代码如下所示,
var events = new List<Event>();
// count can be anything between 1 and 10000
for (int i = 1; i <= count; i++)
{
// creating a sample list of events
events.Add(new Event
{
Channel = string.Format("batch-channel-{0}", i),
EventName = "batch-event",
Data = new
{
Channel = string.Format("batch-channel-{0}", i),
Event = "batch-event",
Message = string.Format("{0} - sample message", i)
{
});
}
var result = new List<HttpStatusCode>();
int chunkSize = 10;
int totalChunks = (int)Math.Ceiling((double)events.Length / chunkSize);
for (int i = 0; i < totalChunks; i++)
{
var eventsChunk = events.Skip(i * chunkSize).Take(chunkSize).ToArray();
// publishing event lists of size 10
var chunkResult = await pusher.TriggerAsync(eventsChunk);
result.Add(chunkResult.StatusCode);
}
我用一个大小为10000的事件列表测试了这段代码,它大约需要6分钟才能完成执行.我想知道我是否遗漏了什么,以及我是否可以以某种方式提高表现.
如有任何帮助,我们不胜感激.谢谢.