From aa0c46539c2386336fc1550b4c6981b647426dc0 Mon Sep 17 00:00:00 2001 From: Kwonunn Date: Sun, 26 Jan 2025 11:56:04 +0100 Subject: [PATCH] review: add retries to generating runid in case of collision --- act/cacheproxy/handler.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/act/cacheproxy/handler.go b/act/cacheproxy/handler.go index b79fa47f..293b2bc7 100644 --- a/act/cacheproxy/handler.go +++ b/act/cacheproxy/handler.go @@ -173,19 +173,21 @@ func (h *Handler) ExternalURL() string { // The RunData contains the information about the repository. // The function returns the 32-bit random key which the run will use to identify itself. func (h *Handler) AddRun(data RunData) (string, error) { - keyBytes := make([]byte, 4) - _, err := rand.Read(keyBytes) - if err != nil { - return "", errors.New("Could not generate the run id") - } - key := hex.EncodeToString(keyBytes) + for retries := 0; retries < 3; retries++ { + keyBytes := make([]byte, 4) + _, err := rand.Read(keyBytes) + if err != nil { + return "", errors.New("Could not generate the run id") + } + key := hex.EncodeToString(keyBytes) - _, loaded := h.runs.LoadOrStore(key, data) - if loaded { - return "", errors.New("Run id already exists") + _, loaded := h.runs.LoadOrStore(key, data) + if !loaded { + // The key was unique and added successfully + return key, nil + } } - - return key, nil + return "", errors.New("Repeated collisions in generating run id") } func (h *Handler) RemoveRun(runID string) error {