Skip to content

Commit d35e53a

Browse files
authored
Merge pull request #34 from segmentio/svls/add-msg-timeout-support
Added support for setting msg_timeout in Identify command
2 parents 5d534ec + 80a6b80 commit d35e53a

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

identify.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"io"
88
"os"
9+
"time"
910

1011
"github.com/pkg/errors"
1112
)
@@ -22,12 +23,18 @@ type Identify struct {
2223
// UserAgent represents the type of the client, by default it is set to
2324
// nsq.DefaultUserAgent.
2425
UserAgent string
26+
27+
// MessageTimeout can bet set to configure the server-side message timeout
28+
// for messages delivered to this consumer. By default it is not sent to
29+
// the server.
30+
MessageTimeout time.Duration
2531
}
2632

2733
type identifyBody struct {
28-
ClientID string `json:"client_id,omitempty"`
29-
Hostname string `json:"hostname,omitempty"`
30-
UserAgent string `json:"user_agent,omitempty"`
34+
ClientID string `json:"client_id,omitempty"`
35+
Hostname string `json:"hostname,omitempty"`
36+
UserAgent string `json:"user_agent,omitempty"`
37+
MessageTimeout int `json:"msg_timeout,omitempty"`
3138
}
3239

3340
// Name returns the name of the command in order to satisfy the Command
@@ -42,9 +49,10 @@ func (c Identify) Write(w *bufio.Writer) (err error) {
4249
var data []byte
4350

4451
if data, err = json.Marshal(identifyBody{
45-
ClientID: c.ClientID,
46-
Hostname: c.Hostname,
47-
UserAgent: c.UserAgent,
52+
ClientID: c.ClientID,
53+
Hostname: c.Hostname,
54+
UserAgent: c.UserAgent,
55+
MessageTimeout: int(c.MessageTimeout / time.Millisecond),
4856
}); err != nil {
4957
return
5058
}
@@ -75,9 +83,10 @@ func readIdentify(r *bufio.Reader) (cmd Identify, err error) {
7583
}
7684

7785
cmd = Identify{
78-
ClientID: body.ClientID,
79-
Hostname: body.Hostname,
80-
UserAgent: body.UserAgent,
86+
ClientID: body.ClientID,
87+
Hostname: body.Hostname,
88+
UserAgent: body.UserAgent,
89+
MessageTimeout: time.Millisecond * time.Duration(body.MessageTimeout),
8190
}
8291
return
8392
}

identify_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
package nsq
22

3-
import "testing"
3+
import (
4+
"testing"
5+
"time"
6+
)
47

58
func TestIdentify(t *testing.T) {
69
testCommand(t, "IDENTIFY", Identify{
710
ClientID: "0123456789",
811
Hostname: "localhost",
912
UserAgent: "nsq-go/test",
13+
// timeout omitted.
14+
})
15+
testCommand(t, "IDENTIFY", Identify{
16+
ClientID: "0123456789",
17+
Hostname: "localhost",
18+
UserAgent: "nsq-go/test",
19+
MessageTimeout: 10 * time.Minute,
1020
})
1121
}

0 commit comments

Comments
 (0)