From 6e044463c764a6fb30ac4c81040557d30f776def Mon Sep 17 00:00:00 2001 From: Ab C <219340+abdala@users.noreply.github.com> Date: Wed, 15 Jan 2025 18:14:59 +0100 Subject: [PATCH] Use RWMutex for better concurrency --- Dockerfile | 6 +++--- secrets/cache.go | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5f33857..d8f20c8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.17-alpine as build +FROM golang:1.23-alpine AS build ARG CGO_ENABLED=0 @@ -12,14 +12,14 @@ RUN go mod download COPY . . -FROM build as build-sidecar +FROM build AS build-sidecar ARG CGO_ENABLED=0 RUN go build -ldflags="-s -w" -o /cache-server main_container.go \ && upx --best --lzma /cache-server -FROM build as build-extension +FROM build AS build-extension ARG CGO_ENABLED=0 diff --git a/secrets/cache.go b/secrets/cache.go index 1a8a770..25a6a0b 100644 --- a/secrets/cache.go +++ b/secrets/cache.go @@ -27,7 +27,7 @@ type CacheData struct { CacheExpiry time.Time } -var mutex = &sync.Mutex{} +var mutex = &sync.RWMutex{} func IsExpired(cacheExpiry time.Time) bool { return cacheExpiry.Before(time.Now()) @@ -48,9 +48,14 @@ func GetCacheExpiry() time.Time { } func GetSecretCache(name string, refresh string) string { + mutex.RLock() secret := secretCache[name] + mutex.RUnlock() + + println("GetSecretCache: ", name) if IsExpired(secret.CacheData.CacheExpiry) || refresh == "1" { + println("GetSecretCache: refresh") mutex.Lock() secretCache[name] = Secret{ CacheData: CacheData{