From eea089c80588b41e0bb4e01ef30c74f7b22a7c09 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Mon, 13 May 2024 10:08:35 +0930 Subject: [PATCH] internal/output/webhook: allow user-configured timeout (#97) --- .changelog/97.txt | 3 +++ internal/command/root.go | 1 + internal/output/options.go | 9 +++++---- internal/output/webhook/webhook.go | 6 ++++-- internal/output/webhook/webhook_test.go | 2 ++ 5 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 .changelog/97.txt diff --git a/.changelog/97.txt b/.changelog/97.txt new file mode 100644 index 0000000..939c3e4 --- /dev/null +++ b/.changelog/97.txt @@ -0,0 +1,3 @@ +```release-note:bug +webhook output: Allow user-configured timeouts. +``` diff --git a/internal/command/root.go b/internal/command/root.go index 635be32..a876b4b 100644 --- a/internal/command/root.go +++ b/internal/command/root.go @@ -74,6 +74,7 @@ func ExecuteContext(ctx context.Context) error { rootCmd.PersistentFlags().StringArrayVar(&opts.WebhookOptions.Headers, "webhook-header", nil, "webhook header to add to request (e.g. Header=Value)") rootCmd.PersistentFlags().StringVar(&opts.WebhookOptions.Password, "webhook-password", "", "webhook password for basic authentication") rootCmd.PersistentFlags().StringVar(&opts.WebhookOptions.Username, "webhook-username", "", "webhook username for basic authentication") + rootCmd.PersistentFlags().DurationVar(&opts.WebhookOptions.Timeout, "webhook-timeout", time.Second, "webhook request timeout (zero is no timeout)") // GCP Pubsub output flags. rootCmd.PersistentFlags().StringVar(&opts.GCPPubsubOptions.Project, "gcppubsub-project", "test", "GCP Pubsub project name") diff --git a/internal/output/options.go b/internal/output/options.go index efb9e4c..4018262 100644 --- a/internal/output/options.go +++ b/internal/output/options.go @@ -26,10 +26,11 @@ type Options struct { } type WebhookOptions struct { - ContentType string // Content-Type header. - Headers []string // Headers in Key=Value format. - Username string // Basic auth username. - Password string // Basic auth password. + ContentType string // Content-Type header. + Headers []string // Headers in Key=Value format. + Username string // Basic auth username. + Password string // Basic auth password. + Timeout time.Duration // Timeout for request handling. } type GCPPubsubOptions struct { diff --git a/internal/output/webhook/webhook.go b/internal/output/webhook/webhook.go index 4647aef..4495869 100644 --- a/internal/output/webhook/webhook.go +++ b/internal/output/webhook/webhook.go @@ -12,7 +12,6 @@ import ( "net/http" "net/url" "strings" - "time" "github.com/elastic/stream/internal/output" ) @@ -31,8 +30,11 @@ func New(opts *output.Options) (output.Output, error) { return nil, fmt.Errorf("address must be a valid URL for webhook output: %w", err) } + if opts.Timeout < 0 { + return nil, fmt.Errorf("timeout must not be negative: %v", opts.Timeout) + } client := &http.Client{ - Timeout: time.Second, + Timeout: opts.Timeout, Transport: &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: opts.InsecureTLS, diff --git a/internal/output/webhook/webhook_test.go b/internal/output/webhook/webhook_test.go index 5e7e26f..00bd843 100644 --- a/internal/output/webhook/webhook_test.go +++ b/internal/output/webhook/webhook_test.go @@ -11,6 +11,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -57,6 +58,7 @@ func TestWebhook(t *testing.T) { }, Username: username, Password: password, + Timeout: time.Second, }, }) require.NoError(t, err)