Skip to content

Commit 08eb898

Browse files
committed
Modify the timing wheel
1 parent 483720b commit 08eb898

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

trantor/utils/TimingWheel.cc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,29 @@ using namespace trantor;
1818

1919
TimingWheel::TimingWheel(trantor::EventLoop *loop,
2020
size_t maxTimeout,
21-
float tickInterval,
21+
float ticksInterval,
2222
size_t bucketsNumPerWheel)
2323
: loop_(loop),
24-
tickInterval_(tickInterval),
24+
ticksInterval_(ticksInterval),
2525
bucketsNumPerWheel_(bucketsNumPerWheel)
2626
{
2727
assert(maxTimeout > 1);
28-
assert(tickInterval > 0);
29-
size_t maxTickNum = maxTimeout / tickInterval;
28+
assert(ticksInterval > 0);
29+
assert(bucketsNumPerWheel_ > 1);
30+
size_t maxTickNum = maxTimeout / ticksInterval;
31+
auto ticksNum = bucketsNumPerWheel;
3032
wheelsNum_ = 1;
31-
while (maxTickNum > bucketsNumPerWheel_)
33+
while (maxTickNum > ticksNum)
3234
{
3335
++wheelsNum_;
34-
maxTickNum = maxTickNum / bucketsNumPerWheel_;
36+
ticksNum *= bucketsNumPerWheel_;
3537
}
3638
wheels_.resize(wheelsNum_);
3739
for (size_t i = 0; i < wheelsNum_; ++i)
3840
{
3941
wheels_[i].resize(bucketsNumPerWheel_);
4042
}
41-
timerId_ = loop_->runEvery(tickInterval_, [=]() {
43+
timerId_ = loop_->runEvery(ticksInterval_, [=]() {
4244
++ticksCounter_;
4345
size_t t = ticksCounter_;
4446
size_t pow = 1;
@@ -90,10 +92,9 @@ void TimingWheel::insertEntry(size_t delay, EntryPtr entryPtr)
9092

9193
void TimingWheel::insertEntryInloop(size_t delay, EntryPtr entryPtr)
9294
{
93-
// protected by bucketMutex;
9495
loop_->assertInLoopThread();
9596

96-
delay = delay / tickInterval_ + 1;
97+
delay = delay / ticksInterval_ + 1;
9798
size_t t = ticksCounter_;
9899
for (size_t i = 0; i < wheelsNum_; ++i)
99100
{

trantor/utils/TimingWheel.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
#define TIMING_BUCKET_NUM_PER_WHEEL 100
3030
#define TIMING_TICK_INTERVAL 1.0
3131

32-
// Four wheels with 200 buckets per wheel means the cache map can work with
33-
// a timeout up to 200^4 seconds,about 50 years;
32+
// Four wheels with 200 buckets per wheel means the timing wheel can work with
33+
// a timeout up to 200^4 seconds, about 50 years;
3434

3535
namespace trantor
3636
{
@@ -60,17 +60,17 @@ class TimingWheel
6060
/// constructor
6161
/// @param loop
6262
/// eventloop pointer
63-
/// @param tickInterval
63+
/// @param ticksInterval
6464
/// second
6565
/// @param wheelsNum
6666
/// number of wheels
6767
/// @param bucketsNumPerWheel
6868
/// buckets number per wheel
6969
/// The max delay of the CacheMap is about
70-
/// tickInterval*(bucketsNumPerWheel^wheelsNum) seconds.
70+
/// ticksInterval*(bucketsNumPerWheel^wheelsNum) seconds.
7171
TimingWheel(trantor::EventLoop *loop,
7272
size_t maxTimeout,
73-
float tickInterval = TIMING_TICK_INTERVAL,
73+
float ticksInterval = TIMING_TICK_INTERVAL,
7474
size_t bucketsNumPerWheel = TIMING_BUCKET_NUM_PER_WHEEL);
7575

7676
// If timeout>0, the value will be erased within the 'timeout' seconds after
@@ -94,7 +94,7 @@ class TimingWheel
9494
trantor::TimerId timerId_;
9595
trantor::EventLoop *loop_;
9696

97-
float tickInterval_;
97+
float ticksInterval_;
9898
size_t wheelsNum_;
9999
size_t bucketsNumPerWheel_;
100100
};

0 commit comments

Comments
 (0)