-
Notifications
You must be signed in to change notification settings - Fork 9
/
ws.go
328 lines (304 loc) · 7.54 KB
/
ws.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package jaal
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strings"
"sync"
"github.com/gorilla/websocket"
"gocloud.dev/pubsub"
"go.appointy.com/jaal/graphql"
"go.appointy.com/jaal/jerrors"
"go.appointy.com/jaal/schemabuilder"
)
// HTTPSubHandler implements the handler required for executing the graphql subscriptions
func HTTPSubHandler(schema *graphql.Schema, s *pubsub.Subscription) (http.Handler, func()) {
source := make(chan *event)
sessions := &sessions{
data: map[string][]chan *event{},
chans: map[string][]chan struct{}{},
}
return &httpSubHandler{
handler: handler{
schema: schema,
executor: &graphql.Executor{},
},
qmHandler: HTTPHandler(schema),
upgrader: &websocket.Upgrader{},
source: source,
sessions: sessions,
}, func() {
go startListening(s, source, func() {
exit(sessions)
})
go listenSource(source, sessions)
}
}
func listenSource(events chan *event, ss *sessions) {
for evt := range events {
ss.RLock()
for _, v := range ss.data {
for _, s := range v {
s <- evt
}
}
ss.RUnlock()
}
}
func startListening(s *pubsub.Subscription, source chan<- *event, cancel func()) {
for {
msg, err := s.Receive(context.Background())
if err != nil {
fmt.Println("Pubsub failed: ", err)
cancel()
return
}
msg.Ack()
source <- &event{
payload: msg.Body,
typ: msg.Metadata["type"],
}
}
}
type httpSubHandler struct {
handler
qmHandler http.Handler
upgrader *websocket.Upgrader
source chan *event
sessions *sessions
}
type event struct {
typ string
payload []byte
}
type sessions struct {
sync.RWMutex
data map[string][]chan *event
chans map[string][]chan struct{}
}
type wsMessage struct {
Type string `json:"type"`
Id string `json:"id,omitempty"`
Payload json.RawMessage `json:"payload,omitempty"`
}
type gqlPayload struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
OpName string `json:"operationName"`
}
func (h *httpSubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet { // If not a subscription request route to normal handler
h.qmHandler.ServeHTTP(w, r)
return
}
log.Println("Request Headers:", r.Header)
// Check origin and set response headers
h.upgrader.CheckOrigin = func(r *http.Request) bool { return true }
res := http.Header{}
res["Sec-Websocket-Protocol"] = []string{"graphql-ws"}
con, err := h.upgrader.Upgrade(w, r, res)
if err != nil {
fmt.Println("failed to upgrade to websocket:", err)
return
}
defer con.Close()
if con.Subprotocol() != "graphql-ws" {
fmt.Println("invalid subprotocol")
return
}
var msg wsMessage
if err := con.ReadJSON(&msg); err != nil {
fmt.Println("failed to parse websocket message: ", err)
return
}
conn := &webConn{conn: con}
if msg.Type != "connection_init" {
if err := writeResponse(conn, "connection_error", "", nil, errors.New("expected init message")); err != nil {
fmt.Println(err)
return
}
}
if err := writeResponse(conn, "connection_ack", "", nil, nil); err != nil {
fmt.Println(err)
return
}
loop:
for {
var data wsMessage
if err := con.ReadJSON(&data); err != nil {
if err := writeResponse(conn, "connection_error", "", nil, err); err != nil {
fmt.Println(err)
return
}
fmt.Println(err)
}
switch data.Type {
case "start":
var gql gqlPayload
if err := json.Unmarshal(data.Payload, &gql); err != nil {
if err := writeResponse(conn, "connection_error", "", nil, err); err != nil {
fmt.Println(err)
return
}
fmt.Println(err)
return
}
query, err := graphql.Parse(gql.Query, gql.Variables)
if err != nil {
if er := writeResponse(conn, "error", data.Id, nil, err); er != nil {
fmt.Println(err)
return
}
fmt.Println(err)
return
}
schema := h.schema.Subscription
if err := graphql.ValidateQuery(r.Context(), schema, query.SelectionSet); err != nil {
if er := writeResponse(conn, "error", data.Id, nil, err); er != nil {
fmt.Println(er)
return
}
fmt.Println(err)
return
}
for _, v := range query.SelectionSet.Selections {
end := make(chan struct{}, 1)
modQuery := &graphql.Query{
Name: query.Name,
Kind: query.Kind,
SelectionSet: &graphql.SelectionSet{
Selections: []*graphql.Selection{v},
Fragments: query.SelectionSet.Fragments,
},
}
go func(conn *webConn, data *wsMessage, schema graphql.Type, query *graphql.Query, end chan struct{}, w http.ResponseWriter, r *http.Request) {
if err := h.serveHTTP(conn, *data, schema, query, end, w, r); err != nil {
fmt.Println("Id:", data.Id, ": terminated: ", err)
}
h.sessions.Lock()
if _, ok := h.sessions.data[data.Id]; ok {
if err := writeResponse(conn, "complete", data.Id, nil, nil); err != nil {
fmt.Println(err)
}
fmt.Println("Id:", data.Id, ": terminated.")
}
delete(h.sessions.data, data.Id)
delete(h.sessions.chans, data.Id)
h.sessions.Unlock()
}(conn, &data, schema, modQuery, end, w, r)
}
case "stop":
h.sessions.RLock()
for _, v := range h.sessions.chans[data.Id] {
v <- struct{}{}
}
h.sessions.RUnlock()
case "connection_terminate":
exit(h.sessions)
break loop
default:
}
}
}
func exit(ss *sessions) {
ss.RLock()
for _, v := range ss.chans {
for _, s := range v {
s <- struct{}{}
}
}
for _, v := range ss.data {
for _, s := range v {
close(s)
}
}
ss.RUnlock()
}
type webConn struct {
sync.Mutex
conn *websocket.Conn
}
func writeResponse(w *webConn, typ, id string, r interface{}, er error) error {
var payload []byte
var err error
if typ == "data" {
if er != nil {
payload, err = json.Marshal(httpResponse{Data: r, Errors: []*jerrors.Error{jerrors.ConvertError(er)}})
if err != nil {
return err
}
} else {
payload, err = json.Marshal(httpResponse{Data: r, Errors: []*jerrors.Error{}})
if err != nil {
return err
}
}
} else if typ == "error" || typ == "connection_error" {
str := strings.Replace(er.Error(), "\"", "\\\"", -1)
payload = json.RawMessage("{ \"error\" : \"" + str + "\"}")
}
res := wsMessage{
Type: typ,
Id: id,
Payload: payload,
}
w.Lock()
if err := w.conn.WriteJSON(res); err != nil {
w.Unlock()
return err
}
w.Unlock()
return nil
}
func (h *httpSubHandler) serveHTTP(conn *webConn, data wsMessage, schema graphql.Type, query *graphql.Query, end chan struct{}, w http.ResponseWriter, r *http.Request) error {
sid := data.Id
sess := make(chan *event)
h.sessions.Lock()
h.sessions.data[sid] = append(h.sessions.data[sid], sess)
h.sessions.chans[sid] = append(h.sessions.chans[sid], end)
h.sessions.Unlock()
cls := func(ss *sessions, sid string) {
ss.Lock()
for _, v := range ss.data[sid] {
close(v)
for range v {
}
}
for _, v := range ss.chans[sid] {
close(v)
for range v {
}
}
ss.Unlock()
}
// Listening on usrChannel for any source event of subType
for msg := range sess {
select {
case <-end:
cls(h.sessions, sid)
return nil
default:
if err := func() error {
res, err := h.executor.Execute(r.Context(), schema, &schemabuilder.Subscription{msg.payload}, query)
if err == graphql.ErrNoUpdate {
return nil
}
rer := err
if err := writeResponse(conn, "data", data.Id, res, rer); err != nil {
return err
}
if rer != nil {
return err
}
return nil
}(); err != nil {
cls(h.sessions, sid)
return err
}
}
}
return nil
}