Skip to content

Commit

Permalink
fix: timeouts in http clients
Browse files Browse the repository at this point in the history
  • Loading branch information
exu committed Apr 22, 2022
1 parent 3ca01a3 commit 2fbdafb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
7 changes: 4 additions & 3 deletions cmd/kubectl-testkube/commands/dashboard.go
Expand Up @@ -2,13 +2,13 @@ package commands

import (
"fmt"
"net/http"
"os"
"os/exec"
"os/signal"
"runtime"
"time"

"github.com/kubeshop/testkube/pkg/http"
"github.com/kubeshop/testkube/pkg/process"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -97,6 +97,7 @@ func NewDashboardCmd() *cobra.Command {

func readinessCheck(apiURI, dashboardURI string) (bool, error) {
const readinessCheckTimeout = 30 * time.Second
client := http.NewClient()

ticker := time.NewTicker(500 * time.Millisecond)
go func() {
Expand All @@ -105,11 +106,11 @@ func readinessCheck(apiURI, dashboardURI string) (bool, error) {
}()

for range ticker.C {
apiResp, err := http.Get(apiURI + "/info")
apiResp, err := client.Get(apiURI + "/info")
if err != nil {
continue
}
dashboardResp, err := http.Get(dashboardURI)
dashboardResp, err := client.Get(dashboardURI)
if err != nil {
continue
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/executor/content/fetcher.go
Expand Up @@ -4,14 +4,14 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
"github.com/kubeshop/testkube/pkg/git"
"github.com/kubeshop/testkube/pkg/http"
)

// NewFetcher returns new file/dir fetcher based on given directory path
Expand Down Expand Up @@ -50,7 +50,8 @@ func (f Fetcher) FetchString(str string) (path string, err error) {

//FetchURI stores uri as local file
func (f Fetcher) FetchURI(uri string) (path string, err error) {
resp, err := http.Get(uri)
client := http.NewClient()
resp, err := client.Get(uri)
if err != nil {
return path, err
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/http/client.go
@@ -0,0 +1,26 @@
package http

import (
"net"
"net/http"
"time"
)

const (
NetDialTimeout = 5 * time.Second
TLSHandshakeTimeout = 5 * time.Second
ClientTimeout = 10 * time.Second
)

func NewClient() *http.Client {
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: NetDialTimeout,
}).Dial,
TLSHandshakeTimeout: TLSHandshakeTimeout,
}
return &http.Client{
Timeout: ClientTimeout,
Transport: netTransport,
}
}
21 changes: 21 additions & 0 deletions pkg/http/client_test.go
@@ -0,0 +1,21 @@
package http

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewClient(t *testing.T) {

t.Run("returns new client instance with configured timeouts correctly", func(t *testing.T) {
// given / when
c := NewClient()

// then
assert.Equal(t, ClientTimeout, c.Timeout)
assert.Equal(t, TLSHandshakeTimeout, c.Transport.(*http.Transport).TLSHandshakeTimeout)
})

}

0 comments on commit 2fbdafb

Please sign in to comment.