Use go-gorm/sharding in concurrency, occur DATA RACE sometimes on production environment.
The code below may occur DATA RACE:
func (s *Sharding) switchConn(db *gorm.DB) {
// Support ignore sharding in some case, like:
// When DoubleWrite is enabled, we need to query database schema
// information by table name during the migration.
if _, ok := db.Get(ShardingIgnoreStoreKey); !ok {
s.ConnPool = &ConnPool{ConnPool: db.Statement.ConnPool, sharding: s}
db.Statement.ConnPool = s.ConnPool
}
}
Update:
func (s *Sharding) switchConn(db *gorm.DB) {
// Support ignore sharding in some case, like:
// When DoubleWrite is enabled, we need to query database schema
// information by table name during the migration.
if _, ok := db.Get(ShardingIgnoreStoreKey); !ok {
s.mutex.Lock()
if db.Statement.ConnPool != nil {
if _, ok := db.Statement.ConnPool.(ConnPool); !ok {
db.Statement.ConnPool = &ConnPool{ConnPool: db.Statement.ConnPool, sharding: s}
}
}
s.mutex.Unlock()
}
}
GORM Playground Link
https://github.com/go-gorm/playground/pull/567
Description
Use go-gorm/sharding in concurrency, occur DATA RACE sometimes on production environment.
The code below may occur DATA RACE:
Update: