-
Notifications
You must be signed in to change notification settings - Fork 27
/
connections.go
345 lines (294 loc) · 9.34 KB
/
connections.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package graphqlws
import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/google/uuid"
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
)
const (
// Constants for operation message types
gqlConnectionInit = "connection_init"
gqlConnectionAck = "connection_ack"
gqlConnectionKeepAlive = "ka"
gqlConnectionError = "connection_error"
gqlConnectionTerminate = "connection_terminate"
gqlStart = "start"
gqlData = "data"
gqlError = "error"
gqlComplete = "complete"
gqlStop = "stop"
// Maximum size of incoming messages
readLimit = 4096
// Timeout for outgoing messages
writeTimeout = 10 * time.Second
)
// InitMessagePayload defines the parameters of a connection
// init message.
type InitMessagePayload struct {
AuthToken string `json:"authToken"`
}
// StartMessagePayload defines the parameters of an operation that
// a client requests to be started.
type StartMessagePayload struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
OperationName string `json:"operationName"`
}
// DataMessagePayload defines the result data of an operation.
type DataMessagePayload struct {
Data interface{} `json:"data"`
Errors []error `json:"errors"`
}
// OperationMessage represents a GraphQL WebSocket message.
type OperationMessage struct {
ID string `json:"id"`
Type string `json:"type"`
Payload interface{} `json:"payload"`
}
func (msg OperationMessage) String() string {
s, _ := json.Marshal(msg)
if s != nil {
return string(s)
}
return "<invalid>"
}
// AuthenticateFunc is a function that resolves an auth token
// into a user (or returns an error if that isn't possible).
type AuthenticateFunc func(token string) (interface{}, error)
// ConnectionEventHandlers define the event handlers for a connection.
// Event handlers allow other system components to react to events such
// as the connection closing or an operation being started or stopped.
type ConnectionEventHandlers struct {
// Close is called whenever the connection is closed, regardless of
// whether this happens because of an error or a deliberate termination
// by the client.
Close func(Connection)
// StartOperation is called whenever the client demands that a GraphQL
// operation be started (typically a subscription). Event handlers
// are expected to take the necessary steps to register the operation
// and send data back to the client with the results eventually.
StartOperation func(Connection, string, *StartMessagePayload) []error
// StopOperation is called whenever the client stops a previously
// started GraphQL operation (typically a subscription). Event handlers
// are expected to unregister the operation and stop sending result
// data to the client.
StopOperation func(Connection, string)
}
// ConnectionConfig defines the configuration parameters of a
// GraphQL WebSocket connection.
type ConnectionConfig struct {
Authenticate AuthenticateFunc
EventHandlers ConnectionEventHandlers
}
// Connection is an interface to represent GraphQL WebSocket connections.
// Each connection is associated with an ID that is unique to the server.
type Connection interface {
// ID returns the unique ID of the connection.
ID() string
// User returns the user associated with the connection (or nil).
User() interface{}
// SendData sends results of executing an operation (typically a
// subscription) to the client.
SendData(string, *DataMessagePayload)
// SendError sends an error to the client.
SendError(error)
}
/**
* The default implementation of the Connection interface.
*/
type connection struct {
id string
ws *websocket.Conn
config ConnectionConfig
logger *log.Entry
outgoing chan OperationMessage
user interface{}
closeMutex *sync.Mutex
closed bool
}
func operationMessageForType(messageType string) OperationMessage {
return OperationMessage{
Type: messageType,
}
}
// NewConnection establishes a GraphQL WebSocket connection. It implements
// the GraphQL WebSocket protocol by managing its internal state and handling
// the client-server communication.
func NewConnection(ws *websocket.Conn, config ConnectionConfig) Connection {
conn := new(connection)
conn.id = uuid.New().String()
conn.ws = ws
conn.config = config
conn.logger = NewLogger("connection/" + conn.id)
conn.closed = false
conn.closeMutex = &sync.Mutex{}
conn.outgoing = make(chan OperationMessage)
go conn.writeLoop()
go conn.readLoop()
conn.logger.Info("Created connection")
return conn
}
func (conn *connection) ID() string {
return conn.id
}
func (conn *connection) User() interface{} {
return conn.user
}
func (conn *connection) SendData(opID string, data *DataMessagePayload) {
msg := operationMessageForType(gqlData)
msg.ID = opID
msg.Payload = data
conn.closeMutex.Lock()
if !conn.closed {
conn.outgoing <- msg
}
conn.closeMutex.Unlock()
}
func (conn *connection) SendError(err error) {
msg := operationMessageForType(gqlError)
msg.Payload = err.Error()
conn.closeMutex.Lock()
if !conn.closed {
conn.outgoing <- msg
}
conn.closeMutex.Unlock()
}
func (conn *connection) sendOperationErrors(opID string, errs []error) {
if conn.closed {
return
}
msg := operationMessageForType(gqlError)
msg.ID = opID
msg.Payload = errs
conn.closeMutex.Lock()
if !conn.closed {
conn.outgoing <- msg
}
conn.closeMutex.Unlock()
}
func (conn *connection) close() {
// Close the write loop by closing the outgoing messages channels
conn.closeMutex.Lock()
conn.closed = true
close(conn.outgoing)
conn.closeMutex.Unlock()
// Notify event handlers
if conn.config.EventHandlers.Close != nil {
conn.config.EventHandlers.Close(conn)
}
conn.logger.Info("Closed connection")
}
func (conn *connection) writeLoop() {
// Close the WebSocket connection when leaving the write loop;
// this ensures the read loop is also terminated and the connection
// closed cleanly
defer conn.ws.Close()
for {
select {
// Take the next outgoing message from the channel
case msg, ok := <-conn.outgoing:
// Close the write loop when the outgoing messages channel is closed;
// this will close the connection
if !ok {
return
}
conn.logger.WithFields(log.Fields{
"msg": msg.String(),
}).Debug("Send message")
conn.ws.SetWriteDeadline(time.Now().Add(writeTimeout))
// Send the message to the client; if this times out, the WebSocket
// connection will be corrupt, hence we need to close the write loop
// and the connection immediately
if err := conn.ws.WriteJSON(msg); err != nil {
conn.logger.WithFields(log.Fields{
"err": err,
}).Warn("Sending message failed")
return
}
}
}
}
func (conn *connection) readLoop() {
// Close the WebSocket connection when leaving the read loop
defer conn.ws.Close()
conn.ws.SetReadLimit(readLimit)
for {
// Read the next message received from the client
rawPayload := json.RawMessage{}
msg := OperationMessage{
Payload: &rawPayload,
}
err := conn.ws.ReadJSON(&msg)
// If this causes an error, close the connection and read loop immediately;
// see https://github.com/gorilla/websocket/blob/master/conn.go#L924 for
// more information on why this is necessary
if err != nil {
conn.logger.WithFields(log.Fields{
"reason": err,
}).Warn("Closing connection")
conn.close()
return
}
conn.logger.WithFields(log.Fields{
"id": msg.ID,
"type": msg.Type,
}).Debug("Received message")
switch msg.Type {
// When the GraphQL WS connection is initiated, send an ACK back
case gqlConnectionInit:
data := InitMessagePayload{}
if err := json.Unmarshal(rawPayload, &data); err != nil {
conn.SendError(errors.New("Invalid GQL_CONNECTION_INIT payload"))
} else {
if conn.config.Authenticate != nil {
user, err := conn.config.Authenticate(data.AuthToken)
if err != nil {
msg := operationMessageForType(gqlConnectionError)
msg.Payload = fmt.Sprintf("Failed to authenticate user: %v", err)
conn.outgoing <- msg
} else {
conn.user = user
conn.outgoing <- operationMessageForType(gqlConnectionAck)
}
} else {
conn.outgoing <- operationMessageForType(gqlConnectionAck)
}
}
// Let event handlers deal with starting operations
case gqlStart:
if conn.config.EventHandlers.StartOperation != nil {
data := StartMessagePayload{}
if err := json.Unmarshal(rawPayload, &data); err != nil {
conn.SendError(errors.New("Invalid GQL_START payload"))
} else {
errs := conn.config.EventHandlers.StartOperation(conn, msg.ID, &data)
if errs != nil {
conn.sendOperationErrors(msg.ID, errs)
}
}
}
// Let event handlers deal with stopping operations
case gqlStop:
if conn.config.EventHandlers.StopOperation != nil {
conn.config.EventHandlers.StopOperation(conn, msg.ID)
}
// When the GraphQL WS connection is terminated by the client,
// close the connection and close the read loop
case gqlConnectionTerminate:
conn.logger.Debug("Connection terminated by client")
conn.close()
return
// GraphQL WS protocol messages that are not handled represent
// a bug in our implementation; make this very obvious by logging
// an error
default:
conn.logger.WithFields(log.Fields{
"msg": msg.String(),
}).Error("Unhandled message")
}
}
}