Make SetHeaderNormalizer safe to call while decoding
SetHeaderNormalizer reassigned the normalizer and copied a fresh sync.Map over the struct info cache while other goroutines were using it. Keep both in one value swapped atomically instead. Fixes #237.
This commit is contained in:
1 parent
391e8f0876
commit
f87ec7522c
3 files changed
+60
-8
No files matched your search
+3
-3
@@ -58,19 +58,19 @@ func removeZeroWidthChars(s string) string {
|
||||
}, s)
|
||||
}
|
||||
|
||||
var structInfoCache sync.Map
|
||||
var structMap = make(map[reflect.Type]*structInfo)
|
||||
var structMapMutex sync.RWMutex
|
||||
|
||||
func getStructInfo(rType reflect.Type) *structInfo {
|
||||
stInfo, ok := structInfoCache.Load(rType)
|
||||
structInfos := &loadHeaderNormalization().structInfos
|
||||
stInfo, ok := structInfos.Load(rType)
|
||||
if ok {
|
||||
return stInfo.(*structInfo)
|
||||
}
|
||||
|
||||
fieldsList, err := getFieldInfos(rType, []int{}, []string{}, make(map[reflect.Type]bool))
|
||||
stInfo = &structInfo{Fields: fieldsList, Err: err}
|
||||
structInfoCache.Store(rType, stInfo)
|
||||
structInfos.Store(rType, stInfo)
|
||||
|
||||
return stInfo.(*structInfo)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user