grule-rule-engine icon indicating copy to clipboard operation
grule-rule-engine copied to clipboard

The NewID interface may encounter lock contention issues during high concurrency

Open xiaohui-txh opened this issue 9 months ago • 1 comments

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

xiaohui-txh avatar Apr 03 '25 07:04 xiaohui-txh

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

Jonfor avatar Jun 16 '25 16:06 Jonfor