mirror of
https://github.com/pomerium/pomerium.git
synced 2025-07-18 17:18:16 +02:00
core/config: refactor change dispatcher
This commit is contained in:
parent
a29476f61e
commit
ddb2b41665
3 changed files with 226 additions and 11 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,29 @@ import (
|
|||
// A ChangeListener is called when configuration changes.
|
||||
type ChangeListener = func(context.Context, *Config)
|
||||
|
||||
type changeDispatcherEvent struct {
|
||||
ctx context.Context
|
||||
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(changeDispatcherEvent{
|
||||
ctx: ctx,
|
||||
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(evt changeDispatcherEvent) {
|
||||
li(evt.ctx, evt.cfg)
|
||||
})
|
||||
}
|
||||
|
||||
// A Source gets configuration.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue