1
0
Fork 0

Set WriteDeadline for marker byte

master
Philip O'Toole 3 years ago
parent 32ec72e982
commit c13e2b0ab4

@ -2,6 +2,7 @@ package tcp
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"net" "net"
"time" "time"
) )
@ -23,28 +24,32 @@ type Dialer struct {
} }
// Dial dials the cluster service at the given addr and returns a connection. // Dial dials the cluster service at the given addr and returns a connection.
func (d *Dialer) Dial(addr string, timeout time.Duration) (net.Conn, error) { func (d *Dialer) Dial(addr string, timeout time.Duration) (conn net.Conn, retErr error) {
dialer := &net.Dialer{Timeout: timeout} dialer := &net.Dialer{Timeout: timeout}
var err error
var conn net.Conn
if d.remoteEncrypted { if d.remoteEncrypted {
conf := &tls.Config{ conf := &tls.Config{
InsecureSkipVerify: d.skipVerify, InsecureSkipVerify: d.skipVerify,
} }
conn, err = tls.DialWithDialer(dialer, "tcp", addr, conf) conn, retErr = tls.DialWithDialer(dialer, "tcp", addr, conf)
} else { } else {
conn, err = dialer.Dial("tcp", addr) conn, retErr = dialer.Dial("tcp", addr)
} }
if err != nil { if retErr != nil {
return nil, err return nil, retErr
}
defer func() {
if retErr != nil {
conn.Close()
} }
}()
// Write a marker byte to indicate message type. // Write a marker byte to indicate message type.
_, err = conn.Write([]byte{d.header}) if err := conn.SetWriteDeadline(time.Now().Add(timeout)); err != nil {
if err != nil { return nil, fmt.Errorf("failed to set WriteDeadline for header: %s", err.Error())
conn.Close() }
if _, err := conn.Write([]byte{d.header}); err != nil {
return nil, err return nil, err
} }
return conn, err return conn, nil
} }

Loading…
Cancel
Save