private void InitTimeCircle() { //get all subscribes, 以确定数组长度:单位时间为最小公约数,长度为最小公倍数/单位时间 var subscribes = _cacheService.GetAllSubscribes(); var intervalList = subscribes.Where(p => p.ReportInterval != 0).Select(p => p.ReportInterval).ToList(); // 3/10 var gcd = Gcd(intervalList); var lcm = Lcm(intervalList); count = lcm / gcd; timeCircle = new TimeNode[count]; for (int j = 0; j < count; j++) { timeCircle[j] = new TimeNode(); for (int i = 0; i < subscribes.Count; i++) { if (subscribes[i].ReportInterval == 0) { subscribes[i].ReportInterval = lcm; } //初始位置不挂载 if ((j + 1) * gcd % subscribes[i].ReportInterval == 0) { timeCircle[j].Subscribes.Add(subscribes[i].SubscribeID); } } } _timer = new Timer(Tick, null, TimeSpan.FromSeconds(gcd), TimeSpan.FromSeconds(gcd)); }
//当时间拨动到某个TimeNode上时,依次处理该时间点需要发送的订阅 private void HandleNode(TimeNode node) { foreach (var subscribe in node.Subscribes) { var notification = PrepareNotificationBySubscribeId(subscribe); DoPost(notification); } }