forked from Terry-Mao/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
63 lines (54 loc) · 2.08 KB
/
worker.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
// Copyright © 2014 Terry Mao All rights reserved.
// This file is part of gosnowflake.
// gosnowflake is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gosnowflake is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gosnowflake. If not, see <http://www.gnu.org/licenses/>.
// Reference: https://github.com/twitter/snowflake
package main
import (
log "github.com/alecthomas/log4go"
"errors"
"fmt"
)
type Workers []*IdWorker
// NewWorkers new id workers instance.
func NewWorkers() (Workers, error) {
idWorkers := make([]*IdWorker, maxWorkerId)
for _, workerId := range MyConf.WorkerId {
if t := idWorkers[workerId]; t != nil {
log.Error("init workerId: %d already exists", workerId)
return nil, fmt.Errorf("init workerId: %d exists", workerId)
}
idWorker, err := NewIdWorker(workerId, MyConf.DatacenterId, MyConf.Twepoch)
if err != nil {
log.Error("NewIdWorker(%d, %d) error(%v)", MyConf.DatacenterId, workerId)
return nil, err
}
idWorkers[workerId] = idWorker
if err := RegWorkerId(workerId); err != nil {
log.Error("RegWorkerId(%d) error(%v)", workerId, err)
return nil, err
}
}
return Workers(idWorkers), nil
}
// Get get a specified worker by workerId.
func (w Workers) Get(workerId int64) (*IdWorker, error) {
if workerId > maxWorkerId || workerId < 0 {
log.Error("worker Id can't be greater than %d or less than 0", maxWorkerId)
return nil, errors.New(fmt.Sprintf("worker Id: %d error", workerId))
}
if worker := w[workerId]; worker == nil {
log.Warn("workerId: %d not register", workerId)
return nil, fmt.Errorf("snowflake workerId: %d don't register in this service", workerId)
} else {
return worker, nil
}
}