apply time-based rate-limiting to got-index-pack 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
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;