1
0
mirror of synced 2025-12-18 11:08:03 +08:00
Files
SnowFlake-IdGenerator/Go/source/idgen/YitIdHelper.go
2021-04-03 17:20:01 +08:00

70 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 版权属于yitter(yitter@126.com)
* 代码编辑guoyahao
* 代码修订yitter
* 开源地址https://gitee.com/yitter/idgenerator
*/
package idgen
import (
"sync"
)
//var yitIdHelper *YitIdHelper
//var once sync.Once
var idGenerator *DefaultIdGenerator
var singletonMutex sync.Mutex
type YitIdHelper struct {
idGenInstance interface {
NewLong() uint64
}
}
//
//func GetIns() *YitIdHelper {
// once.Do(func() {
// yitIdHelper = &YitIdHelper{}
// })
// return yitIdHelper
//}
//
//func (yih *YitIdHelper) GetIdGenInstance() interface{} {
// return yih.idGenInstance
//}
//
//func (yih *YitIdHelper) SetIdGenerator(options *contract.IdGeneratorOptions) {
// yih.idGenInstance = NewDefaultIdGenerator(options)
//}
//
//func (yih *YitIdHelper) NextId() uint64 {
// once.Do(func() {
// if yih.idGenInstance == nil {
// options := contract.NewIdGeneratorOptions(1)
// yih.idGenInstance = NewDefaultIdGenerator(options)
// }
// })
//
// return yih.idGenInstance.NewLong()
//}
func SetIdGenerator(options *IdGeneratorOptions) {
singletonMutex.Lock()
idGenerator = NewDefaultIdGenerator(options)
singletonMutex.Unlock()
}
func NextId() uint64 {
if idGenerator == nil {
singletonMutex.Lock()
if idGenerator == nil {
options := NewIdGeneratorOptions(1)
idGenerator = NewDefaultIdGenerator(options)
}
singletonMutex.Unlock()
}
return idGenerator.NewLong()
}