Message ID | edc08023ed51890f0390aacf783d7213e82704a7.1691699987.git.gitgitgadget@gmail.com (mailing list archive) |
---|---|
State | Accepted |
Commit | 89024a0ab018bb6e8ad2e4a6500b98b889088c54 |
Headers | show |
Series | maintenance: schedule maintenance on a random minute | expand |
On Thu, Aug 10, 2023 at 08:39:40PM +0000, Derrick Stolee via GitGitGadget wrote: > --- > builtin/gc.c | 10 ++++++++++ > wrapper.c | 10 ++++++++++ > wrapper.h | 6 ++++++ > 3 files changed, 26 insertions(+) Very nice, this patch LGTM. Thanks for working on it! Thanks, Taylor
diff --git a/builtin/gc.c b/builtin/gc.c index 19d73067aad..2ebae7bc17c 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -1708,6 +1708,16 @@ static int get_schedule_cmd(const char **cmd, int *is_available) return 1; } +MAYBE_UNUSED +static int get_random_minute(void) +{ + /* Use a static value when under tests. */ + if (getenv("GIT_TEST_MAINT_SCHEDULER")) + return 13; + + return git_rand() % 60; +} + static int is_launchctl_available(void) { const char *cmd = "launchctl"; diff --git a/wrapper.c b/wrapper.c index 5160c9e28de..48065c4f533 100644 --- a/wrapper.c +++ b/wrapper.c @@ -819,3 +819,13 @@ int csprng_bytes(void *buf, size_t len) return 0; #endif } + +uint32_t git_rand(void) +{ + uint32_t result; + + if (csprng_bytes(&result, sizeof(result)) < 0) + die(_("unable to get random bytes")); + + return result; +} diff --git a/wrapper.h b/wrapper.h index 79a9c1b5077..79c7321bb37 100644 --- a/wrapper.h +++ b/wrapper.h @@ -139,4 +139,10 @@ void sleep_millisec(int millisec); */ int csprng_bytes(void *buf, size_t len); +/* + * Returns a random uint32_t, uniformly distributed across all possible + * values. + */ +uint32_t git_rand(void); + #endif /* WRAPPER_H */