1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-11 17:50:58 +00:00

review: add retries to generating runid in case of collision

This commit is contained in:
Kwonunn 2025-01-26 11:56:04 +01:00 committed by Kwonunn
parent ef43d7c615
commit aa0c46539c

View file

@ -173,6 +173,7 @@ func (h *Handler) ExternalURL() string {
// The RunData contains the information about the repository. // The RunData contains the information about the repository.
// The function returns the 32-bit random key which the run will use to identify itself. // The function returns the 32-bit random key which the run will use to identify itself.
func (h *Handler) AddRun(data RunData) (string, error) { func (h *Handler) AddRun(data RunData) (string, error) {
for retries := 0; retries < 3; retries++ {
keyBytes := make([]byte, 4) keyBytes := make([]byte, 4)
_, err := rand.Read(keyBytes) _, err := rand.Read(keyBytes)
if err != nil { if err != nil {
@ -181,12 +182,13 @@ func (h *Handler) AddRun(data RunData) (string, error) {
key := hex.EncodeToString(keyBytes) key := hex.EncodeToString(keyBytes)
_, loaded := h.runs.LoadOrStore(key, data) _, loaded := h.runs.LoadOrStore(key, data)
if loaded { if !loaded {
return "", errors.New("Run id already exists") // 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 { func (h *Handler) RemoveRun(runID string) error {
_, existed := h.runs.LoadAndDelete(runID) _, existed := h.runs.LoadAndDelete(runID)