now = get_current_btime(); /* microseconds */
temp = now - m_last_tick; /* microseconds */
- m_nb_bytes += bytes;
- if (temp < 0 || temp > (ONE_SEC*10)) { /* Take care of clock problems (>10s) or back in time */
+ if (temp < 0 || temp > m_backlog_limit) { /* Take care of clock problems (>10s) or back in time */
m_nb_bytes = bytes;
m_last_tick = now;
return;
}
+ /* remove what as been consumed */
+ m_nb_bytes -= bytes;;
+
/* Less than 0.1ms since the last call, see the next time */
if (temp < 100) {
return;
}
- /* Remove what was authorised to be written in temp us */
- m_nb_bytes -= (int64_t)(temp * ((double)m_bwlimit / ONE_SEC));
-
- if (m_nb_bytes < 0) {
- m_nb_bytes = 0;
- }
+ /* Add what is authorized to be written in temp us */
+ m_nb_bytes += (int64_t)(temp * ((double)m_bwlimit / ONE_SEC));
+ m_last_tick = now;
- /* What exceed should be converted in sleep time */
- int64_t usec_sleep = (int64_t)(m_nb_bytes /((double)m_bwlimit / ONE_SEC));
- if (usec_sleep > 100) {
- bmicrosleep(usec_sleep / ONE_SEC, usec_sleep % ONE_SEC);
- m_last_tick = get_current_btime();
- m_nb_bytes = 0;
- } else {
- m_last_tick = now;
+ /* limit the backlog */
+ if (m_nb_bytes > m_backlog_limit) {
+ m_nb_bytes = m_backlog_limit;
+ } else if (m_nb_bytes < 0) {
+ /* What exceed should be converted in sleep time */
+ int64_t usec_sleep = (int64_t)(-m_nb_bytes /((double)m_bwlimit / ONE_SEC));
+ if (usec_sleep > 100) {
+ bmicrosleep(usec_sleep / ONE_SEC, usec_sleep % ONE_SEC);
+ }
+ /* m_nb_bytes & m_last_tick will be updated at next iteration */
}
}
int64_t m_bwlimit; /* set to limit bandwidth */
int64_t m_nb_bytes; /* bytes sent/recv since the last tick */
btime_t m_last_tick; /* last tick used by bwlimit */
+ btime_t m_backlog_limit; /* don't handle more backlog thna this us */
+ int64_t m_provision;
pthread_mutex_t m_bw_mutex;
public:
- bwlimit(int64_t speed=0): m_bwlimit(speed), m_nb_bytes(0), m_last_tick(0) {
+ bwlimit(int64_t speed=0): m_bwlimit(speed), m_nb_bytes(0), m_last_tick(0),
+ m_backlog_limit(10*1000*1000) {
pthread_mutex_init(&m_bw_mutex, NULL);
};
~bwlimit() {