apply time-based rate-limiting to got-fetch-pack download progress output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
diff --git a/libexec/got-fetch-pack/Makefile b/libexec/got-fetch-pack/Makefile
index 7df6b00..fb60037 100644
--- a/libexec/got-fetch-pack/Makefile
+++ b/libexec/got-fetch-pack/Makefile
@@ -4,7 +4,7 @@
PROG= got-fetch-pack
SRCS= got-fetch-pack.c error.c inflate.c object_parse.c \
- path.c privsep.c sha1.c pkt.c gitproto.c
+ path.c privsep.c sha1.c pkt.c gitproto.c ratelimit.c
CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
LDADD = -lutil -lz
diff --git a/libexec/got-fetch-pack/got-fetch-pack.c b/libexec/got-fetch-pack/got-fetch-pack.c
index b85f024..68f212c 100644
--- a/libexec/got-fetch-pack/got-fetch-pack.c
+++ b/libexec/got-fetch-pack/got-fetch-pack.c
@@ -50,6 +50,7 @@
#include "got_lib_pack.h"
#include "got_lib_pkt.h"
#include "got_lib_gitproto.h"
+#include "got_lib_ratelimit.h"
#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
@@ -140,8 +141,18 @@ send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
}
static const struct got_error *
-send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
+send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
+ struct got_ratelimit *rl)
{
+ const struct got_error *err;
+ int elapsed = 0;
+
+ if (rl) {
+ err = got_ratelimit_check(&elapsed, rl);
+ if (err || !elapsed)
+ return err;
+ }
+
if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
&bytes, sizeof(bytes)) == -1)
return got_error_from_errno(
@@ -324,9 +335,11 @@ fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
size_t sha1_buf_len = 0;
ssize_t w;
+ struct got_ratelimit rl;
TAILQ_INIT(&symrefs);
SHA1Init(&sha1_ctx);
+ got_ratelimit_init(&rl, 0, 500);
have = malloc(refsz * sizeof(have[0]));
if (have == NULL)
@@ -744,13 +757,13 @@ fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
/* Don't send too many progress privsep messages. */
if (packsz > last_reported_packsz + 1024) {
- err = send_fetch_download_progress(ibuf, packsz);
+ err = send_fetch_download_progress(ibuf, packsz, &rl);
if (err)
goto done;
last_reported_packsz = packsz;
}
}
- err = send_fetch_download_progress(ibuf, packsz);
+ err = send_fetch_download_progress(ibuf, packsz, NULL);
if (err)
goto done;