-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.go
131 lines (119 loc) · 3.3 KB
/
response.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
package gremgoser
import (
"bytes"
"encoding/json"
"time"
"github.com/google/uuid"
)
func (c *Client) handleResponse(msg []byte) error {
resp, err := marshalResponse(msg)
if err != nil && err != Error407Authenticate {
c.debug("error handling response: %s", err)
c.saveResponse(resp)
return err
}
c.verbose("handling response: %+v", resp)
if resp.Status.Code == 407 { //Server request authentication
return c.authenticate(resp.RequestId)
}
c.saveResponse(resp)
return nil
}
// marshalResponse creates a response struct for every incoming response for further manipulation
func marshalResponse(msg []byte) (*GremlinResponse, error) {
resp := &GremlinResponse{}
decoder := json.NewDecoder(bytes.NewReader(msg))
decoder.UseNumber()
err := decoder.Decode(resp)
//err := json.Unmarshal(msg, resp)
if err != nil {
return resp, err
}
err = responseDetectError(resp.Status.Code)
if err != nil {
return resp, err
}
return resp, nil
}
// saveResponse makes the response available for retrieval by the requester. Mutexes are used for thread safety.
func (c *Client) saveResponse(resp *GremlinResponse) {
c.respMutex.Lock()
var container []*GremlinRespData
existingData, ok := c.results.Load(resp.RequestId) // Retrieve old data container (for requests with multiple responses)
if ok {
container = existingData.([]*GremlinRespData)
}
c.verbose("RequestId: %s, existing data: %+v", resp.RequestId, container)
container = append(container, resp.Result.Data...)
c.verbose("RequestId: %s, new data: %+v", resp.RequestId, container)
c.results.Store(resp.RequestId, container) // Add new data to buffer for future retrieval
respNotifier, _ := c.responseNotifier.LoadOrStore(resp.RequestId, make(chan int, 1))
if resp.Status.Code != 206 {
respNotifier.(chan int) <- 1
}
c.respMutex.Unlock()
}
// retrieveResponse retrieves the response saved by saveResponse.
func (c *Client) retrieveResponse(id uuid.UUID) []*GremlinRespData {
data := []*GremlinRespData{}
resp, _ := c.responseNotifier.Load(id)
timeout := make(chan bool, 1)
go func() {
time.Sleep(c.conf.ReadingWait)
timeout <- true
}()
select {
case n := <-resp.(chan int):
if n == 1 {
if dataI, ok := c.results.Load(id); ok {
data, ok = dataI.([]*GremlinRespData)
if !ok {
return nil
}
close(resp.(chan int))
c.responseNotifier.Delete(id)
c.deleteResponse(id)
}
}
case <-timeout:
// the read from resp ch has timed out
c.debug("timeout on response")
return nil
}
return data
}
// deleteRespones deletes the response from the container. Used for cleanup purposes by requester.
func (c *Client) deleteResponse(id uuid.UUID) {
c.results.Delete(id)
return
}
// responseDetectError detects any possible errors in responses from Gremlin Server and generates an error for each code
func responseDetectError(code int) error {
switch code {
case 200:
return nil
case 204:
return nil
case 206:
return nil
case 401:
return Error401Unauthorized
case 407:
return Error407Authenticate
case 498:
return Error498MalformedRequest
case 499:
return Error499InvalidRequestArguments
case 500:
return Error500ServerError
case 597:
return Error597ScriptEvaluationError
case 598:
return Error598ServerTimeout
case 599:
return Error599ServerSerializationError
default:
return ErrorUnknownCode
}
return nil
}