util: add function for simple rate limiting.
Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>master
parent
776f772c36
commit
01d523fb48
14
util.c
14
util.c
|
@ -512,3 +512,17 @@ void parray_extend(void ***a, ...)
|
|||
va_end(ap);
|
||||
(*a)[len - 1] = NULL;
|
||||
}
|
||||
|
||||
int rate_limited(int interval, time_t *last)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts))
|
||||
return 1;
|
||||
if (*last + interval > ts.tv_sec)
|
||||
return 1;
|
||||
|
||||
*last = ts.tv_sec;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
9
util.h
9
util.h
|
@ -354,4 +354,13 @@ void parray_append(void ***a, void *p);
|
|||
*/
|
||||
void parray_extend(void ***a, ...);
|
||||
|
||||
/**
|
||||
* Check if enough time has passed to implement a simple rate limiting.
|
||||
*
|
||||
* @param interval Minimum interval between two calls returning 0 (in seconds).
|
||||
* @param last Time of the last call that returned 0, input/output.
|
||||
* @return 1 when rate limited, 0 otherwise.
|
||||
*/
|
||||
int rate_limited(int interval, time_t *last);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue