-
Notifications
You must be signed in to change notification settings - Fork 4
/
ws_notifier.go
202 lines (170 loc) · 4.17 KB
/
ws_notifier.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package notifier
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"sync"
"github.com/go-logr/logr"
"golang.org/x/net/http2"
"k8s.io/client-go/util/workqueue"
)
type Request struct {
Sid string `json:"sid,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
}
type task struct {
req Request
method string
}
// Waiter 用于等待一个Workspace的Pod处于Ready状态
type Waiter interface {
WaitFor(context.Context, string) error
}
// Notifier 用于通知一个Workspace可用(即它的Pod处于Ready状态)
// 注册或注销Workspace的IP地址到网关中,使得网关可以发现可用的Workspace
type Notifier interface {
Login(sid, endpoint string)
Logout(sid string)
Notify(sid string)
}
type WorkspaceNotifier struct {
logger logr.Logger
// 通过HTTP请求来从网关中注册或注销Workspace
clients []*http.Client
Url string
Token string
ctx context.Context
queue workqueue.Interface
mux sync.Mutex
// 保存sid到chan的映射,用于通知指定的Workspace已经可用或等待Workspace可用
wsc map[string]chan struct{}
}
func NewWorkspaceNotifier(ctx context.Context, logger logr.Logger, svcName, path, token string, workers int) (*WorkspaceNotifier, error) {
// https://servicename/internal/endpoint
w := &WorkspaceNotifier{
logger: logger,
Url: fmt.Sprintf("https://%s%s", svcName, path),
Token: token,
ctx: ctx,
queue: workqueue.NewRateLimitingQueue(workqueue.DefaultItemBasedRateLimiter()),
wsc: make(map[string]chan struct{}),
}
if workers <= 0 {
workers = 4
}
go func() {
<-ctx.Done()
w.queue.ShutDown()
}()
// 开启http2
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 不校验服务端证书
}
err := http2.ConfigureTransport(transport)
if err != nil {
logger.Error(err, "configure transport")
return nil, err
}
for i := 0; i < workers; i++ {
w.clients = append(w.clients, &http.Client{
Transport: transport,
})
go w.worker(i)
}
return w, nil
}
// Login 通过HTTP请求将Pod的IP地址和端口注册到网关中
// 使得网关可以访问到Pod
func (w *WorkspaceNotifier) Login(sid, endpoint string) {
w.queue.Add(task{
req: Request{Sid: sid, Endpoint: endpoint},
method: http.MethodPost,
})
}
// Logout 从网关中注销Pod,防止网关访问到不存在或其它用户的Pod
func (w *WorkspaceNotifier) Logout(sid string) {
w.queue.Add(task{
req: Request{Sid: sid},
method: http.MethodDelete,
})
}
func (w *WorkspaceNotifier) worker(i int) {
client := w.clients[i]
for {
item, shutdown := w.queue.Get()
if shutdown {
return
}
tsk := item.(task)
err := w.doRequest(client, tsk.req, tsk.method)
if err != nil {
w.logger.Error(err, "do request", "method", tsk.method, "sid", tsk.req.Sid)
} else {
w.queue.Done(item)
}
}
}
func (w *WorkspaceNotifier) doRequest(client *http.Client, req Request, method string) error {
data, err := json.Marshal(req)
if err != nil {
return err
}
reader := bytes.NewReader(data)
request, err := http.NewRequest(method, w.Url, reader)
if err != nil {
return err
}
request.Header.Set("token", w.Token)
_, err = client.Do(request)
if err != nil {
return err
}
return nil
}
// WaitFor 等待Pod可用
func (w *WorkspaceNotifier) WaitFor(ctx context.Context, sid string) error {
w.logger.V(5).Info("WaitFor ", "sid", sid)
var (
ch chan struct{}
ok bool
)
w.mux.Lock()
ch, ok = w.wsc[sid]
if !ok {
w.logger.V(5).Info("creating chan", "sid", sid)
ch = make(chan struct{})
w.wsc[sid] = ch
} else {
w.logger.V(5).Info("find chan", "sid", sid)
}
w.mux.Unlock()
defer func() {
w.mux.Lock()
w.logger.V(5).Info("deleting chan", "sid", sid)
delete(w.wsc, sid)
w.mux.Unlock()
}()
w.logger.V(5).Info("waiting for", "sid", sid)
select {
case <-ctx.Done():
return ctx.Err()
case <-ch:
}
return nil
}
// Notify 通知Pod已经可用
func (w *WorkspaceNotifier) Notify(sid string) {
w.logger.V(5).Info("Notify ", "sid", sid)
w.mux.Lock()
ch, ok := w.wsc[sid]
w.mux.Unlock()
if !ok {
w.logger.V(5).Info("cant find chan", "sid", sid)
return
}
w.logger.V(5).Info("closed chan", "sid", sid)
close(ch)
}