forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackofftimer.cpp
More file actions
226 lines (195 loc) · 5.13 KB
/
backofftimer.cpp
File metadata and controls
226 lines (195 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* @file backofftimer.cpp
* @brief Generic timer facility with exponential backoff
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "mega/waiter.h"
#include "mega/backofftimer.h"
#include "mega/logging.h"
namespace mega {
// timer with capped exponential backoff
BackoffTimer::BackoffTimer(PrnGen &rng)
: rng(rng)
{
reset();
}
void BackoffTimer::reset()
{
next = 0;
delta = 1;
base = 1;
}
void BackoffTimer::backoff()
{
next = Waiter::ds + delta;
base <<= 1;
if (base > 6000)
{
base = 6000;
}
delta = base + (dstime)((base / 2.0) * (rng.genuint32(RAND_MAX)/(float)RAND_MAX));
}
void BackoffTimer::backoff(dstime newdelta)
{
next = (newdelta == NEVER) ? NEVER : (Waiter::ds + newdelta);
delta = newdelta;
base = newdelta;
}
bool BackoffTimer::armed() const
{
return next <= 1 || Waiter::ds >= next;
}
bool BackoffTimer::arm()
{
if (next == NEVER || (next + delta) > Waiter::ds)
{
next = Waiter::ds;
delta = 1;
base = 1;
return true;
}
return false;
}
void BackoffTimer::set(dstime newds)
{
if (newds < next)
{
next = newds;
}
}
dstime BackoffTimer::retryin() const
{
if (armed())
{
return 0;
}
return next - Waiter::ds;
}
dstime BackoffTimer::backoffdelta()
{
return delta;
}
dstime BackoffTimer::nextset() const
{
return next;
}
// event in the future: potentially updates waituntil
// event in the past: zeros out waituntil and clears event
void BackoffTimer::update(dstime* waituntil)
{
if (next)
{
assert(next != 1);
if (next == 1)
{
LOG_warn << "Possible wrong management of timer";
}
if (next <= Waiter::ds)
{
*waituntil = (next == 1) ? Waiter::ds + 1 : 0;
next = 1;
}
else if (next < *waituntil)
{
*waituntil = next;
}
}
}
TimerWithBackoff::TimerWithBackoff(PrnGen &rng, int tag)
: BackoffTimer(rng)
{
this->tag = tag;
}
void BackoffTimerGroupTracker::update(dstime* waituntil, bool transfers)
{
// This function performs a similar action as calling BackoffTimer::update for all the timers in the group,
// which is to say, the `waituntil` parameter will be updated with the soonest time that we would need to
// wake up from any of the timers in this group, should any of them be in a back-off state.
// There are also some side-effects specfic to transfers which are preserved from the old system.
vector<BackoffTimerTracked*> v;
v.reserve(timeouts.size());
if (transfers)
{
// used to be:
//for (transfer_map::iterator it = transfers[d].begin(); it != transfers[d].end(); it++)
//{
// if ((!it->second->slot || !it->second->slot->fa)
// && it->second->bt.nextset())
// {
// it->second->bt.update(dsmin);
// if (it->second->bt.armed())
// {
// // fire the timer only once but keeping it armed
// it->second->bt.set(0);
// LOG_debug << "Disabling armed transfer backoff";
// }
// }
//}
for (auto t : timeouts)
{
// put the ones to work on in a vector, as working on them changes their position in the map
if (t.first <= Waiter::ds)
{
v.push_back(t.second);
}
else
{
break;
}
}
for (auto t : v)
{
t->update(waituntil);
if (t->armed())
{
// fire the timer only once but keeping it armed
t->set(0);
LOG_debug << "Disabling armed transfer backoff";
}
}
}
else
{
// used to be:
//for (transferslot_list::iterator it = tslots.begin(); it != tslots.end(); it++)
//{
// if (!(*it)->retrybt.armed())
// {
// (*it)->retrybt.update(&nds);
// }
//}
// put the ones to work on in a vector, as working on them changes their position in the map
for (auto t : timeouts)
{
if (t.second->armed())
{
v.push_back(t.second);
}
if (t.first > Waiter::ds)
{
break;
}
}
for (auto t : v)
{
// update may set next=1 so we can't just call the first one.
t->update(waituntil);
}
}
}
} // namespace