MT#40962 decouple RateLimit from AmAppTimer

Remove dependency on AmAppTimer's wall_clock ticks by switching to
direct call of gettimeofday() and converting the internally used time
units to milliseconds instead of the tick-based wall clock time.

Remove ::getLastUpdate() as it's unused.

Change-Id: Ia3a026103b594351c0a6943de67dd94883e30578
mr12.3.1
Richard Fuchs 2 years ago
parent eeae1d1343
commit e33e002d98

@ -1,13 +1,12 @@
#include "RateLimit.h"
#include "AmAppTimer.h"
#include <sys/time.h>
#define min(a,b) ((a) < (b) ? (a) : (b))
DynRateLimit::DynRateLimit(unsigned int time_base_ms)
: last_update(0), counter(0)
DynRateLimit::DynRateLimit(unsigned int _time_base_ms)
: last_update(0), counter(0), time_base_ms(_time_base_ms)
{
// wall_clock has a resolution of 20ms
time_base = time_base_ms / 20;
}
bool DynRateLimit::limit(unsigned int rate, unsigned int peak,
@ -15,8 +14,8 @@ bool DynRateLimit::limit(unsigned int rate, unsigned int peak,
{
lock();
if(AmAppTimer::instance()->wall_clock - last_update
> time_base) {
if(wall_clock_ms() - last_update
> time_base_ms) {
update_limit(rate,peak);
}
@ -35,5 +34,12 @@ bool DynRateLimit::limit(unsigned int rate, unsigned int peak,
void DynRateLimit::update_limit(int rate, int peak)
{
counter = min(peak, counter+rate);
last_update = AmAppTimer::instance()->wall_clock;
last_update = wall_clock_ms();
}
u_int32_t DynRateLimit::wall_clock_ms()
{
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_sec * 1000 + now.tv_usec / 1000;
}

@ -11,7 +11,7 @@ class DynRateLimit
u_int32_t last_update;
int counter;
unsigned int time_base;
unsigned int time_base_ms;
void update_limit(int rate, int peak);
@ -21,7 +21,7 @@ public:
virtual ~DynRateLimit() {}
unsigned int getTimeBase() const { return time_base; }
unsigned int getTimeBase() const { return time_base_ms * 20; /* 20 ms increments */ }
/**
* rate: units/time_base
@ -30,8 +30,8 @@ public:
*/
bool limit(unsigned int rate, unsigned int peak, unsigned int size);
/** Get last update timestamp (wheeltimer::wallclock ticks) */
u_int32_t getLastUpdate() { return last_update; }
private:
static u_int32_t wall_clock_ms();
};
class RateLimit

Loading…
Cancel
Save