THRIFT-5152: Separate timeout in TSSLSocket
Client: go
We separated timeout in go's TSocket into connect timeout and socket
timeout in 81334cd, this change does the same for TSSLSocket to keep
them consistent.
Also rename the arg in NewTSocketFromConnTimeout from connTimeout to
socketTimeout, because in that function we already have a connection,
so connect timeout is never used again. The timeout passed into that
function is really for socket timeout, not connect timeout. The name of
that function actually means "New TSocket From Conn (with) Timeout", not
"New TSocket From ConnTimeout" (I guess that's where the original
confusion came from).
Also add the missing change note for the breaking change.
diff --git a/CHANGES.md b/CHANGES.md
index ceb8f8b..b6c2021 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -15,6 +15,7 @@
- [THRIFT-5164](https://issues.apache.org/jira/browse/THRIFT-5164) - In Go library TProcessor interface now includes ProcessorMap and AddToProcessorMap functions.
- [THRIFT-5186](https://issues.apache.org/jira/browse/THRIFT-5186) - cpp: use all getaddrinfo() results when retrying failed bind() in T{Nonblocking,}ServerSocket
- [THRIFT-5233](https://issues.apache.org/jira/browse/THRIFT-5233) - go: Now all Read*, Write* and Skip functions in TProtocol accept context arg
+- [THRIFT-5152](https://issues.apache.org/jira/browse/THRIFT-5152) - go: TSocket and TSSLSocket now have separated connect timeout and socket timeout
### Java
diff --git a/lib/go/thrift/socket.go b/lib/go/thrift/socket.go
index eb94faf..af75dd1 100644
--- a/lib/go/thrift/socket.go
+++ b/lib/go/thrift/socket.go
@@ -57,8 +57,8 @@
}
// Creates a TSocket from an existing net.Conn
-func NewTSocketFromConnTimeout(conn net.Conn, connTimeout time.Duration) *TSocket {
- return &TSocket{conn: wrapSocketConn(conn), addr: conn.RemoteAddr(), connectTimeout: connTimeout, socketTimeout: connTimeout}
+func NewTSocketFromConnTimeout(conn net.Conn, socketTimeout time.Duration) *TSocket {
+ return &TSocket{conn: wrapSocketConn(conn), addr: conn.RemoteAddr(), socketTimeout: socketTimeout}
}
// Sets the connect timeout
diff --git a/lib/go/thrift/ssl_socket.go b/lib/go/thrift/ssl_socket.go
index 31ef3a0..15ae96f 100644
--- a/lib/go/thrift/ssl_socket.go
+++ b/lib/go/thrift/ssl_socket.go
@@ -33,9 +33,11 @@
hostPort string
// addr is nil when hostPort is not "", and is only used when the
// TSSLSocket is constructed from a net.Addr.
- addr net.Addr
- timeout time.Duration
- cfg *tls.Config
+ addr net.Addr
+ cfg *tls.Config
+
+ connectTimeout time.Duration
+ socketTimeout time.Duration
}
// NewTSSLSocket creates a net.Conn-backed TTransport, given a host and port and tls Configuration
@@ -43,38 +45,59 @@
// Example:
// trans, err := thrift.NewTSSLSocket("localhost:9090", nil)
func NewTSSLSocket(hostPort string, cfg *tls.Config) (*TSSLSocket, error) {
- return NewTSSLSocketTimeout(hostPort, cfg, 0)
+ return NewTSSLSocketTimeout(hostPort, cfg, 0, 0)
}
// NewTSSLSocketTimeout creates a net.Conn-backed TTransport, given a host and port
-// it also accepts a tls Configuration and a timeout as a time.Duration
-func NewTSSLSocketTimeout(hostPort string, cfg *tls.Config, timeout time.Duration) (*TSSLSocket, error) {
+// it also accepts a tls Configuration and connect/socket timeouts as time.Duration
+func NewTSSLSocketTimeout(hostPort string, cfg *tls.Config, connectTimeout, socketTimeout time.Duration) (*TSSLSocket, error) {
if cfg.MinVersion == 0 {
cfg.MinVersion = tls.VersionTLS10
}
- return &TSSLSocket{hostPort: hostPort, timeout: timeout, cfg: cfg}, nil
+ return &TSSLSocket{
+ hostPort: hostPort,
+ cfg: cfg,
+ connectTimeout: connectTimeout,
+ socketTimeout: socketTimeout,
+ }, nil
}
// Creates a TSSLSocket from a net.Addr
-func NewTSSLSocketFromAddrTimeout(addr net.Addr, cfg *tls.Config, timeout time.Duration) *TSSLSocket {
- return &TSSLSocket{addr: addr, timeout: timeout, cfg: cfg}
+func NewTSSLSocketFromAddrTimeout(addr net.Addr, cfg *tls.Config, connectTimeout, socketTimeout time.Duration) *TSSLSocket {
+ return &TSSLSocket{
+ addr: addr,
+ cfg: cfg,
+ connectTimeout: connectTimeout,
+ socketTimeout: socketTimeout,
+ }
}
// Creates a TSSLSocket from an existing net.Conn
-func NewTSSLSocketFromConnTimeout(conn net.Conn, cfg *tls.Config, timeout time.Duration) *TSSLSocket {
- return &TSSLSocket{conn: wrapSocketConn(conn), addr: conn.RemoteAddr(), timeout: timeout, cfg: cfg}
+func NewTSSLSocketFromConnTimeout(conn net.Conn, cfg *tls.Config, socketTimeout time.Duration) *TSSLSocket {
+ return &TSSLSocket{
+ conn: wrapSocketConn(conn),
+ addr: conn.RemoteAddr(),
+ cfg: cfg,
+ socketTimeout: socketTimeout,
+ }
+}
+
+// Sets the connect timeout
+func (p *TSSLSocket) SetConnTimeout(timeout time.Duration) error {
+ p.connectTimeout = timeout
+ return nil
}
// Sets the socket timeout
-func (p *TSSLSocket) SetTimeout(timeout time.Duration) error {
- p.timeout = timeout
+func (p *TSSLSocket) SetSocketTimeout(timeout time.Duration) error {
+ p.socketTimeout = timeout
return nil
}
func (p *TSSLSocket) pushDeadline(read, write bool) {
var t time.Time
- if p.timeout > 0 {
- t = time.Now().Add(time.Duration(p.timeout))
+ if p.socketTimeout > 0 {
+ t = time.Now().Add(time.Duration(p.socketTimeout))
}
if read && write {
p.conn.SetDeadline(t)
@@ -93,7 +116,7 @@
if p.hostPort != "" {
if p.conn, err = createSocketConnFromReturn(tls.DialWithDialer(
&net.Dialer{
- Timeout: p.timeout,
+ Timeout: p.connectTimeout,
},
"tcp",
p.hostPort,
@@ -116,7 +139,7 @@
}
if p.conn, err = createSocketConnFromReturn(tls.DialWithDialer(
&net.Dialer{
- Timeout: p.timeout,
+ Timeout: p.connectTimeout,
},
p.addr.Network(),
p.addr.String(),