Fix concurrent read writes (#3311)

mine
Vageesha B R 2025-04-28 20:48:34 +05:30 committed by GitHub
parent d3f646aafb
commit 8e3f453006
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import (
"log/slog" "log/slog"
"path" "path"
"strings" "strings"
"sync"
"github.com/derailed/k9s/internal/slogs" "github.com/derailed/k9s/internal/slogs"
"github.com/fvbommel/sortorder" "github.com/fvbommel/sortorder"
@ -25,23 +26,33 @@ type GVR struct {
raw, g, v, r, sr string raw, g, v, r, sr string
} }
type gvrCache map[string]*GVR type gvrCache struct {
data map[string]*GVR
sync.RWMutex
}
func (c gvrCache) add(gvr *GVR) { func (c *gvrCache) add(gvr *GVR) {
if c.get(gvr.String()) == nil { if c.get(gvr.String()) == nil {
c[gvr.String()] = gvr c.Lock()
c.data[gvr.String()] = gvr
c.Unlock()
} }
} }
func (c gvrCache) get(gvrs string) *GVR { func (c *gvrCache) get(gvrs string) *GVR {
if gvr, ok := c[gvrs]; ok { c.RLock()
defer c.RUnlock()
if gvr, ok := c.data[gvrs]; ok {
return gvr return gvr
} }
return nil return nil
} }
var gvrsCache = make(gvrCache) var gvrsCache = gvrCache{
data: make(map[string]*GVR),
}
// NewGVR builds a new gvr from a group, version, resource. // NewGVR builds a new gvr from a group, version, resource.
func NewGVR(s string) *GVR { func NewGVR(s string) *GVR {