Blame


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