Skip to content

Commit db3da6f

Browse files
committed
fix error: concurrent map read and map write
1 parent 7f717ce commit db3da6f

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

mapping/to_event.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package mapping
22

3+
import "sync"
4+
35
// turnToShortColumn is a default seeker that simply translates the column's name if a match exists.
46
func seekFromShortColumn(m *Mapping, column string, value string) (bool, string, string) {
57
r, ok := m.Raws[column]
@@ -41,19 +43,17 @@ type cacheEntry struct {
4143

4244
// reverseTransformer is to use for data stored in a "reversed way" meaning that the column qualifier is the actual data and is a kind of enum value
4345
type reverseSeeker struct {
44-
cache map[string]*cacheEntry
46+
cache sync.Map
4547
}
4648

4749
func newReverseSeeker() *reverseSeeker {
48-
return &reverseSeeker{
49-
cache: make(map[string]*cacheEntry),
50-
}
50+
return &reverseSeeker{}
5151
}
5252

5353
// seekFromCache returns the column and value from the cache if it exists.
5454
func (c *reverseSeeker) seekFromCache(_ *Mapping, column string, _ string) (bool, string, string) {
55-
if entry, ok := c.cache[column]; ok {
56-
return true, entry.col, entry.value
55+
if entry, ok := c.cache.Load(column); ok {
56+
return true, entry.(cacheEntry).col, entry.(cacheEntry).value
5757
}
5858
return false, "", ""
5959
}
@@ -63,7 +63,7 @@ func (c *reverseSeeker) seekFromMapping(m *Mapping, column string, _ string) (bo
6363
for _, ma := range m.Reversed {
6464
for short, val := range ma.Values {
6565
if column == short {
66-
c.cache[column] = &cacheEntry{col: ma.Name, value: val}
66+
c.cache.Store(column, cacheEntry{col: ma.Name, value: val})
6767
return true, ma.Name, val
6868
}
6969
}

0 commit comments

Comments
 (0)