The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
woop/utils/singleton.go

31 lines
652 B

/* This module keeps all struct used as singleton */
package utils
import (
"sync"
"sync/atomic"
)
// UniqueValidatorID defines the structure of unique validator ID
type UniqueValidatorID struct {
uniqueID uint32
}
var instance *UniqueValidatorID
var once sync.Once
// GetUniqueValidatorIDInstance returns a singleton instance
func GetUniqueValidatorIDInstance() *UniqueValidatorID {
once.Do(func() {
instance = &UniqueValidatorID{
uniqueID: 0,
}
})
return instance
}
// GetUniqueID returns a unique ID and increment the internal variable
func (s *UniqueValidatorID) GetUniqueID() uint32 {
return atomic.AddUint32(&s.uniqueID, 1)
}