forked from Terry-Mao/gosnowflake
/
rpc.go
112 lines (100 loc) · 3.12 KB
/
rpc.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright © 2014 Terry Mao, LiuDing 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/>.
package main
import (
"fmt"
"github.com/golang/glog"
"net"
"net/rpc"
"time"
)
type SnowflakeRPC struct {
idWorkers []*IdWorker
}
// StartRPC start rpc listen.
func InitRPC() error {
if err := SanityCheckPeers(); err != nil {
glog.Errorf("SanityCheckPeers() error(%v)", err)
return err
}
idWorkers := make([]*IdWorker, maxWorkerId)
for _, workerId := range MyConf.WorkerId {
if t := idWorkers[workerId]; t != nil {
glog.Errorf("init workerId: %d already exists", workerId)
return fmt.Errorf("init workerId: %d exists", workerId)
}
idWorker, err := NewIdWorker(workerId, MyConf.DatacenterId)
if err != nil {
glog.Errorf("NewIdWorker(%d, %d) error(%v)", MyConf.DatacenterId, workerId)
return err
}
idWorkers[workerId] = idWorker
if err := RegWorkerId(workerId); err != nil {
glog.Errorf("RegWorkerId(%d) error(%v)", workerId, err)
return err
}
}
s := &SnowflakeRPC{idWorkers: idWorkers}
rpc.Register(s)
for _, bind := range MyConf.RPCBind {
glog.Infof("start listen rpc addr: \"%s\"", bind)
go rpcListen(bind)
}
return nil
}
// rpcListen start rpc listen.
func rpcListen(bind string) {
l, err := net.Listen("tcp", bind)
if err != nil {
glog.Errorf("net.Listen(\"tcp\", \"%s\") error(%v)", bind, err)
panic(err)
}
// if process exit, then close the rpc bind
defer func() {
glog.Infof("rpc addr: \"%s\" close", bind)
if err := l.Close(); err != nil {
glog.Errorf("listener.Close() error(%v)", err)
}
}()
rpc.Accept(l)
}
// NextId generate a id.
func (s *SnowflakeRPC) NextId(workerId int64, id *int64) error {
if worker := s.idWorkers[workerId]; worker == nil {
glog.Warningf("workerId: %d not register", workerId)
return fmt.Errorf("snowflake workerId: %d don't register in this service", workerId)
} else {
if tid, err := worker.NextId(); err != nil {
glog.Errorf("worker.NextId() error(%v)", err)
return err
} else {
*id = tid
}
}
return nil
}
// DatacenterId return the services's datacenterId.
func (s *SnowflakeRPC) DatacenterId(ignore int, dataCenterId *int64) error {
*dataCenterId = MyConf.DatacenterId
return nil
}
// Timestamp return the service current unixnano
func (s *SnowflakeRPC) Timestamp(ignore int, timestamp *int64) error {
*timestamp = time.Now().UnixNano()
return nil
}
// Ping return the service status.
func (s *SnowflakeRPC) Ping(ignore int, status *int) error {
*status = 0
return nil
}