-
Notifications
You must be signed in to change notification settings - Fork 0
/
YouPlusEntity.go
142 lines (135 loc) · 3.14 KB
/
YouPlusEntity.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package entry
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/project-xpolaris/youplustoolkit/youplus/rpc"
"github.com/rs/xid"
"github.com/sirupsen/logrus"
"time"
)
const (
ErrorCodeUnknown = 9999
ErrorCodeEntityNotFound = 6001
)
type EntityExport struct {
Urls []string `json:"urls"`
Extra interface{} `json:"extra"`
}
type EntityClient struct {
Name string
Version int64
Instance string
Export interface{}
Client *rpc.YouPlusRPCClient
HeartbeatRate int64
StopHeartbeatContext context.Context
StopHeartbeatContextFunc context.CancelFunc
}
func NewEntityClient(name string, version int64, export interface{}, client *rpc.YouPlusRPCClient) *EntityClient {
instance := fmt.Sprintf("%s_%s", name, xid.New().String())
return &EntityClient{
Name: name,
Version: version,
Export: export,
Client: client,
Instance: instance,
}
}
func (e *EntityClient) Register() error {
client, err := e.Client.GetClient()
if err != nil {
return err
}
result, err := client.RegisterEntry(context.Background(), &rpc.RegisterEntryRequest{
Name: &e.Name,
Version: &e.Version,
Instance: &e.Instance,
})
if err != nil {
return err
}
if !result.GetSuccess() {
return errors.New(result.GetReason())
}
return nil
}
func (e *EntityClient) Unregister() error {
client, err := e.Client.GetClient()
if err != nil {
return err
}
result, err := client.UnregisterEntry(context.Background(), &rpc.UnregisterEntryRequest{
Instance: &e.Instance,
})
if err != nil {
return err
}
if !result.GetSuccess() {
return errors.New(result.GetReason())
}
return nil
}
func (e *EntityClient) UpdateExport(data interface{}) error {
client, err := e.Client.GetClient()
if err != nil {
return err
}
raw, err := json.Marshal(&data)
e.Export = data
if err != nil {
return err
}
rawString := string(raw)
result, err := client.UpdateEntryExport(context.Background(), &rpc.UpdateEntryExportRequest{
Instance: &e.Instance,
Data: &rawString,
})
if err != nil {
return err
}
if !result.GetSuccess() {
return errors.New(result.GetReason())
}
return nil
}
func (e *EntityClient) StartHeartbeat(ctx context.Context) error {
if e.StopHeartbeatContext != nil {
return errors.New("only one heartbeat")
}
e.StopHeartbeatContext, e.StopHeartbeatContextFunc = context.WithCancel(ctx)
go func() {
for {
select {
case <-time.After(time.Duration(e.HeartbeatRate) * time.Millisecond):
client, err := e.Client.GetClient()
if err != nil {
logrus.Info(err)
continue
}
state := "online"
reply, err := client.EntryHeartbeat(ctx, &rpc.HeartbeatRequest{
Name: &e.Name,
Instance: &e.Instance,
State: &state,
})
if err != nil {
logrus.Info(err)
continue
}
if !*reply.Success {
logrus.Info(*reply.Reason)
if *reply.Code == ErrorCodeEntityNotFound {
logrus.Info("try to register entity again")
e.Register()
e.UpdateExport(e.Export)
}
}
case <-e.StopHeartbeatContext.Done():
return
}
}
}()
return nil
}