github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/webhook/notifier.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package webhook
    16  
    17  import (
    18  	"context"
    19  	"sync"
    20  
    21  	"github.com/livekit/protocol/livekit"
    22  	"github.com/livekit/protocol/logger"
    23  )
    24  
    25  type QueuedNotifier interface {
    26  	QueueNotify(ctx context.Context, event *livekit.WebhookEvent) error
    27  }
    28  
    29  type DefaultNotifier struct {
    30  	urlNotifiers []*URLNotifier
    31  }
    32  
    33  func NewDefaultNotifier(apiKey, apiSecret string, urls []string) QueuedNotifier {
    34  	n := &DefaultNotifier{}
    35  	for _, url := range urls {
    36  		u := NewURLNotifier(URLNotifierParams{
    37  			URL:       url,
    38  			Logger:    logger.GetLogger().WithComponent("webhook"),
    39  			APIKey:    apiKey,
    40  			APISecret: apiSecret,
    41  		})
    42  		n.urlNotifiers = append(n.urlNotifiers, u)
    43  	}
    44  	return n
    45  }
    46  
    47  func (n *DefaultNotifier) Stop(force bool) {
    48  	wg := sync.WaitGroup{}
    49  	for _, u := range n.urlNotifiers {
    50  		wg.Add(1)
    51  		go func(u *URLNotifier) {
    52  			defer wg.Done()
    53  			u.Stop(force)
    54  		}(u)
    55  	}
    56  	wg.Wait()
    57  }
    58  
    59  func (n *DefaultNotifier) QueueNotify(_ context.Context, event *livekit.WebhookEvent) error {
    60  	for _, u := range n.urlNotifiers {
    61  		if err := u.QueueNotify(event); err != nil {
    62  			return err
    63  		}
    64  	}
    65  	return nil
    66  }