Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RWMutex for better concurrency #2

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.17-alpine as build
FROM golang:1.23-alpine AS build

ARG CGO_ENABLED=0

Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion secrets/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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{
Expand Down
Loading