Skip to content

Commit

Permalink
add timeout for linodego http client
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulait committed Jun 18, 2024
1 parent 71e7855 commit 8043390
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
21 changes: 19 additions & 2 deletions cloud/linode/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ package client

import (
"context"
"net/http"
"time"

"github.com/linode/linodego"
"golang.org/x/oauth2"
"k8s.io/klog/v2"
)

const (
// DefaultClientTimeout is the default timeout for a client Linode API call
DefaultClientTimeout = 120 * time.Second
)

type Client interface {
Expand Down Expand Up @@ -48,14 +57,22 @@ type Client interface {
var _ Client = (*linodego.Client)(nil)

// New creates a new linode client with a given token, userAgent, and API URL
func New(token, userAgent, apiURL string) (*linodego.Client, error) {
linodeClient := linodego.NewClient(nil)
func New(token, userAgent, apiURL string, timeout time.Duration) (*linodego.Client, error) {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
oauth2Client := &http.Client{
Transport: &oauth2.Transport{
Source: tokenSource,
},
Timeout: timeout,
}
linodeClient := linodego.NewClient(oauth2Client)
client, err := linodeClient.UseURL(apiURL)
if err != nil {
return nil, err
}
client.SetUserAgent(userAgent)
client.SetToken(token)

klog.V(3).Infof("Linode client created with default timeout of %v seconds", timeout)
return client, nil
}
12 changes: 11 additions & 1 deletion cloud/linode/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"io"
"os"
"strconv"
"sync"
"time"

"github.com/linode/linodego"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -98,7 +100,15 @@ func newCloud() (cloudprovider.Interface, error) {
url := os.Getenv(urlEnv)
ua := fmt.Sprintf("linode-cloud-controller-manager %s", linodego.DefaultUserAgent)

linodeClient, err := client.New(apiToken, ua, url)
// set timeout used by linodeclient for API calls
timeout := client.DefaultClientTimeout
if raw, ok := os.LookupEnv("LINODE_REQUEST_TIMEOUT_SECONDS"); ok {
if t, _ := strconv.Atoi(raw); t > 0 {
timeout = time.Duration(t) * time.Second
}
}

linodeClient, err := client.New(apiToken, ua, url, timeout)
if err != nil {
return nil, fmt.Errorf("client was not created succesfully: %w", err)
}
Expand Down

0 comments on commit 8043390

Please sign in to comment.