-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
49 lines (43 loc) · 961 Bytes
/
helper.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
package paxos_simple
import (
"fmt"
"net/rpc"
"strconv"
)
// 生成提案编号(format: 自增 + 议员id)
func generateNumber(id int, number float32) float32 {
var strNum string
if number == 0 {
strNum = fmt.Sprintf("1.%d", id)
n, err := strconv.ParseFloat(strNum, 32)
if err != nil {
panic("数字转换失败:" + err.Error())
}
return float32(n)
}
// 自增流水号
i := int(number) + 1
strNum = fmt.Sprintf("%d.%d", i, id)
n, err := strconv.ParseFloat(strNum, 32)
if err != nil {
panic("数字转换失败:" + err.Error())
}
return float32(n)
}
func logPrint(format string, a ...interface{}) {
fmt.Printf(format+"\n", a...)
}
func callRpc(addr, roleService, method string, arg interface{}, reply interface{}) error {
c, err := rpc.Dial("tcp", addr)
if err != nil {
return err
}
defer func() {
_ = c.Close()
}()
err = c.Call(roleService+"."+method, arg, reply)
if err != nil {
return err
}
return nil
}