github.com/whoyao/protocol@v0.0.0-20230519045905-2d8ace718ca5/webhook/notifier.go (about)

     1  package webhook
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  
     7  	"github.com/whoyao/protocol/livekit"
     8  	"github.com/whoyao/protocol/logger"
     9  )
    10  
    11  type QueuedNotifier interface {
    12  	QueueNotify(ctx context.Context, event *livekit.WebhookEvent) error
    13  }
    14  
    15  type DefaultNotifier struct {
    16  	urlNotifiers []*URLNotifier
    17  }
    18  
    19  func NewDefaultNotifier(apiKey, apiSecret string, urls []string) QueuedNotifier {
    20  	n := &DefaultNotifier{}
    21  	for _, url := range urls {
    22  		u := NewURLNotifier(URLNotifierParams{
    23  			URL:       url,
    24  			Logger:    logger.GetLogger(),
    25  			APIKey:    apiKey,
    26  			APISecret: apiSecret,
    27  		})
    28  		n.urlNotifiers = append(n.urlNotifiers, u)
    29  	}
    30  	return n
    31  }
    32  
    33  func (n *DefaultNotifier) Stop(force bool) {
    34  	wg := sync.WaitGroup{}
    35  	for _, u := range n.urlNotifiers {
    36  		wg.Add(1)
    37  		go func(u *URLNotifier) {
    38  			defer wg.Done()
    39  			u.Stop(force)
    40  		}(u)
    41  	}
    42  	wg.Wait()
    43  }
    44  
    45  func (n *DefaultNotifier) QueueNotify(_ context.Context, event *livekit.WebhookEvent) error {
    46  	for _, u := range n.urlNotifiers {
    47  		if err := u.QueueNotify(event); err != nil {
    48  			return err
    49  		}
    50  	}
    51  	return nil
    52  }