From bb53f4833f4b05f9944d445e17052024ba5cc4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=BCller?= Date: Fri, 6 Sep 2024 09:43:54 +0200 Subject: [PATCH] internal/worker/client.go: refactor reading worker ID Adds a helper function to the worker client instead of redeclaring the same inline function. --- internal/worker/client.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/internal/worker/client.go b/internal/worker/client.go index 16fbe007a..8f572bccc 100644 --- a/internal/worker/client.go +++ b/internal/worker/client.go @@ -203,16 +203,18 @@ func (c *Client) registerWorker() error { return nil } +func (c *Client) getWorkerID() uuid.UUID { + c.workerIDMu.RLock() + defer c.workerIDMu.RUnlock() + return c.workerID +} + func (c *Client) workerHeartbeat() { //nolint:staticcheck // avoid SA1015, this is an endless function for range time.Tick(time.Minute * 1) { - workerID := func() uuid.UUID { - c.workerIDMu.RLock() - defer c.workerIDMu.RUnlock() - return c.workerID - } + workerID := c.getWorkerID() - if workerID() == uuid.Nil { + if workerID == uuid.Nil { err := c.registerWorker() if err != nil { logrus.Errorf("Error registering worker, %v", err) @@ -220,7 +222,7 @@ func (c *Client) workerHeartbeat() { } } - url, err := c.serverURL.Parse(fmt.Sprintf("workers/%s/status", workerID())) + url, err := c.serverURL.Parse(fmt.Sprintf("workers/%s/status", workerID)) if err != nil { logrus.Errorf("Error parsing worker status: %v", err) continue @@ -344,12 +346,7 @@ func (c *Client) RequestJob(types []string, arch string) (Job, error) { Arch: arch, } - workerID := func() uuid.UUID { - c.workerIDMu.RLock() - defer c.workerIDMu.RUnlock() - return c.workerID - }() - + workerID := c.getWorkerID() if workerID != uuid.Nil { reqBody.WorkerId = common.ToPtr(workerID.String()) }