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

Add timeout support for client #3

Merged
merged 1 commit into from
Nov 17, 2017
Merged
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
17 changes: 14 additions & 3 deletions relp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"strconv"
"strings"
"time"
)

const relpVersion = 0
Expand Down Expand Up @@ -176,9 +177,9 @@ func NewServer(host string, port int, autoAck bool) (server Server, err error) {
return server, nil
}

// NewClient - Starts a new RELP client
func NewClient(host string, port int) (client Client, err error) {
client.connection, err = net.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
// NewClientTimeout - Starts a new RELP client with Dial timeout set
func NewClientTimeout(host string, port int, timeout time.Duration) (client Client, err error) {
client.connection, err = net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), timeout)
if err != nil {
return client, err
}
Expand Down Expand Up @@ -207,6 +208,11 @@ func NewClient(host string, port int) (client Client, err error) {
return client, err
}

// NewClient - Starts a new RELP client with Dial timeout set
func NewClient(host string, port int) (client Client, err error) {
return NewClientTimeout(host, port, 0)
}

// Close - Stops listening for connections and closes the message channel
func (s Server) Close() {
s.done = true
Expand Down Expand Up @@ -275,6 +281,11 @@ func (c *Client) SendMessage(msg Message) (err error) {
return err
}

// SetDeadline - make the next operation timeout if not completed before the given time
func (c *Client) SetDeadline(t time.Time) error {
return c.connection.SetDeadline(t)
}

// Close - Closes the connection gracefully
func (c Client) Close() (err error) {
closeMessage := Message{
Expand Down