From 4892a766a88a6dab6c1fd13263e7d6de05c15911 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 30 Oct 2022 16:45:53 -0700 Subject: [PATCH] do not panic if webhook returns an error (#15970) --- internal/event/target/webhook.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/event/target/webhook.go b/internal/event/target/webhook.go index c5108d9d1..88c94047e 100644 --- a/internal/event/target/webhook.go +++ b/internal/event/target/webhook.go @@ -98,7 +98,8 @@ type WebhookTarget struct { httpClient *http.Client store Store loggerOnce logger.LogOnce - quitCh chan struct{} + cancel context.CancelFunc + cancelCh <-chan struct{} } // ID - returns target ID. @@ -199,14 +200,12 @@ func (target *WebhookTarget) send(eventData event.Event) error { resp, err := target.httpClient.Do(req) if err != nil { - target.Close() return err } defer resp.Body.Close() io.Copy(io.Discard, resp.Body) if resp.StatusCode < 200 || resp.StatusCode > 299 { - target.Close() return fmt.Errorf("sending event failed with %v", resp.Status) } @@ -242,7 +241,7 @@ func (target *WebhookTarget) Send(eventKey string) error { // Close - does nothing and available for interface compatibility. func (target *WebhookTarget) Close() error { - close(target.quitCh) + target.cancel() return nil } @@ -274,18 +273,21 @@ func (target *WebhookTarget) initWebhook() error { } if target.store != nil { - streamEventsFromStore(target.store, target, target.quitCh, target.loggerOnce) + streamEventsFromStore(target.store, target, target.cancelCh, target.loggerOnce) } return nil } // NewWebhookTarget - creates new Webhook target. func NewWebhookTarget(ctx context.Context, id string, args WebhookArgs, loggerOnce logger.LogOnce, transport *http.Transport) (*WebhookTarget, error) { + ctx, cancel := context.WithCancel(ctx) + var store Store if args.QueueDir != "" { queueDir := filepath.Join(args.QueueDir, storePrefix+"-webhook-"+id) store = NewQueueStore(queueDir, args.QueueLimit) if err := store.Open(); err != nil { + cancel() return nil, fmt.Errorf("unable to initialize the queue store of Webhook `%s`: %w", id, err) } } @@ -296,6 +298,7 @@ func NewWebhookTarget(ctx context.Context, id string, args WebhookArgs, loggerOn loggerOnce: loggerOnce, transport: transport, store: store, - quitCh: make(chan struct{}), + cancel: cancel, + cancelCh: ctx.Done(), }, nil }