Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 0 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ func (c *Client) messageHandler() {
for {
if c.scanner.Scan() {
line := c.scanner.Text()
//nolint: gocritic
if line == "error id=0 msg=ok" {
c.err <- nil
} else if matches := respTrailerRe.FindStringSubmatch(line); len(matches) == 4 {
Expand Down Expand Up @@ -253,9 +252,6 @@ func (c *Client) workHandler() {
}

func (c *Client) process(data string) {
if err := c.conn.SetWriteDeadline(time.Now().Add(c.timeout)); err != nil {
c.err <- err
}
if _, err := c.conn.Write([]byte(data)); err != nil {
c.err <- err
}
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestClientWriteFail(t *testing.T) {
if !assert.NoError(t, err) {
return
}
assert.NoError(t, c.conn.(*legacyConnection).Conn.(*net.TCPConn).CloseWrite())
assert.NoError(t, c.conn.(*legacyConnection).Conn.(*writeTimeoutConn).Conn.(*net.TCPConn).CloseWrite())

_, err = c.Exec("version")
assert.Error(t, err)
Expand Down
31 changes: 29 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ const (
DefaultSSHPort = 10022
)

type writeTimeoutConn struct {
net.Conn
timeout time.Duration
}

func (c *writeTimeoutConn) Write(p []byte) (n int, err error) {
if err = c.Conn.SetWriteDeadline(time.Now().Add(c.timeout)); err != nil {
return 0, fmt.Errorf("writeTimeoutConn: SetWriteDeadline: %w", err)
}
if n, err = c.Conn.Write(p); err != nil {
return n, fmt.Errorf("writeTimeoutConn: write: %w", err)
}
return n, nil
}

// legacyConnection is an insecure TCP connection.
type legacyConnection struct {
net.Conn
Expand All @@ -32,10 +47,16 @@ func (c *legacyConnection) Connect(addr string, timeout time.Duration) error {
return err
}

c.Conn, err = net.DialTimeout("tcp", addr, timeout)
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return fmt.Errorf("legacy connection: dial: %w", err)
}

c.Conn = &writeTimeoutConn{
Conn: conn,
timeout: timeout,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using what the user believes is the connect timeout and not the read / write timeout, same for the ssh version.

Given the use is passing in a read / write / dial timeout the best option might be to just clarify it's use, thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using NewClient() with the option Timeout() the behavior didn't change. This is also documented for the Timeout option. "Timeout sets read / write / dial timeout for a TeamSpeak 3 Client."

I added some documentation to the Connect methods for clarification though.

}

return nil
}

Expand All @@ -53,10 +74,16 @@ func (c *sshConnection) Connect(addr string, timeout time.Duration) error {
return err
}

if c.Conn, err = net.DialTimeout("tcp", addr, timeout); err != nil {
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return fmt.Errorf("ssh connection: dial: %w", err)
}

c.Conn = &writeTimeoutConn{
Conn: conn,
timeout: timeout,
}

clientConn, chans, reqs, err := ssh.NewClientConn(c.Conn, addr, c.config)
if err != nil {
return fmt.Errorf("ssh connecion: ssh client conn: %w", err)
Expand Down