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.
32 lines
652 B
32 lines
652 B
6 years ago
|
/* This module keeps all struct used as singleton */
|
||
|
|
||
|
package utils
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
"sync/atomic"
|
||
|
)
|
||
|
|
||
6 years ago
|
// UniqueValidatorID defines the structure of unique validator ID
|
||
|
type UniqueValidatorID struct {
|
||
|
uniqueID uint32
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
var instance *UniqueValidatorID
|
||
6 years ago
|
var once sync.Once
|
||
|
|
||
6 years ago
|
// GetUniqueValidatorIDInstance returns a singleton instance
|
||
|
func GetUniqueValidatorIDInstance() *UniqueValidatorID {
|
||
6 years ago
|
once.Do(func() {
|
||
6 years ago
|
instance = &UniqueValidatorID{
|
||
|
uniqueID: 0,
|
||
6 years ago
|
}
|
||
|
})
|
||
|
return instance
|
||
|
}
|
||
|
|
||
6 years ago
|
// GetUniqueID returns a unique ID and increment the internal variable
|
||
|
func (s *UniqueValidatorID) GetUniqueID() uint32 {
|
||
|
return atomic.AddUint32(&s.uniqueID, 1)
|
||
6 years ago
|
}
|