-
Notifications
You must be signed in to change notification settings - Fork 21
/
pool.go
71 lines (62 loc) · 1.54 KB
/
pool.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package bizno
import (
"fmt"
"math/rand"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
)
// Pool is the interface for creating a new biz number generator pool
type Pool interface {
NextID() (string, error)
}
// bizNoPool is the biz number generator pool
type bizNoPool struct {
GeneratorList []Generator
}
// Config is the configuration for the biz number generator
type Config struct {
// EtcdEndpoints is the list of endpoints to connect to the etcd
EtcdEndpoints []string
// EtcdDialTimeout is the name of the etcd dial timeout
EtcdDialTimeout time.Duration
// GeneratorNum is the number of generators
GeneratorNum int
}
// NewPool creates a new biz number generator pool
func NewPool(c *Config) (Pool, error) {
if c == nil {
return nil, fmt.Errorf("config is nil")
}
if c.GeneratorNum == 0 {
c.GeneratorNum = 1
}
// initialize etcd client
cli, err := clientv3.New(clientv3.Config{
Endpoints: c.EtcdEndpoints,
DialTimeout: c.EtcdDialTimeout,
})
if err != nil {
return nil, err
}
p := &bizNoPool{
GeneratorList: make([]Generator, 0, c.GeneratorNum),
}
// initialize generator pool
for i := 0; i < c.GeneratorNum; i++ {
generator, err := NewGenerator(cli)
if err != nil {
return nil, err
}
p.GeneratorList = append(p.GeneratorList, generator)
}
return p, nil
}
// NextID is used to return the next generation number
func (s *bizNoPool) NextID() (string, error) {
rand.Seed(time.Now().UnixNano())
i := 0
if len(s.GeneratorList) > 1 {
i = rand.Intn(len(s.GeneratorList))
}
return s.GeneratorList[i].NextID()
}