Skip to content

Commit 9b34314

Browse files
Make ResourceURLNotifier configurable. (#1004)
* Make ResourceURLNotifier configurable. Also change default max age to 10s. * generated protobuf --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 39426df commit 9b34314

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

webhook/resource_url_notifier.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,19 @@ import (
3535

3636
const (
3737
defaultTimeout = 5 * time.Minute
38-
defaultMaxAge = 5 * time.Second
38+
defaultMaxAge = 10 * time.Second
3939
defaultMaxDepth = 200
4040
)
4141

4242
var (
4343
errClosed = errors.New("notifier is closed")
4444
)
4545

46+
type ResourceURLNotifierConfig struct {
47+
MaxAge time.Duration
48+
MaxDepth int
49+
}
50+
4651
type poster interface {
4752
Process(ctx context.Context, queuedAt time.Time, event *livekit.WebhookEvent)
4853
}
@@ -57,8 +62,7 @@ type ResourceURLNotifierParams struct {
5762
HTTPClientParams
5863
Logger logger.Logger
5964
Timeout time.Duration
60-
MaxAge time.Duration
61-
MaxDepth int
65+
Config ResourceURLNotifierConfig
6266
URL string
6367
APIKey string
6468
APISecret string
@@ -93,11 +97,11 @@ func NewResourceURLNotifier(params ResourceURLNotifierParams) *ResourceURLNotifi
9397
if params.Timeout == 0 {
9498
params.Timeout = defaultTimeout
9599
}
96-
if params.MaxAge == 0 {
97-
params.MaxAge = defaultMaxAge
100+
if params.Config.MaxAge == 0 {
101+
params.Config.MaxAge = defaultMaxAge
98102
}
99-
if params.MaxDepth == 0 {
100-
params.MaxDepth = defaultMaxDepth
103+
if params.Config.MaxDepth == 0 {
104+
params.Config.MaxDepth = defaultMaxDepth
101105
}
102106

103107
rhc := retryablehttp.NewClient()
@@ -167,7 +171,7 @@ func (r *ResourceURLNotifier) QueueNotify(ctx context.Context, event *livekit.We
167171
rqi := r.resourceQueues[key]
168172
if rqi == nil || !r.resourceQueueTimeoutQueue.Reset(rqi.tqi) {
169173
rq := newResourceQueue(resourceQueueParams{
170-
MaxDepth: r.params.MaxDepth,
174+
MaxDepth: r.params.Config.MaxDepth,
171175
Poster: r,
172176
})
173177
rqi = &resourceQueueInfo{resourceQueue: rq, key: key}
@@ -223,7 +227,7 @@ func (r *ResourceURLNotifier) Process(ctx context.Context, queuedAt time.Time, e
223227
queueDuration := time.Since(queuedAt)
224228
fields = append(fields, "queueDuration", queueDuration)
225229

226-
if queueDuration > r.params.MaxAge {
230+
if queueDuration > r.params.Config.MaxAge {
227231
fields = append(fields, "reason", "age")
228232
r.params.Logger.Infow("dropped webhook", fields...)
229233

webhook/webhook_test.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,10 @@ func TestResourceURLNotifierLifecycle(t *testing.T) {
601601
URL: testUrl,
602602
APIKey: apiKey,
603603
APISecret: apiSecret,
604-
MaxAge: 200 * time.Millisecond,
605-
MaxDepth: 50,
604+
Config: ResourceURLNotifierConfig{
605+
MaxAge: 200 * time.Millisecond,
606+
MaxDepth: 50,
607+
},
606608
HTTPClientParams: HTTPClientParams{
607609
RetryWaitMax: time.Millisecond,
608610
MaxRetries: 1,
@@ -636,8 +638,10 @@ func TestResourceURLNotifierLifecycle(t *testing.T) {
636638
URL: "http://localhost:9987",
637639
APIKey: apiKey,
638640
APISecret: apiSecret,
639-
MaxAge: 200 * time.Millisecond,
640-
MaxDepth: 50,
641+
Config: ResourceURLNotifierConfig{
642+
MaxAge: 200 * time.Millisecond,
643+
MaxDepth: 50,
644+
},
641645
HTTPClientParams: HTTPClientParams{
642646
RetryWaitMax: time.Millisecond,
643647
MaxRetries: 1,
@@ -660,11 +664,13 @@ func TestResourceURLNotifierFilter(t *testing.T) {
660664

661665
t.Run("none", func(t *testing.T) {
662666
resourceURLNotifier := NewResourceURLNotifier(ResourceURLNotifierParams{
663-
URL: testUrl,
664-
APIKey: apiKey,
665-
APISecret: apiSecret,
666-
MaxAge: 200 * time.Millisecond,
667-
MaxDepth: 50,
667+
URL: testUrl,
668+
APIKey: apiKey,
669+
APISecret: apiSecret,
670+
Config: ResourceURLNotifierConfig{
671+
MaxAge: 200 * time.Millisecond,
672+
MaxDepth: 50,
673+
},
668674
FilterParams: FilterParams{},
669675
})
670676
defer resourceURLNotifier.Stop(false)
@@ -691,8 +697,10 @@ func TestResourceURLNotifierFilter(t *testing.T) {
691697
URL: testUrl,
692698
APIKey: apiKey,
693699
APISecret: apiSecret,
694-
MaxAge: 200 * time.Millisecond,
695-
MaxDepth: 50,
700+
Config: ResourceURLNotifierConfig{
701+
MaxAge: 200 * time.Millisecond,
702+
MaxDepth: 50,
703+
},
696704
FilterParams: FilterParams{
697705
IncludeEvents: []string{EventRoomStarted},
698706
},
@@ -721,8 +729,10 @@ func TestResourceURLNotifierFilter(t *testing.T) {
721729
URL: testUrl,
722730
APIKey: apiKey,
723731
APISecret: apiSecret,
724-
MaxAge: 200 * time.Millisecond,
725-
MaxDepth: 50,
732+
Config: ResourceURLNotifierConfig{
733+
MaxAge: 200 * time.Millisecond,
734+
MaxDepth: 50,
735+
},
726736
FilterParams: FilterParams{
727737
ExcludeEvents: []string{EventRoomStarted},
728738
},
@@ -751,8 +761,10 @@ func TestResourceURLNotifierFilter(t *testing.T) {
751761
URL: testUrl,
752762
APIKey: apiKey,
753763
APISecret: apiSecret,
754-
MaxAge: 200 * time.Millisecond,
755-
MaxDepth: 50,
764+
Config: ResourceURLNotifierConfig{
765+
MaxAge: 200 * time.Millisecond,
766+
MaxDepth: 50,
767+
},
756768
FilterParams: FilterParams{
757769
IncludeEvents: []string{EventRoomStarted},
758770
ExcludeEvents: []string{EventRoomStarted, EventRoomFinished},
@@ -785,8 +797,10 @@ func newTestResourceNotifier(timeout time.Duration, maxAge time.Duration, maxDep
785797
APIKey: apiKey,
786798
APISecret: apiSecret,
787799
Timeout: timeout,
788-
MaxAge: maxAge,
789-
MaxDepth: maxDepth,
800+
Config: ResourceURLNotifierConfig{
801+
MaxAge: maxAge,
802+
MaxDepth: maxDepth,
803+
},
790804
})
791805
}
792806

0 commit comments

Comments
 (0)