Skip to content

Commit

Permalink
Set timeout for http.Client
Browse files Browse the repository at this point in the history
Used a default of 100 seconds for the timeout.  This is the same value
as what c# uses by default. I didn't find any other good default.

Make annotation_tasks_install.go use the common function instead of
defining it's own.

Fixes #1514

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
  • Loading branch information
chmouel committed Dec 11, 2023
1 parent 18bb994 commit 823ebbb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
10 changes: 1 addition & 9 deletions pkg/matcher/annotation_tasks_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -106,16 +104,10 @@ func (rt RemoteTasks) getRemote(ctx context.Context, uri string, fromHub bool) (

switch {
case strings.HasPrefix(uri, "https://"), strings.HasPrefix(uri, "http://"): // if it starts with http(s)://, it is a remote resource
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
res, err := rt.Run.Clients.HTTP.Do(req)
data, err := rt.Run.Clients.GetURL(ctx, uri)
if err != nil {
return "", err
}
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("cannot get remote resource: %s: %s", uri, res.Status)
}
data, _ := io.ReadAll(res.Body)
defer res.Body.Close()
rt.Logger.Infof("successfully fetched %s from remote https url", uri)
return string(data), nil
case fromHub && strings.Contains(uri, "://"): // if it contains ://, it is a remote custom catalog
Expand Down
22 changes: 21 additions & 1 deletion pkg/params/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"fmt"
"io"
"net"
"net/http"
"time"

"github.com/openshift-pipelines/pipelines-as-code/pkg/consoleui"
"github.com/openshift-pipelines/pipelines-as-code/pkg/generated/clientset/versioned"
Expand All @@ -18,6 +20,13 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

const (
// most programming languages do not have a timeout, but c# does a default
// of 100 seconds so using that value.
ConnectMaxWaitTime = 100 * time.Second
RequestMaxWaitTime = 100 * time.Second
)

type Clients struct {
ClientInitialized bool
PipelineAsCode versioned.Interface
Expand All @@ -30,7 +39,10 @@ type Clients struct {
}

func (c *Clients) GetURL(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
nctx, cancel := context.WithTimeout(ctx, RequestMaxWaitTime)
defer cancel()

req, err := http.NewRequestWithContext(nctx, http.MethodGet, url, nil)
if err != nil {
return []byte{}, err
}
Expand Down Expand Up @@ -125,6 +137,14 @@ func (c *Clients) NewClients(ctx context.Context, info *info.Info) error {
}()
c.Log = logger

c.HTTP = http.Client{
Timeout: RequestMaxWaitTime,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: ConnectMaxWaitTime,
}).DialContext,
},
}
config, err := c.kubeConfig(info)
if err != nil {
return err
Expand Down

0 comments on commit 823ebbb

Please sign in to comment.