是System.Threading.Timer为成千上万的并发计时器足够的效率?计时器、效率、System、Threading

2023-09-03 02:47:31 作者:左手邊の記憶

我工作的请求超时机制。我最初的方法是创建一个System.Threading.Timer为每个请求。并发请求数可以扩展至数千个。

I'm working on a request timeout mechanism. My initial approach would be to create one System.Threading.Timer for each request. The number of concurrent requests could scale up to thousands.

我不知道我是否应该改为创建一个TimeoutScheduler,将在内部使用每请求一个只有一个定时器来代替。

I'm wondering if I should instead create a TimeoutScheduler that would internally use only one timer instead of having one per request.

任何人都可以,谁知道System.Threading.Timer的内部给我一些见解,如果TimeoutScheduler将是一个不错的主意,或者如果它肯尝试,以优化的东西已经足够高效。

Can anyone who knows the internals of System.Threading.Timer give me some insights on if a TimeoutScheduler would be a good idea or if it would only try to optimize something already efficient enough.

注:对于我的情况下,定时precision并不重要

Note: For my scenario, the timer precision is not important.

(我做了System.Threading.Timer一些性能测试有很多并发定时器,它似乎很好地扩展,但我不知道它会把多余的pressure在实际系统中)

(I did some performance test with System.Threading.Timer with alot of concurrent timers. It seemed to scale well, but I'm not sure if it will put unwanted pressure in a real system)

推荐答案

我会引导您到这个职位从雷蒙德陈:

I will direct you to this post from Raymond Chen:

什么是定时器的程序可以创建的最大数量?

在技术上存在与创造成千上万的人没有问题。要知道,这些使用全局系统资源,所以你的最终的触及上限和/或开始挨饿其他程序(包括Windows本身),如果你去疯狂吧。

Technically there is no problem with creating thousands of them. Just be aware that these use global system resources, so you will eventually hit an upper limit and/or start starving other programs (including Windows itself) if you go crazy with it.

此外确保你处置定时器,当你与他们就大功告成了,否则你会最终有一个巨大的资源泄漏。

Also make sure you dispose the timers when you're done with them, otherwise you'll end up with a huge resource leak.

我不得不说,这听起来像的东西,你可以用一个定时器实现;只是保持活动的请求和超时的字典,并在每个tick,经过整个字典,并检查每个条目。你可以通过它的排序列表提高性​​能;这样,如果没有什么是超时的5分钟,刻度方法看的第一个条目后退出。

I do have to say, this sounds like something you could implement with a single timer; just maintain a dictionary of active requests and timeouts, and on each tick, go through the whole dictionary and check each entry. You could improve performance by making it a sorted list; that way if nothing is timing out for another 5 minutes, the tick method will exit after looking at the first entry.

在除上述外,你也可以动态地调整刻度间隔每当下一个请求预定时间来火;然后在每个tick,重新安排的下一个剔下超时。这会占用几乎没有任何的系统资源(只有一个定时器),也非常快(与你的一个定时器每个请求的想法,你只会运行昂贵的code当请求实际到期)。

In addition to the above, you could also dynamically adjust the tick interval to fire whenever the next request is scheduled to time out; then on each tick, reschedule the next tick to the next timeout. This would take up virtually no system resources (just one timer) and also be very fast (as with your one-timer-per-request idea, you would only be running expensive code when requests actually expire).

即使你的可以的创造数千个定时器,上述办法将规模好得多,也将是一个更容易测试和维护。

Even though you can create thousands of timers, the above approach will scale far better and will also be a lot easier to test and maintain.