Skip to content

Commit

Permalink
fix concurrent map write for base map usage
Browse files Browse the repository at this point in the history
Signed-off-by: Callum Styan <[email protected]>
  • Loading branch information
cstyan committed Jan 5, 2024
1 parent 56caa15 commit ffb171a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pkg/logql/log/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type BaseLabelsBuilder struct {
// LabelsBuilder is the same as labels.Builder but tailored for this package.
type LabelsBuilder struct {
base labels.Labels
mapLock sync.RWMutex
baseMap map[string]string
buf labels.Labels
currentResult LabelsResult
Expand Down Expand Up @@ -497,10 +498,16 @@ func (b *LabelsBuilder) IntoMap(m map[string]string) {

func (b *LabelsBuilder) Map() map[string]string {
if !b.hasDel() && !b.hasAdd() && !b.HasErr() {
if b.baseMap == nil {
b.baseMap = b.base.Map()
b.mapLock.RLock()
if b.baseMap != nil {
defer b.mapLock.RUnlock()
return b.baseMap
}
return b.baseMap
b.mapLock.RUnlock()
b.mapLock.Lock()
b.baseMap = b.base.Map()
b.mapLock.Unlock()

}
b.buf = b.UnsortedLabels(b.buf)
// todo should we also cache maps since limited by the result ?
Expand Down

0 comments on commit ffb171a

Please sign in to comment.