The NewID interface may encounter lock contention issues during high concurrency
When I call NewKnowledgeBaseInstance every time I call Execute, there is a lock contention issue with 100 concurrent calls
The source code is as follows: package unique
import ( "fmt" "sync" "time" )
var ( offset int64 = 0 lastMS int64 = 0 mutex sync.Mutex )
// NewID will create a new unique ID string for this runtime. // Uniqueness between system or apps is not necessary. func NewID() string { mutex.Lock() defer mutex.Unlock() millisUnix := time.Now().Unix() if lastMS == millisUnix { offset++
return fmt.Sprintf("%d-%d", lastMS, offset)
}
lastMS = millisUnix
offset = 0
return fmt.Sprintf("%d-%d", lastMS, offset)
}
Suggest using atomic variables
Hey there,
This is fixed in a fork I help maintain if you are interested. Here is the PR that fixes it: https://github.com/DataWiseHQ/grule-rule-engine/pull/14