-
Notifications
You must be signed in to change notification settings - Fork 0
/
snowflake.go
30 lines (26 loc) · 923 Bytes
/
snowflake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package snowflake
import (
hold "github.com/JerryZhou343/leaf-go/domain/aggregate/snowflake/holder"
"github.com/JerryZhou343/leaf-go/domain/aggregate/snowflake/idgen"
"github.com/JerryZhou343/leaf-go/infra/conf"
"github.com/bilibili/kratos/pkg/net/ip"
"strconv"
"strings"
)
type Snowflake struct {
generator *idgen.SnowflakeIDGen
holder hold.Holder
config *conf.Config
}
func NewSnowflake(config *conf.Config) (ret *Snowflake, err error) {
ret = &Snowflake{}
ret.generator, err = idgen.NewSnowflakeIDGenImpl(int64(config.Snowflake.WorkerId))
addrInfo := strings.Split(config.Grpc.Addr, ":")
port, err := strconv.Atoi(addrInfo[1])
ret.holder, err = hold.NewEtcdHolder(ip.InternalIP(), port, config.Snowflake.SrvName, config.Snowflake.WorkerId)
err = ret.holder.Init(config.Snowflake.EtcdCluster)
return ret, err
}
func (s *Snowflake) Get(key string) (int64, error) {
return s.generator.Get(key)
}