forked from panco95/go-garden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
71 lines (60 loc) · 1.28 KB
/
sync.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 core
import (
"bytes"
"context"
"strings"
"github.com/jxcia/go-garden/core/log"
)
type Rpc int
type SyncRoutesArgs struct {
Yml []byte
}
type SyncRoutesReply struct {
Result bool
}
//SyncRoutes sync routes.yml method
func (r *Rpc) SyncRoutes(ctx context.Context, args *SyncRoutesArgs, reply *SyncRoutesReply) error {
reply.Result = true
if err := writeFile("./configs/routes.yml", args.Yml); err != nil {
reply.Result = false
}
return nil
}
func (g *Garden) sendRoutes() {
fileData, err := readFile("configs/routes.yml")
if err != nil {
log.Error("syncRoutes", err)
return
}
if len(fileData) == 0 {
return
}
if bytes.Compare(g.syncCache, fileData) == 0 {
return
}
g.syncCache = fileData
args := SyncRoutesArgs{
Yml: fileData,
}
reply := SyncRoutesReply{}
for k1, v1 := range g.services {
for k2, v2 := range v1.Nodes {
if strings.Compare(v2.Addr, g.GetServiceId()) == 0 {
continue
}
addr, err := g.getServiceRpcAddr(k1, k2)
if err != nil {
log.Error("getServiceRpcAddr", err)
continue
}
if err := rpcCall(nil, addr, k1, "SyncRoutes", &args, &reply, 10000); err != nil {
log.Error("syncRoutes", err)
return
}
if !reply.Result {
log.Error("syncRoutes", "fail")
}
log.Info("syncRoutes", "success")
}
}
}