void TokenBucketThrottle::add_tokens() {
list<Blocker> tmp_blockers;
{
- std::lock_guard<Mutex> lock(m_lock);
+ std::lock_guard lock(m_lock);
// put tokens into bucket.
m_throttle.put(tokens_this_tick());
+ if (0 == m_avg || 0 == m_throttle.max)
+ tmp_blockers.swap(m_blockers);
// check the m_blockers from head to tail, if blocker can get
// enough tokens, let it go.
while (!m_blockers.empty()) {
template <typename T, typename I, void(T::*MF)(int, I*, uint64_t)>
bool get(uint64_t c, T *handler, I *item, uint64_t flag) {
- if (0 == m_avg)
+ if (0 == c)
return false;
-
+
bool wait = false;
uint64_t got = 0;
- std::lock_guard<Mutex> lock(m_lock);
+ std::lock_guard lock(m_lock);
if (!m_blockers.empty()) {
// Keep the order of requests, add item after previous blocked requests.
wait = true;