diff --git a/internal/slurm/client.go b/internal/slurm/client.go index bc5c996..d68ded5 100644 --- a/internal/slurm/client.go +++ b/internal/slurm/client.go @@ -9,6 +9,7 @@ import ( "net/http" "net/url" "strings" + "time" ) const ( @@ -16,6 +17,8 @@ const ( DefaultBaseURL = "http://localhost:6820/" // DefaultUserAgent is the default User-Agent header value. DefaultUserAgent = "slurm-go-sdk" + // DefaultTimeout is the default HTTP request timeout. + DefaultTimeout = 30 * time.Second ) // Client manages communication with the Slurm REST API. @@ -85,7 +88,7 @@ type Response struct { // http.DefaultClient is used. func NewClient(baseURL string, httpClient *http.Client) (*Client, error) { if httpClient == nil { - httpClient = http.DefaultClient + httpClient = &http.Client{Timeout: DefaultTimeout} } parsedURL, err := url.Parse(baseURL) diff --git a/internal/slurm/client_test.go b/internal/slurm/client_test.go index 02efe33..18f0725 100644 --- a/internal/slurm/client_test.go +++ b/internal/slurm/client_test.go @@ -20,7 +20,7 @@ func TestNewClient(t *testing.T) { t.Errorf("expected UserAgent %q, got %q", DefaultUserAgent, client.UserAgent) } if client.client == nil { - t.Error("expected http.Client to be initialized (nil httpClient should default to http.DefaultClient)") + t.Error("expected http.Client to be initialized (nil httpClient should create a client with default timeout)") } _, err = NewClient("://invalid", nil) diff --git a/internal/slurm/options.go b/internal/slurm/options.go index e926d2c..e0b4b36 100644 --- a/internal/slurm/options.go +++ b/internal/slurm/options.go @@ -24,6 +24,8 @@ func defaultClientConfig() *clientConfig { } } +const defaultHTTPTimeout = 30 * time.Second + // WithJWTKey specifies the path to the JWT key file. func WithJWTKey(path string) ClientOption { return func(c *clientConfig) error { @@ -89,11 +91,12 @@ func NewClientWithOpts(baseURL string, opts ...ClientOption) (*Client, error) { } tr := NewJWTAuthTransport(cfg.username, key, transportOpts...) - httpClient = tr.Client() + httpClient = &http.Client{ + Transport: tr, + Timeout: defaultHTTPTimeout, + } } else if cfg.httpClient != nil { httpClient = cfg.httpClient - } else { - httpClient = http.DefaultClient } return NewClient(baseURL, httpClient) diff --git a/internal/slurm/options_test.go b/internal/slurm/options_test.go index e9d5cc7..2229f54 100644 --- a/internal/slurm/options_test.go +++ b/internal/slurm/options_test.go @@ -2,7 +2,6 @@ package slurm import ( "crypto/rand" - "net/http" "os" "path/filepath" "testing" @@ -61,8 +60,8 @@ func TestNewClientWithOpts_BackwardCompatible(t *testing.T) { if client == nil { t.Fatal("expected non-nil client") } - if client.client != http.DefaultClient { - t.Error("expected http.DefaultClient when no options provided") + if client.client.Timeout != DefaultTimeout { + t.Errorf("expected Timeout=%v, got %v", DefaultTimeout, client.client.Timeout) } }