Skip to content

Commit

Permalink
replace Timeout with ConnectTimeout/SocketTimeout/HttpReadTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
iambus authored and beltran committed Oct 22, 2021
1 parent 9701375 commit d164d8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
26 changes: 14 additions & 12 deletions hive.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ type ConnectConfiguration struct {
TLSConfig *tls.Config
ZookeeperNamespace string
Database string
Timeout time.Duration
ConnectTimeout time.Duration
SocketTimeout time.Duration
HttpReadTimeout time.Duration
DialContext DialContextFunc
}

Expand Down Expand Up @@ -187,33 +189,33 @@ func innerConnect(ctx context.Context, host string, port int, auth string,
addr := fmt.Sprintf("%s:%d", host, port)
if configuration.DialContext != nil {
var netConn net.Conn
netConn, err = dial(ctx, addr, configuration.DialContext, configuration.Timeout)
netConn, err = dial(ctx, addr, configuration.DialContext, configuration.ConnectTimeout)
if err != nil {
return
}
if configuration.TLSConfig != nil {
socket = thrift.NewTSSLSocketFromConnConf(netConn, &thrift.TConfiguration{
ConnectTimeout: configuration.Timeout,
SocketTimeout: configuration.Timeout,
ConnectTimeout: configuration.ConnectTimeout,
SocketTimeout: configuration.SocketTimeout,
TLSConfig: configuration.TLSConfig,
})
} else {
socket = thrift.NewTSocketFromConnConf(netConn, &thrift.TConfiguration{
ConnectTimeout: configuration.Timeout,
SocketTimeout: configuration.Timeout,
ConnectTimeout: configuration.ConnectTimeout,
SocketTimeout: configuration.SocketTimeout,
})
}
} else {
if configuration.TLSConfig != nil {
socket, err = thrift.NewTSSLSocketConf(addr, &thrift.TConfiguration{
ConnectTimeout: configuration.Timeout,
SocketTimeout: configuration.Timeout,
ConnectTimeout: configuration.ConnectTimeout,
SocketTimeout: configuration.SocketTimeout,
TLSConfig: configuration.TLSConfig,
})
} else {
socket, err = thrift.NewTSocketConf(addr, &thrift.TConfiguration{
ConnectTimeout: configuration.Timeout,
SocketTimeout: configuration.Timeout,
ConnectTimeout: configuration.ConnectTimeout,
SocketTimeout: configuration.SocketTimeout,
})
}
if err != nil {
Expand Down Expand Up @@ -366,7 +368,7 @@ func innerConnect(ctx context.Context, host string, port int, auth string,
func getHTTPClient(configuration *ConnectConfiguration) (httpClient *http.Client, protocol string, err error) {
if configuration.TLSConfig != nil {
httpClient = &http.Client{
Timeout: configuration.Timeout,
Timeout: configuration.HttpReadTimeout,
Transport: &http.Transport{
TLSClientConfig: configuration.TLSConfig,
DialContext: configuration.DialContext,
Expand All @@ -375,7 +377,7 @@ func getHTTPClient(configuration *ConnectConfiguration) (httpClient *http.Client
protocol = "https"
} else {
httpClient = &http.Client{
Timeout: configuration.Timeout,
Timeout: configuration.HttpReadTimeout,
Transport: &http.Transport{
DialContext: configuration.DialContext,
},
Expand Down
27 changes: 14 additions & 13 deletions hive_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build all || integration
// +build all integration

package gohive
Expand Down Expand Up @@ -585,15 +586,15 @@ func NoopDialContext(ctx context.Context, network string, addr string) (net.Conn
}

func sleepContext(ctx context.Context, delay time.Duration) {
select {
case <-ctx.Done():
case <-time.After(delay):
}
select {
case <-ctx.Done():
case <-time.After(delay):
}
}

func SleepDialContext(ctx context.Context, network string, addr string) (net.Conn, error) {
var d net.Dialer
sleepContext(ctx, 10 * time.Second)
sleepContext(ctx, 10*time.Second)
return d.DialContext(ctx, network, addr)
}

Expand Down Expand Up @@ -627,7 +628,7 @@ func TestSimpleSelectWithDialFunctionAndTimeout(t *testing.T) {
configuration.TransportMode = getTransport()
configuration.Service = "hive"
configuration.FetchSize = 1000
configuration.Timeout = time.Hour
configuration.ConnectTimeout = time.Hour

connection, cursor := makeConnectionWithConnectConfiguration(t, configuration)
createTable(t, cursor)
Expand All @@ -648,7 +649,7 @@ func TestSimpleSelectWithDialFunctionAndTimeout(t *testing.T) {

func TestSimpleSelectWithTimeout(t *testing.T) {
configuration := NewConnectConfiguration()
configuration.Timeout = time.Hour
configuration.ConnectTimeout = time.Hour
configuration.TransportMode = getTransport()
configuration.Service = "hive"
configuration.FetchSize = 1000
Expand Down Expand Up @@ -676,7 +677,7 @@ func TestConnectTimeoutWithDialFn(t *testing.T) {
configuration := NewConnectConfiguration()
configuration.Service = "hive"
configuration.TransportMode = mode
configuration.Timeout = 3 * time.Second
configuration.ConnectTimeout = 3 * time.Second
configuration.DialContext = SleepDialContext

if ssl {
Expand All @@ -701,10 +702,10 @@ func TestConnectTimeoutWithDialFn(t *testing.T) {
if !strings.Contains(errConn.Error(), "timeout") {
t.Fatalf("Expected a timeout error, but received: %+v", errConn)
}
if elapsed <= 2 * time.Second {
if elapsed <= 2*time.Second {
t.Fatalf("Timed out too fast: %v", elapsed)
}
if elapsed >= 4 * time.Second {
if elapsed >= 4*time.Second {
t.Fatalf("Timed out too slow: %v", elapsed)
}
}
Expand All @@ -715,7 +716,7 @@ func TestConnectTimeout(t *testing.T) {
configuration := NewConnectConfiguration()
configuration.Service = "hive"
configuration.TransportMode = mode
configuration.Timeout = 3 * time.Second
configuration.ConnectTimeout = 3 * time.Second

if ssl {
tlsConfig, err := getTlsConfiguration("client.cer.pem", "client.cer.key")
Expand All @@ -739,10 +740,10 @@ func TestConnectTimeout(t *testing.T) {
if !strings.Contains(errConn.Error(), "timeout") {
t.Fatalf("Expected a timeout error, but received: %+v", errConn)
}
if elapsed <= 2 * time.Second {
if elapsed <= 2*time.Second {
t.Fatalf("Timed out too fast: %v", elapsed)
}
if elapsed >= 4 * time.Second {
if elapsed >= 4*time.Second {
t.Fatalf("Timed out too slow: %v", elapsed)
}
}
Expand Down

0 comments on commit d164d8c

Please sign in to comment.