mirror of https://github.com/go-gorm/gorm.git
Race fix. Changes modelStructsMap implementation from map with mutex to sync.Map (#2022)
* fix (https://github.com/jinzhu/gorm/issues/1407) * changed map with mutex to sync.Map (https://github.com/jinzhu/gorm/issues/1407) * removed newModelStructsMap func * commit to rerun pipeline, comment changed
This commit is contained in:
parent
409121d9e3
commit
0e04d414d5
3
main.go
3
main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -162,7 +163,7 @@ func (s *DB) HasBlockGlobalUpdate() bool {
|
||||||
|
|
||||||
// SingularTable use singular table by default
|
// SingularTable use singular table by default
|
||||||
func (s *DB) SingularTable(enable bool) {
|
func (s *DB) SingularTable(enable bool) {
|
||||||
modelStructsMap = newModelStructsMap()
|
modelStructsMap = sync.Map{}
|
||||||
s.parent.singularTable = enable
|
s.parent.singularTable = enable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,28 +17,7 @@ var DefaultTableNameHandler = func(db *DB, defaultTableName string) string {
|
||||||
return defaultTableName
|
return defaultTableName
|
||||||
}
|
}
|
||||||
|
|
||||||
type safeModelStructsMap struct {
|
var modelStructsMap sync.Map
|
||||||
m map[reflect.Type]*ModelStruct
|
|
||||||
l *sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *safeModelStructsMap) Set(key reflect.Type, value *ModelStruct) {
|
|
||||||
s.l.Lock()
|
|
||||||
defer s.l.Unlock()
|
|
||||||
s.m[key] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *safeModelStructsMap) Get(key reflect.Type) *ModelStruct {
|
|
||||||
s.l.RLock()
|
|
||||||
defer s.l.RUnlock()
|
|
||||||
return s.m[key]
|
|
||||||
}
|
|
||||||
|
|
||||||
func newModelStructsMap() *safeModelStructsMap {
|
|
||||||
return &safeModelStructsMap{l: new(sync.RWMutex), m: make(map[reflect.Type]*ModelStruct)}
|
|
||||||
}
|
|
||||||
|
|
||||||
var modelStructsMap = newModelStructsMap()
|
|
||||||
|
|
||||||
// ModelStruct model definition
|
// ModelStruct model definition
|
||||||
type ModelStruct struct {
|
type ModelStruct struct {
|
||||||
|
@ -48,7 +27,7 @@ type ModelStruct struct {
|
||||||
defaultTableName string
|
defaultTableName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName get model's table name
|
// TableName returns model's table name
|
||||||
func (s *ModelStruct) TableName(db *DB) string {
|
func (s *ModelStruct) TableName(db *DB) string {
|
||||||
if s.defaultTableName == "" && db != nil && s.ModelType != nil {
|
if s.defaultTableName == "" && db != nil && s.ModelType != nil {
|
||||||
// Set default table name
|
// Set default table name
|
||||||
|
@ -152,8 +131,8 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Cached model struct
|
// Get Cached model struct
|
||||||
if value := modelStructsMap.Get(reflectType); value != nil {
|
if value, ok := modelStructsMap.Load(reflectType); ok && value != nil {
|
||||||
return value
|
return value.(*ModelStruct)
|
||||||
}
|
}
|
||||||
|
|
||||||
modelStruct.ModelType = reflectType
|
modelStruct.ModelType = reflectType
|
||||||
|
@ -601,7 +580,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
modelStructsMap.Set(reflectType, &modelStruct)
|
modelStructsMap.Store(reflectType, &modelStruct)
|
||||||
|
|
||||||
return &modelStruct
|
return &modelStruct
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue