mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-12 00:27:35 +02:00
core/config: refactor change dispatcher (#4657)
* core/config: refactor change dispatcher * update test * close listener go routine when context is canceled * use cancel cause * use context * add more time * more time
This commit is contained in:
parent
53573dc046
commit
e0693e54f0
4 changed files with 241 additions and 14 deletions
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/events"
|
||||
"github.com/pomerium/pomerium/internal/fileutil"
|
||||
"github.com/pomerium/pomerium/internal/log"
|
||||
"github.com/pomerium/pomerium/internal/telemetry/metrics"
|
||||
|
@ -19,27 +20,27 @@ import (
|
|||
// A ChangeListener is called when configuration changes.
|
||||
type ChangeListener = func(context.Context, *Config)
|
||||
|
||||
type changeDispatcherEvent struct {
|
||||
cfg *Config
|
||||
}
|
||||
|
||||
// A ChangeDispatcher manages listeners on config changes.
|
||||
type ChangeDispatcher struct {
|
||||
sync.Mutex
|
||||
onConfigChangeListeners []ChangeListener
|
||||
target events.Target[changeDispatcherEvent]
|
||||
}
|
||||
|
||||
// Trigger triggers a change.
|
||||
func (dispatcher *ChangeDispatcher) Trigger(ctx context.Context, cfg *Config) {
|
||||
dispatcher.Lock()
|
||||
defer dispatcher.Unlock()
|
||||
|
||||
for _, li := range dispatcher.onConfigChangeListeners {
|
||||
li(ctx, cfg)
|
||||
}
|
||||
dispatcher.target.Dispatch(ctx, changeDispatcherEvent{
|
||||
cfg: cfg,
|
||||
})
|
||||
}
|
||||
|
||||
// OnConfigChange adds a listener.
|
||||
func (dispatcher *ChangeDispatcher) OnConfigChange(_ context.Context, li ChangeListener) {
|
||||
dispatcher.Lock()
|
||||
defer dispatcher.Unlock()
|
||||
dispatcher.onConfigChangeListeners = append(dispatcher.onConfigChangeListeners, li)
|
||||
dispatcher.target.AddListener(func(ctx context.Context, evt changeDispatcherEvent) {
|
||||
li(ctx, evt.cfg)
|
||||
})
|
||||
}
|
||||
|
||||
// A Source gets configuration.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue