Skip to content

Commit 56615aa

Browse files
Move WebHookConfig to use location (#1006)
* Move WebHookConfig to use location * generate * generated protobuf * make URLNotifier config --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent d04fdea commit 56615aa

File tree

3 files changed

+90
-46
lines changed

3 files changed

+90
-46
lines changed

webhook/notifier.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ import (
2424
"google.golang.org/protobuf/types/known/timestamppb"
2525
)
2626

27+
type WebHookConfig struct {
28+
URLs []string `yaml:"urls,omitempty"`
29+
APIKey string `yaml:"api_key,omitempty"`
30+
URLNotifier URLNotifierConfig `yaml:"url_notifier,omitempty"`
31+
ResourceURLNotifier ResourceURLNotifierConfig `yaml:"resource_url_notifier,omitempty"`
32+
}
33+
34+
var DefaultWebHookConfig = WebHookConfig{
35+
URLNotifier: DefaultURLNotifierConfig,
36+
ResourceURLNotifier: DefaultResourceURLNotifierConfig,
37+
}
38+
2739
type QueuedNotifier interface {
2840
RegisterProcessedHook(f func(ctx context.Context, whi *livekit.WebhookInfo))
2941
SetKeys(apiKey, apiSecret string)
@@ -36,14 +48,15 @@ type DefaultNotifier struct {
3648
notifiers []QueuedNotifier
3749
}
3850

39-
func NewDefaultNotifier(apiKey, apiSecret string, urls []string) QueuedNotifier {
51+
func NewDefaultNotifier(config WebHookConfig, apiSecret string) QueuedNotifier {
4052
n := &DefaultNotifier{}
41-
for _, url := range urls {
53+
for _, url := range config.URLs {
4254
u := NewResourceURLNotifier(ResourceURLNotifierParams{
4355
URL: url,
4456
Logger: logger.GetLogger().WithComponent("webhook"),
45-
APIKey: apiKey,
57+
APIKey: config.APIKey,
4658
APISecret: apiSecret,
59+
Config: config.ResourceURLNotifier,
4760
})
4861
n.notifiers = append(n.notifiers, u)
4962
}

webhook/url_notifier.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,20 @@ const (
3737
defaultQueueSize = 100
3838
)
3939

40+
type URLNotifierConfig struct {
41+
NumWorkers int
42+
QueueSize int
43+
}
44+
45+
var DefaultURLNotifierConfig = URLNotifierConfig{
46+
NumWorkers: 10,
47+
QueueSize: 100,
48+
}
49+
4050
type URLNotifierParams struct {
4151
HTTPClientParams
4252
Logger logger.Logger
43-
QueueSize int
53+
Config URLNotifierConfig
4454
URL string
4555
APIKey string
4656
APISecret string
@@ -61,8 +71,11 @@ type URLNotifier struct {
6171
}
6272

6373
func NewURLNotifier(params URLNotifierParams) *URLNotifier {
64-
if params.QueueSize == 0 {
65-
params.QueueSize = defaultQueueSize
74+
if params.Config.NumWorkers == 0 {
75+
params.Config.NumWorkers = DefaultURLNotifierConfig.NumWorkers
76+
}
77+
if params.Config.QueueSize == 0 {
78+
params.Config.QueueSize = DefaultURLNotifierConfig.QueueSize
6679
}
6780
if params.Logger == nil {
6881
params.Logger = logger.GetLogger()
@@ -88,8 +101,8 @@ func NewURLNotifier(params URLNotifierParams) *URLNotifier {
88101
}
89102
n.client.Logger = &logAdapter{}
90103

91-
n.pool = core.NewQueuePool(numWorkers, core.QueueWorkerParams{
92-
QueueSize: params.QueueSize,
104+
n.pool = core.NewQueuePool(params.Config.NumWorkers, core.QueueWorkerParams{
105+
QueueSize: params.Config.QueueSize,
93106
DropWhenFull: true,
94107
})
95108
return n

webhook/webhook_test.go

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ import (
3131
)
3232

3333
const (
34-
apiKey = "mykey"
35-
apiSecret = "mysecret"
34+
testAPIKey = "mykey"
35+
testAPISecret = "mysecret"
3636
testAddr = ":8765"
3737
testUrl = "http://localhost:8765"
3838
webhookCheckInterval = 100 * time.Millisecond
3939
)
4040

4141
var authProvider = auth.NewSimpleKeyProvider(
42-
apiKey, apiSecret,
42+
testAPIKey, testAPISecret,
4343
)
4444

4545
func TestWebHook(t *testing.T) {
@@ -146,15 +146,17 @@ func TestURLNotifierLifecycle(t *testing.T) {
146146

147147
t.Run("times out after accepting connection", func(t *testing.T) {
148148
urlNotifier := NewURLNotifier(URLNotifierParams{
149-
QueueSize: 20,
150149
URL: testUrl,
151-
APIKey: apiKey,
152-
APISecret: apiSecret,
150+
APIKey: testAPIKey,
151+
APISecret: testAPISecret,
153152
HTTPClientParams: HTTPClientParams{
154153
RetryWaitMax: time.Millisecond,
155154
MaxRetries: 1,
156155
ClientTimeout: 100 * time.Millisecond,
157156
},
157+
Config: URLNotifierConfig{
158+
QueueSize: 20,
159+
},
158160
})
159161

160162
numCalled := atomic.Int32{}
@@ -181,8 +183,8 @@ func TestURLNotifierLifecycle(t *testing.T) {
181183
defer ln.Close()
182184
urlNotifier := NewURLNotifier(URLNotifierParams{
183185
URL: "http://localhost:9987",
184-
APIKey: apiKey,
185-
APISecret: apiSecret,
186+
APIKey: testAPIKey,
187+
APISecret: testAPISecret,
186188
HTTPClientParams: HTTPClientParams{
187189
RetryWaitMax: time.Millisecond,
188190
MaxRetries: 1,
@@ -205,10 +207,12 @@ func TestURLNotifierFilter(t *testing.T) {
205207

206208
t.Run("none", func(t *testing.T) {
207209
urlNotifier := NewURLNotifier(URLNotifierParams{
208-
QueueSize: 20,
209210
URL: testUrl,
210-
APIKey: apiKey,
211-
APISecret: apiSecret,
211+
APIKey: testAPIKey,
212+
APISecret: testAPISecret,
213+
Config: URLNotifierConfig{
214+
QueueSize: 20,
215+
},
212216
})
213217
defer urlNotifier.Stop(false)
214218

@@ -231,13 +235,15 @@ func TestURLNotifierFilter(t *testing.T) {
231235

232236
t.Run("includes", func(t *testing.T) {
233237
urlNotifier := NewURLNotifier(URLNotifierParams{
234-
QueueSize: 20,
235238
URL: testUrl,
236-
APIKey: apiKey,
237-
APISecret: apiSecret,
239+
APIKey: testAPIKey,
240+
APISecret: testAPISecret,
238241
FilterParams: FilterParams{
239242
IncludeEvents: []string{EventRoomStarted},
240243
},
244+
Config: URLNotifierConfig{
245+
QueueSize: 20,
246+
},
241247
})
242248
defer urlNotifier.Stop(false)
243249

@@ -260,13 +266,15 @@ func TestURLNotifierFilter(t *testing.T) {
260266

261267
t.Run("excludes", func(t *testing.T) {
262268
urlNotifier := NewURLNotifier(URLNotifierParams{
263-
QueueSize: 20,
264269
URL: testUrl,
265-
APIKey: apiKey,
266-
APISecret: apiSecret,
270+
APIKey: testAPIKey,
271+
APISecret: testAPISecret,
267272
FilterParams: FilterParams{
268273
ExcludeEvents: []string{EventRoomStarted},
269274
},
275+
Config: URLNotifierConfig{
276+
QueueSize: 20,
277+
},
270278
})
271279
defer urlNotifier.Stop(false)
272280

@@ -289,14 +297,16 @@ func TestURLNotifierFilter(t *testing.T) {
289297

290298
t.Run("includes + excludes", func(t *testing.T) {
291299
urlNotifier := NewURLNotifier(URLNotifierParams{
292-
QueueSize: 20,
293300
URL: testUrl,
294-
APIKey: apiKey,
295-
APISecret: apiSecret,
301+
APIKey: testAPIKey,
302+
APISecret: testAPISecret,
296303
FilterParams: FilterParams{
297304
IncludeEvents: []string{EventRoomStarted},
298305
ExcludeEvents: []string{EventRoomStarted, EventRoomFinished},
299306
},
307+
Config: URLNotifierConfig{
308+
QueueSize: 20,
309+
},
300310
})
301311
defer urlNotifier.Stop(false)
302312

@@ -321,10 +331,12 @@ func TestURLNotifierFilter(t *testing.T) {
321331

322332
func newTestNotifier() *URLNotifier {
323333
return NewURLNotifier(URLNotifierParams{
324-
QueueSize: 20,
325334
URL: testUrl,
326-
APIKey: apiKey,
327-
APISecret: apiSecret,
335+
APIKey: testAPIKey,
336+
APISecret: testAPISecret,
337+
Config: URLNotifierConfig{
338+
QueueSize: 20,
339+
},
328340
})
329341
}
330342

@@ -336,7 +348,13 @@ func TestResourceWebHook(t *testing.T) {
336348
defer s.Stop()
337349

338350
t.Run("test event payload", func(t *testing.T) {
339-
resourceURLNotifier := NewDefaultNotifier(apiKey, apiSecret, []string{testUrl})
351+
resourceURLNotifier := NewDefaultNotifier(
352+
WebHookConfig{
353+
URLs: []string{testUrl},
354+
APIKey: testAPIKey,
355+
},
356+
testAPISecret,
357+
)
340358
defer resourceURLNotifier.Stop(false)
341359

342360
event := &livekit.WebhookEvent{
@@ -599,8 +617,8 @@ func TestResourceURLNotifierLifecycle(t *testing.T) {
599617
t.Run("times out after accepting connection", func(t *testing.T) {
600618
resourceURLNotifier := NewResourceURLNotifier(ResourceURLNotifierParams{
601619
URL: testUrl,
602-
APIKey: apiKey,
603-
APISecret: apiSecret,
620+
APIKey: testAPIKey,
621+
APISecret: testAPISecret,
604622
Config: ResourceURLNotifierConfig{
605623
MaxAge: 200 * time.Millisecond,
606624
MaxDepth: 50,
@@ -636,8 +654,8 @@ func TestResourceURLNotifierLifecycle(t *testing.T) {
636654
defer ln.Close()
637655
resourceURLNotifier := NewResourceURLNotifier(ResourceURLNotifierParams{
638656
URL: "http://localhost:9987",
639-
APIKey: apiKey,
640-
APISecret: apiSecret,
657+
APIKey: testAPIKey,
658+
APISecret: testAPISecret,
641659
Config: ResourceURLNotifierConfig{
642660
MaxAge: 200 * time.Millisecond,
643661
MaxDepth: 50,
@@ -665,8 +683,8 @@ func TestResourceURLNotifierFilter(t *testing.T) {
665683
t.Run("none", func(t *testing.T) {
666684
resourceURLNotifier := NewResourceURLNotifier(ResourceURLNotifierParams{
667685
URL: testUrl,
668-
APIKey: apiKey,
669-
APISecret: apiSecret,
686+
APIKey: testAPIKey,
687+
APISecret: testAPISecret,
670688
Config: ResourceURLNotifierConfig{
671689
MaxAge: 200 * time.Millisecond,
672690
MaxDepth: 50,
@@ -695,8 +713,8 @@ func TestResourceURLNotifierFilter(t *testing.T) {
695713
t.Run("includes", func(t *testing.T) {
696714
resourceURLNotifier := NewResourceURLNotifier(ResourceURLNotifierParams{
697715
URL: testUrl,
698-
APIKey: apiKey,
699-
APISecret: apiSecret,
716+
APIKey: testAPIKey,
717+
APISecret: testAPISecret,
700718
Config: ResourceURLNotifierConfig{
701719
MaxAge: 200 * time.Millisecond,
702720
MaxDepth: 50,
@@ -727,8 +745,8 @@ func TestResourceURLNotifierFilter(t *testing.T) {
727745
t.Run("excludes", func(t *testing.T) {
728746
resourceURLNotifier := NewResourceURLNotifier(ResourceURLNotifierParams{
729747
URL: testUrl,
730-
APIKey: apiKey,
731-
APISecret: apiSecret,
748+
APIKey: testAPIKey,
749+
APISecret: testAPISecret,
732750
Config: ResourceURLNotifierConfig{
733751
MaxAge: 200 * time.Millisecond,
734752
MaxDepth: 50,
@@ -759,8 +777,8 @@ func TestResourceURLNotifierFilter(t *testing.T) {
759777
t.Run("includes + excludes", func(t *testing.T) {
760778
resourceURLNotifier := NewResourceURLNotifier(ResourceURLNotifierParams{
761779
URL: testUrl,
762-
APIKey: apiKey,
763-
APISecret: apiSecret,
780+
APIKey: testAPIKey,
781+
APISecret: testAPISecret,
764782
Config: ResourceURLNotifierConfig{
765783
MaxAge: 200 * time.Millisecond,
766784
MaxDepth: 50,
@@ -794,8 +812,8 @@ func TestResourceURLNotifierFilter(t *testing.T) {
794812
func newTestResourceNotifier(timeout time.Duration, maxAge time.Duration, maxDepth int) *ResourceURLNotifier {
795813
return NewResourceURLNotifier(ResourceURLNotifierParams{
796814
URL: testUrl,
797-
APIKey: apiKey,
798-
APISecret: apiSecret,
815+
APIKey: testAPIKey,
816+
APISecret: testAPISecret,
799817
Timeout: timeout,
800818
Config: ResourceURLNotifierConfig{
801819
MaxAge: maxAge,

0 commit comments

Comments
 (0)