Skip to content

Commit

Permalink
internal/output/webhook: allow user-configured timeout (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
efd6 authored May 13, 2024
1 parent 2969e28 commit eea089c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changelog/97.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
webhook output: Allow user-configured timeouts.
```
1 change: 1 addition & 0 deletions internal/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 5 additions & 4 deletions internal/output/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions internal/output/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/http"
"net/url"
"strings"
"time"

"github.com/elastic/stream/internal/output"
)
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions internal/output/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -57,6 +58,7 @@ func TestWebhook(t *testing.T) {
},
Username: username,
Password: password,
Timeout: time.Second,
},
})
require.NoError(t, err)
Expand Down

0 comments on commit eea089c

Please sign in to comment.