Skip to content

Commit

Permalink
add mutex to cached responses
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoandredinis authored and github-actions committed Dec 17, 2024
1 parent c4a51c7 commit 96ff5bb
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/srv/discovery/fetchers/kube_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ type ProtoChecker struct {
// When the Kubernetes Service ResourceVersion changes, then we assume the protocol might've changed as well, so the cache is invalidated.
// Only protocol checkers that require a network connection are cached.
cacheKubernetesServiceProtocol map[kubernetesNameNamespace]appResourceVersionProtocol
cacheMU sync.RWMutex
}

type appResourceVersionProtocol struct {
Expand Down Expand Up @@ -368,10 +369,13 @@ func (p *ProtoChecker) CheckProtocol(service v1.Service, port v1.ServicePort) st
}

key := kubernetesNameNamespace{namespace: service.Namespace, name: service.Name}
if versionProtocol, ok := p.cacheKubernetesServiceProtocol[key]; ok {
if versionProtocol.resourceVersion == service.ResourceVersion {
return versionProtocol.protocol
}

p.cacheMU.RLock()
versionProtocol, keyIsCached := p.cacheKubernetesServiceProtocol[key]
p.cacheMU.RUnlock()

if keyIsCached && versionProtocol.resourceVersion == service.ResourceVersion {
return versionProtocol.protocol
}

var result string
Expand All @@ -391,10 +395,12 @@ func (p *ProtoChecker) CheckProtocol(service v1.Service, port v1.ServicePort) st

}

p.cacheMU.Lock()
p.cacheKubernetesServiceProtocol[key] = appResourceVersionProtocol{
resourceVersion: service.ResourceVersion,
protocol: result,
}
p.cacheMU.Unlock()

return result
}

0 comments on commit 96ff5bb

Please sign in to comment.