Some checks failed
Build and deploy Docs site to GitHub Pages / github-pages (push) Has been cancelled
Run Unit Tests / test (push) Has been cancelled
PR Build Check / build (Heltec_v3_companion_radio_ble) (push) Has been cancelled
PR Build Check / build (Heltec_v3_repeater) (push) Has been cancelled
PR Build Check / build (Heltec_v3_room_server) (push) Has been cancelled
PR Build Check / build (LilyGo_Tlora_C6_repeater_) (push) Has been cancelled
PR Build Check / build (PicoW_repeater) (push) Has been cancelled
PR Build Check / build (RAK_4631_companion_radio_ble) (push) Has been cancelled
PR Build Check / build (RAK_4631_repeater) (push) Has been cancelled
PR Build Check / build (RAK_4631_room_server) (push) Has been cancelled
PR Build Check / build (Tbeam_SX1276_repeater) (push) Has been cancelled
PR Build Check / build (wio-e5-mini_repeater) (push) Has been cancelled
PR Build Check / build (wio_wm1110_repeater) (push) Has been cancelled
23 lines
515 B
C++
23 lines
515 B
C++
#pragma once
|
|
|
|
#include <stdint.h>
|
|
|
|
class RateLimiter {
|
|
uint32_t _start_timestamp;
|
|
uint32_t _secs;
|
|
uint16_t _maximum, _count;
|
|
|
|
public:
|
|
RateLimiter(uint16_t maximum, uint32_t secs): _maximum(maximum), _secs(secs), _start_timestamp(0), _count(0) { }
|
|
|
|
bool allow(uint32_t now) {
|
|
if (now < _start_timestamp + _secs) {
|
|
_count++;
|
|
if (_count > _maximum) return false; // deny
|
|
} else { // time window now expired
|
|
_start_timestamp = now;
|
|
_count = 1;
|
|
}
|
|
return true;
|
|
}
|
|
}; |