apply time-based rate-limiting to got-send-pack upload 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 74
diff --git a/libexec/got-send-pack/Makefile b/libexec/got-send-pack/Makefile
index c4d5d83..8e79a14 100644
--- a/libexec/got-send-pack/Makefile
+++ b/libexec/got-send-pack/Makefile
@@ -4,7 +4,7 @@
PROG= got-send-pack
SRCS= got-send-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-send-pack/got-send-pack.c b/libexec/got-send-pack/got-send-pack.c
index bec42fb..11c0b2d 100644
--- a/libexec/got-send-pack/got-send-pack.c
+++ b/libexec/got-send-pack/got-send-pack.c
@@ -51,6 +51,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]))
@@ -70,8 +71,18 @@ static const struct got_capability got_capabilities[] = {
};
static const struct got_error *
-send_upload_progress(struct imsgbuf *ibuf, off_t bytes)
+send_upload_progress(struct imsgbuf *ibuf, off_t bytes,
+ struct got_ratelimit *rl)
{
+ const struct got_error *err = NULL;
+ int elapsed = 0;
+
+ if (rl) {
+ err = got_ratelimit_check(&elapsed, rl);
+ if (err || !elapsed)
+ return err;
+ }
+
if (imsg_compose(ibuf, GOT_IMSG_SEND_UPLOAD_PROGRESS, 0, 0, -1,
&bytes, sizeof(bytes)) == -1)
return got_error_from_errno(
@@ -137,10 +148,13 @@ send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
unsigned char buf[8192];
ssize_t r, w;
off_t wtotal = 0;
+ struct got_ratelimit rl;
if (lseek(packfd, 0L, SEEK_SET) == -1)
return got_error_from_errno("lseek");
+ got_ratelimit_init(&rl, 0, 500);
+
for (;;) {
r = read(packfd, buf, sizeof(buf));
if (r == -1)
@@ -153,12 +167,12 @@ send_pack_file(int sendfd, int packfd, struct imsgbuf *ibuf)
if (w != r)
return got_error(GOT_ERR_IO);
wtotal += w;
- err = send_upload_progress(ibuf, wtotal);
+ err = send_upload_progress(ibuf, wtotal, &rl);
if (err)
return err;
}
- return NULL;
+ return send_upload_progress(ibuf, wtotal, NULL);
}
static const struct got_error *