Commit d0f1e2f1b9f0e472831dc88cc9885c61d52c87f1

Stefan Sperling 2022-02-23T12:13:03

apply time-based rate-limiting to got-index-pack progress output

diff --git a/libexec/got-index-pack/Makefile b/libexec/got-index-pack/Makefile
index 483b835..08a453b 100644
--- a/libexec/got-index-pack/Makefile
+++ b/libexec/got-index-pack/Makefile
@@ -4,7 +4,7 @@
 
 PROG=		got-index-pack
 SRCS=		got-index-pack.c error.c inflate.c object_parse.c object_idset.c \
-		delta_cache.c delta.c pack.c path.c privsep.c sha1.c
+		delta_cache.c delta.c pack.c path.c privsep.c sha1.c ratelimit.c
 
 CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
 LDADD = -lutil -lz
diff --git a/libexec/got-index-pack/got-index-pack.c b/libexec/got-index-pack/got-index-pack.c
index bd07835..8c98a68 100644
--- a/libexec/got-index-pack/got-index-pack.c
+++ b/libexec/got-index-pack/got-index-pack.c
@@ -52,6 +52,7 @@
 #include "got_lib_privsep.h"
 #include "got_lib_pack.h"
 #include "got_lib_delta_cache.h"
+#include "got_lib_ratelimit.h"
 
 #ifndef nitems
 #define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
@@ -594,9 +595,18 @@ update_packidx(struct got_packidx *packidx, int nobj,
 
 static const struct got_error *
 send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
-    int nobj_indexed, int nobj_loose, int nobj_resolved)
+    int nobj_indexed, int nobj_loose, int nobj_resolved,
+    struct got_ratelimit *rl)
 {
+	const struct got_error *err;
 	struct got_imsg_index_pack_progress iprogress;
+	int elapsed = 0;
+
+	if (rl) {
+		err = got_ratelimit_check(&elapsed, rl);
+		if (err || !elapsed)
+			return err;
+	}
 
 	iprogress.nobj_total = nobj_total;
 	iprogress.nobj_indexed = nobj_indexed;
@@ -638,6 +648,7 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
 	size_t mapoff = 0;
 	int p_indexed = 0, last_p_indexed = -1;
 	int p_resolved = 0, last_p_resolved = -1;
+	struct got_ratelimit rl;
 
 	/* Require that pack file header and SHA1 trailer are present. */
 	if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
@@ -725,6 +736,8 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
 	if (objects == NULL)
 		return got_error_from_errno("calloc");
 
+	got_ratelimit_init(&rl, 0, 500);
+
 	/*
 	 * First pass: locate all objects and identify un-deltified objects.
 	 *
@@ -739,7 +752,7 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
 		p_indexed = ((i + 1) * 100) / nobj;
 		if (p_indexed != last_p_indexed) {
 			err = send_index_pack_progress(ibuf, nobj, i + 1,
-			    nloose, 0);
+			    nloose, 0, &rl);
 			if (err)
 				goto done;
 			last_p_indexed = p_indexed;
@@ -884,7 +897,7 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
 			p_resolved = ((nresolved + n) * 100) / nobj;
 			if (p_resolved != last_p_resolved) {
 				err = send_index_pack_progress(ibuf, nobj,
-				    nobj, nloose, nresolved + n);
+				    nobj, nloose, nresolved + n, &rl);
 				if (err)
 					goto done;
 				last_p_resolved = p_resolved;
@@ -911,7 +924,8 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
 		goto done;
 	}
 
-	err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved);
+	err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved,
+	    NULL);
 	if (err)
 		goto done;