util: add function for simple rate limiting.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
master
Miroslav Lichvar 2015-09-10 11:49:26 +02:00 committed by Richard Cochran
parent 776f772c36
commit 01d523fb48
2 changed files with 23 additions and 0 deletions

14
util.c
View File

@ -512,3 +512,17 @@ void parray_extend(void ***a, ...)
va_end(ap); va_end(ap);
(*a)[len - 1] = NULL; (*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
View File

@ -354,4 +354,13 @@ void parray_append(void ***a, void *p);
*/ */
void parray_extend(void ***a, ...); 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 #endif