more asserts to bandwidth limiter

This commit is contained in:
Marcos Pinto 2007-09-19 02:45:29 +00:00
parent 25146891ff
commit 760acc8f30
1 changed files with 26 additions and 6 deletions

View File

@ -180,7 +180,12 @@ struct bandwidth_manager
, m_limit(bandwidth_limit::inf) , m_limit(bandwidth_limit::inf)
, m_current_quota(0) , m_current_quota(0)
, m_channel(channel) , m_channel(channel)
{} {
#ifndef NDEBUG
m_in_hand_out_bandwidth = false;
#endif
}
void throttle(int limit) throw() void throttle(int limit) throw()
{ {
@ -329,6 +334,9 @@ private:
void hand_out_bandwidth() throw() void hand_out_bandwidth() throw()
{ {
#ifndef NDEBUG #ifndef NDEBUG
assert(m_in_hand_out_bandwidth == false);
m_in_hand_out_bandwidth = true;
try { try {
#endif #endif
INVARIANT_CHECK; INVARIANT_CHECK;
@ -361,6 +369,7 @@ private:
if (qe.peer->is_disconnecting()) if (qe.peer->is_disconnecting())
{ {
t->expire_bandwidth(m_channel, qe.max_block_size); t->expire_bandwidth(m_channel, qe.max_block_size);
assert(amount == limit - m_current_quota);
continue; continue;
} }
@ -374,6 +383,7 @@ private:
if (max_assignable == 0) if (max_assignable == 0)
{ {
t->expire_bandwidth(m_channel, qe.max_block_size); t->expire_bandwidth(m_channel, qe.max_block_size);
assert(amount == limit - m_current_quota);
continue; continue;
} }
@ -388,15 +398,15 @@ private:
// the history window is one second, and the block will be forgotten // the history window is one second, and the block will be forgotten
// after one second. // after one second.
int block_size = (std::min)(qe.peer->bandwidth_throttle(m_channel) int block_size = (std::min)(qe.peer->bandwidth_throttle(m_channel)
, m_limit / 10); , limit / 10);
if (block_size < min_bandwidth_block_size) if (block_size < min_bandwidth_block_size)
{ {
block_size = (std::min)(int(min_bandwidth_block_size), m_limit); block_size = (std::min)(int(min_bandwidth_block_size), limit);
} }
else if (block_size > max_bandwidth_block_size) else if (block_size > max_bandwidth_block_size)
{ {
if (m_limit == bandwidth_limit::inf) if (limit == bandwidth_limit::inf)
{ {
block_size = max_bandwidth_block_size; block_size = max_bandwidth_block_size;
} }
@ -407,8 +417,8 @@ private:
// as possible // as possible
// TODO: move this calculcation to where the limit // TODO: move this calculcation to where the limit
// is changed // is changed
block_size = m_limit block_size = limit
/ (m_limit / max_bandwidth_block_size); / (limit / max_bandwidth_block_size);
} }
} }
if (block_size > qe.max_block_size) block_size = qe.max_block_size; if (block_size > qe.max_block_size) block_size = qe.max_block_size;
@ -428,17 +438,21 @@ private:
int hand_out_amount = (std::min)((std::min)(block_size, max_assignable) int hand_out_amount = (std::min)((std::min)(block_size, max_assignable)
, amount); , amount);
assert(hand_out_amount > 0); assert(hand_out_amount > 0);
assert(amount == limit - m_current_quota);
amount -= hand_out_amount; amount -= hand_out_amount;
assert(hand_out_amount <= qe.max_block_size); assert(hand_out_amount <= qe.max_block_size);
t->assign_bandwidth(m_channel, hand_out_amount, qe.max_block_size); t->assign_bandwidth(m_channel, hand_out_amount, qe.max_block_size);
qe.peer->assign_bandwidth(m_channel, hand_out_amount); qe.peer->assign_bandwidth(m_channel, hand_out_amount);
add_history_entry(history_entry<PeerConnection, Torrent>( add_history_entry(history_entry<PeerConnection, Torrent>(
qe.peer, t, hand_out_amount, now + bw_window_size)); qe.peer, t, hand_out_amount, now + bw_window_size));
assert(amount == limit - m_current_quota);
} }
#ifndef NDEBUG #ifndef NDEBUG
} }
catch (std::exception& e) catch (std::exception& e)
{ assert(false); }; { assert(false); };
m_in_hand_out_bandwidth = false;
#endif #endif
} }
@ -472,9 +486,15 @@ private:
// this is the channel within the consumers // this is the channel within the consumers
// that bandwidth is assigned to (upload or download) // that bandwidth is assigned to (upload or download)
int m_channel; int m_channel;
#ifndef NDEBUG
bool m_in_hand_out_bandwidth;
#endif
}; };
} }
#endif #endif