-
Notifications
You must be signed in to change notification settings - Fork 401
/
discovery.go
169 lines (155 loc) · 3.38 KB
/
discovery.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package http
import (
"encoding/json"
"fmt"
"time"
"github.com/bilibili/discovery/model"
"github.com/go-kratos/kratos/pkg/ecode"
log "github.com/go-kratos/kratos/pkg/log"
bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
)
const (
_pollWaitSecond = 30 * time.Second
)
func register(c *bm.Context) {
arg := new(model.ArgRegister)
if err := c.Bind(arg); err != nil {
return
}
i := model.NewInstance(arg)
if i.Status == 0 || i.Status > 2 {
log.Error("register params status invalid")
return
}
if arg.Metadata != "" {
// check the metadata type is json
if !json.Valid([]byte(arg.Metadata)) {
c.JSON(nil, ecode.RequestErr)
log.Error("register params() metadata(%v) invalid json", arg.Metadata)
return
}
}
// register replication
if arg.DirtyTimestamp > 0 {
i.DirtyTimestamp = arg.DirtyTimestamp
}
dis.Register(c, i, arg.LatestTimestamp, arg.Replication, arg.FromZone)
c.JSON(nil, nil)
}
func renew(c *bm.Context) {
arg := new(model.ArgRenew)
if err := c.Bind(arg); err != nil {
return
}
// renew
c.JSON(dis.Renew(c, arg))
}
func cancel(c *bm.Context) {
arg := new(model.ArgCancel)
if err := c.Bind(arg); err != nil {
return
}
// cancel
c.JSON(nil, dis.Cancel(c, arg))
}
func fetchAll(c *bm.Context) {
c.JSON(dis.FetchAll(c), nil)
}
func fetch(c *bm.Context) {
arg := new(model.ArgFetch)
if err := c.Bind(arg); err != nil {
return
}
c.JSON(dis.Fetch(c, arg))
}
func fetchs(c *bm.Context) {
arg := new(model.ArgFetchs)
if err := c.Bind(arg); err != nil {
return
}
c.JSON(dis.Fetchs(c, arg))
}
func poll(c *bm.Context) {
arg := new(model.ArgPolls)
if err := c.Bind(arg); err != nil {
return
}
ch, new, miss, err := dis.Polls(c, arg)
if err != nil {
c.JSON(nil, err)
return
}
for _, mi := range miss {
if mi == arg.AppID[0] {
c.JSONMap(map[string]interface{}{
"message": fmt.Sprintf("%s not found", mi),
}, ecode.NothingFound)
return
}
}
// wait for instance change
select {
case e := <-ch:
c.JSON(e[arg.AppID[0]], nil)
if !new {
dis.DelConns(arg) // broadcast will delete all connections of appid
}
return
case <-time.After(_pollWaitSecond):
case <-c.Done():
}
c.JSON(nil, ecode.NotModified)
dis.DelConns(arg)
}
func polls(c *bm.Context) {
arg := new(model.ArgPolls)
if err := c.Bind(arg); err != nil {
return
}
if len(arg.AppID) != len(arg.LatestTimestamp) {
c.JSON(nil, ecode.RequestErr)
return
}
ch, new, miss, err := dis.Polls(c, arg)
if err != nil {
c.JSON(nil, err)
return
}
// wait for instance change
select {
case e := <-ch:
c.JSONMap(map[string]interface{}{
"data": e,
"error": map[ecode.Code]interface{}{
ecode.NothingFound: miss,
},
}, nil)
if !new {
dis.DelConns(arg) // broadcast will delete all connections of appid
}
return
case <-time.After(_pollWaitSecond):
case <-c.Done():
}
c.JSON(nil, ecode.NotModified)
dis.DelConns(arg)
}
func set(c *bm.Context) {
arg := new(model.ArgSet)
if err := c.Bind(arg); err != nil {
return
}
// len of color,status,metadata must equal to len of hostname or be zero
if (len(arg.Hostname) != len(arg.Status) && len(arg.Status) != 0) ||
(len(arg.Hostname) != len(arg.Metadata) && len(arg.Metadata) != 0) {
c.JSON(nil, ecode.RequestErr)
return
}
if arg.SetTimestamp == 0 {
arg.SetTimestamp = time.Now().UnixNano()
}
c.JSON(nil, dis.Set(c, arg))
}
func nodes(c *bm.Context) {
c.JSON(dis.Nodes(c), nil)
}