Skip to content

Commit

Permalink
mx_util: Add mx_within_rate_limit_or_return(sec, ret)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariux committed Oct 22, 2015
1 parent 3fb0752 commit 259145a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions mx_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <string.h>
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
#include <sys/time.h>

#include "mx_log.h"

Expand Down Expand Up @@ -118,6 +120,18 @@ static inline void __mx_fclose(FILE **ptr) {
#undef mx_streq_nocase
#define mx_streq_nocase(a, b) (strcasecmp((a), (b)) == 0)

#define mx_within_rate_limit_or_return(sec, ret) \
do {\
static struct timeval _sleep = {0};\
struct timeval _now;\
struct timeval _delta;\
gettimeofday(&_now, NULL);\
timersub(&_now, &_sleep, &_delta);\
if (_delta.tv_sec < (sec))\
return (ret);\
_sleep = _now;\
} while(0)

int mx_strbeginswith(char *str, const char *start, char **endptr);
int mx_stribeginswith(char *str, const char *start, char **endptr);
int mx_strbeginswithany(char *str, char **starts, char **endptr);
Expand Down

0 comments on commit 259145a

Please sign in to comment.