From a6a3fe8ec290ec11f6a30dcae3de07fe3aea76fa Mon Sep 17 00:00:00 2001 From: Eauldane Date: Mon, 1 Sep 2025 02:38:29 +0100 Subject: [PATCH] Added phased startup string function for bringing services back online gradually --- global/global.go | 13 +++++++++++++ global/global_test.go | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/global/global.go b/global/global.go index 8fd619d..5339968 100644 --- a/global/global.go +++ b/global/global.go @@ -3,6 +3,7 @@ package global import ( crand "crypto/rand" // Probably better than math/rand for this "log" + "math/rand" ) func GenerateRandomString(length int) string { @@ -23,3 +24,15 @@ func GenerateRandomString(length int) string { } return string(charArray) } + +func PhasedStartupString() string { + // Redis is used for a lot of stuff, which means the Postgres DB gets HAMMERED on a fresh startup. Use + // this to generate a shuffled string that can be used to only allow e.g. UIDs starting with [x] to connect + // for the first however many seconds and incrementally expand access so that data doesn't need to be loaded all at + // once + chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + rand.Shuffle(len(chars), func(i, j int) { + chars[i], chars[j] = chars[j], chars[i] + }) + return string(chars) +} diff --git a/global/global_test.go b/global/global_test.go index 8afc12c..1cd2c7b 100644 --- a/global/global_test.go +++ b/global/global_test.go @@ -15,3 +15,7 @@ func TestGenerateRandomString(t *testing.T) { fmt.Println("Test: 30 chars") fmt.Println(GenerateRandomString(30)) } + +func TestPhasedStartup(t *testing.T) { + fmt.Printf("%c", PhasedStartupString()[1]) +}