Files
kustomize/api/internal/crawl/utils/utils.go
Haiyan Meng 171412cc98 Use RWMutex to control the map access
Without RWMutex, we may run into fatal error: concurrent map read and map write.
2020-06-23 11:25:37 -07:00

38 lines
642 B
Go

package utils
import "sync"
type SeenMap struct {
data map[string]string
lock sync.RWMutex
}
// TODO: add lock to avoid race condition
func (seen SeenMap) Seen(item string) bool {
seen.lock.RLock()
_, ok := seen.data[item]
seen.lock.RUnlock()
return ok
}
func (seen SeenMap) Set(k, v string) {
seen.lock.Lock()
seen.data[k] = v
seen.lock.Unlock()
}
// The caller should make sure that key is in the map.
func (seen SeenMap) Value(k string) string {
seen.lock.RLock()
v := seen.data[k]
seen.lock.RUnlock()
return v
}
func NewSeenMap() SeenMap {
return SeenMap{
data: make(map[string]string),
lock: sync.RWMutex{},
}
}