Blame


1 31ba2236 2022-01-05 thomas /*
2 31ba2236 2022-01-05 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 31ba2236 2022-01-05 thomas *
4 31ba2236 2022-01-05 thomas * Permission to use, copy, modify, and distribute this software for any
5 31ba2236 2022-01-05 thomas * purpose with or without fee is hereby granted, provided that the above
6 31ba2236 2022-01-05 thomas * copyright notice and this permission notice appear in all copies.
7 31ba2236 2022-01-05 thomas *
8 31ba2236 2022-01-05 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 31ba2236 2022-01-05 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 31ba2236 2022-01-05 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 31ba2236 2022-01-05 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 31ba2236 2022-01-05 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 31ba2236 2022-01-05 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 31ba2236 2022-01-05 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 31ba2236 2022-01-05 thomas */
16 31ba2236 2022-01-05 thomas
17 31ba2236 2022-01-05 thomas #include <sys/time.h>
18 31ba2236 2022-01-05 thomas
19 31ba2236 2022-01-05 thomas #include <stdio.h>
20 31ba2236 2022-01-05 thomas #include <limits.h>
21 31ba2236 2022-01-05 thomas #include <string.h>
22 31ba2236 2022-01-05 thomas #include <time.h>
23 31ba2236 2022-01-05 thomas
24 31ba2236 2022-01-05 thomas #include "got_lib_ratelimit.h"
25 31ba2236 2022-01-05 thomas
26 de1dbfe9 2022-01-05 thomas #include "got_compat.h"
27 31ba2236 2022-01-05 thomas #include "got_error.h"
28 31ba2236 2022-01-05 thomas
29 31ba2236 2022-01-05 thomas void
30 31ba2236 2022-01-05 thomas got_ratelimit_init(struct got_ratelimit *rl, time_t interval_sec,
31 31ba2236 2022-01-05 thomas unsigned int interval_msec)
32 31ba2236 2022-01-05 thomas {
33 31ba2236 2022-01-05 thomas memset(rl, 0, sizeof(*rl));
34 31ba2236 2022-01-05 thomas rl->interval.tv_sec = interval_sec;
35 31ba2236 2022-01-05 thomas rl->interval.tv_nsec = interval_msec * 1000000UL;
36 31ba2236 2022-01-05 thomas }
37 31ba2236 2022-01-05 thomas
38 31ba2236 2022-01-05 thomas const struct got_error *
39 31ba2236 2022-01-05 thomas got_ratelimit_check(int *elapsed, struct got_ratelimit *rl)
40 31ba2236 2022-01-05 thomas {
41 31ba2236 2022-01-05 thomas struct timespec now, delta;
42 31ba2236 2022-01-05 thomas
43 31ba2236 2022-01-05 thomas if (clock_gettime(CLOCK_MONOTONIC, &now) == -1)
44 31ba2236 2022-01-05 thomas return got_error_from_errno("clock_gettime");
45 31ba2236 2022-01-05 thomas
46 31ba2236 2022-01-05 thomas if (timespecisset(&rl->last)) {
47 31ba2236 2022-01-05 thomas timespecsub(&now, &rl->last, &delta);
48 31ba2236 2022-01-05 thomas *elapsed = timespeccmp(&delta, &rl->interval, >=) ? 1 : 0;
49 31ba2236 2022-01-05 thomas } else
50 31ba2236 2022-01-05 thomas *elapsed = 1;
51 31ba2236 2022-01-05 thomas
52 31ba2236 2022-01-05 thomas if (*elapsed)
53 31ba2236 2022-01-05 thomas rl->last = now;
54 31ba2236 2022-01-05 thomas
55 31ba2236 2022-01-05 thomas return NULL;
56 31ba2236 2022-01-05 thomas }