Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

identify: add MsgTimeout #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"io"
"os"
"time"

"github.com/pkg/errors"
)
Expand All @@ -22,12 +23,17 @@ type Identify struct {
// UserAgent represents the type of the client, by default it is set to
// nsq.DefaultUserAgent.
UserAgent string

// Configure the server-side message timeout
// for messages delivered to this client.
MsgTimeout time.Duration
}

type identifyBody struct {
ClientID string `json:"client_id,omitempty"`
Hostname string `json:"hostname,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
ClientID string `json:"client_id,omitempty"`
Hostname string `json:"hostname,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
MsgTimeout int64 `json:"msg_timeout,omitempty"`
}

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

if data, err = json.Marshal(identifyBody{
ClientID: c.ClientID,
Hostname: c.Hostname,
UserAgent: c.UserAgent,
ClientID: c.ClientID,
Hostname: c.Hostname,
UserAgent: c.UserAgent,
MsgTimeout: int64(c.MsgTimeout / time.Millisecond),
}); err != nil {
return
}
Expand Down Expand Up @@ -75,9 +82,10 @@ func readIdentify(r *bufio.Reader) (cmd Identify, err error) {
}

cmd = Identify{
ClientID: body.ClientID,
Hostname: body.Hostname,
UserAgent: body.UserAgent,
ClientID: body.ClientID,
Hostname: body.Hostname,
UserAgent: body.UserAgent,
MsgTimeout: time.Duration(body.MsgTimeout) * time.Millisecond,
}
return
}
Expand Down
12 changes: 11 additions & 1 deletion identify_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package nsq

import "testing"
import (
"testing"
"time"
)

func TestIdentify(t *testing.T) {
testCommand(t, "IDENTIFY", Identify{
ClientID: "0123456789",
Hostname: "localhost",
UserAgent: "nsq-go/test",
})

testCommand(t, "IDENTIFY", Identify{
ClientID: "0123456789",
Hostname: "localhost",
UserAgent: "nsq-go/test",
MsgTimeout: time.Second * 5,
})
}