Skip to content

Commit 422411a

Browse files
authored
Merge pull request #3 from beyondnetworks/add-request-timeout
Add request timeout
2 parents b19f145 + 6f0c498 commit 422411a

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package grammes
2222

2323
import (
2424
"sync"
25+
"time"
2526

2627
"github.com/northwesternmutual/grammes/gremconnect"
2728
"github.com/northwesternmutual/grammes/gremerror"
@@ -31,6 +32,7 @@ import (
3132

3233
// maxConCurrentMessages determines the size of the request channel.
3334
const maxConCurrentMessages = 3
35+
const defaultTimeout = time.Minute
3436

3537
// Client is used to handle the graph, schema, connection,
3638
// and basic debug logging when querying the graph database.
@@ -60,6 +62,8 @@ type Client struct {
6062
broken bool
6163
// logger is used to log out debug statements and errors from the client.
6264
logger logging.Logger
65+
// requestTimeout is used for timeouting requests that a response is not received for
66+
requestTimeout time.Duration
6367
}
6468

6569
// setupClient default values some fields in the client.
@@ -71,6 +75,7 @@ func setupClient() *Client {
7175
resultMessenger: &sync.Map{},
7276
logger: logging.NewNilLogger(),
7377
gremlinVersion: "3",
78+
requestTimeout: defaultTimeout,
7479
}
7580
}
7681

configuration.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,11 @@ func WithReadingWait(interval time.Duration) ClientConfiguration {
102102
c.conn.SetReadingWait(interval)
103103
}
104104
}
105+
106+
// WithRequestTimeout sets the timeout when
107+
// reading a request from the gremlin server
108+
func WithRequestTimeout(interval time.Duration) ClientConfiguration {
109+
return func(c *Client) {
110+
c.requestTimeout = interval
111+
}
112+
}

response.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ package grammes
2222

2323
import (
2424
"encoding/json"
25+
"errors"
26+
"time"
2527

2628
"github.com/northwesternmutual/grammes/gremconnect"
2729
)
@@ -71,20 +73,32 @@ func (c *Client) retrieveResponse(id string) ([][]byte, error) {
7173
dataPart []byte
7274
)
7375

74-
if n := <-notifier.(chan int); n == 1 {
75-
if dataI, ok := c.results.Load(id); ok {
76-
for _, d := range dataI.([]interface{}) {
77-
if err, ok = d.(error); ok {
78-
break
79-
}
80-
if dataPart, err = jsonMarshalData(d); err != nil {
81-
break
76+
timeout := make(chan bool, 1)
77+
78+
time.AfterFunc(c.requestTimeout, func() {
79+
timeout <- true
80+
})
81+
82+
for n := 1;n == 1; {
83+
select {
84+
case n = <-notifier.(chan int):
85+
86+
if dataI, ok := c.results.Load(id); ok {
87+
for _, d := range dataI.([]interface{}) {
88+
if err, ok = d.(error); ok {
89+
break
90+
}
91+
if dataPart, err = jsonMarshalData(d); err != nil {
92+
break
93+
}
94+
data = append(data, dataPart)
8295
}
83-
data = append(data, dataPart)
96+
close(notifier.(chan int))
97+
c.resultMessenger.Delete(id)
98+
c.deleteResponse(id)
8499
}
85-
close(notifier.(chan int))
86-
c.resultMessenger.Delete(id)
87-
c.deleteResponse(id)
100+
case <-timeout:
101+
return nil, errors.New("request failed with timeout")
88102
}
89103
}
90104

0 commit comments

Comments
 (0)