1
0
Fork 0
mirror of https://github.com/kyverno/policy-reporter.git synced 2024-12-14 11:57:32 +00:00
policy-reporter/pkg/report/publisher.go
fengshunli 71377bf21b format comment format
Signed-off-by: fengshunli <1171313930@qq.com>
2023-01-18 11:18:45 +08:00

58 lines
1.4 KiB
Go

package report
import (
"sync"
)
type EventPublisher interface {
// RegisterListener register Handlers called on each PolicyReport Event
RegisterListener(string, PolicyReportListener)
// UnregisterListener removes an registered handler
UnregisterListener(string)
// GetListener returns a list of all registered Listeners
GetListener() map[string]PolicyReportListener
// Publish Process LifecycleEvent with all registered listeners
Publish(event LifecycleEvent)
}
type lifecycleEventPublisher struct {
listeners map[string]PolicyReportListener
listenerCount int
}
func (p *lifecycleEventPublisher) RegisterListener(name string, listener PolicyReportListener) {
p.listeners[name] = listener
p.listenerCount++
}
func (p *lifecycleEventPublisher) UnregisterListener(name string) {
if _, ok := p.listeners[name]; ok {
delete(p.listeners, name)
p.listenerCount--
}
}
func (p *lifecycleEventPublisher) GetListener() map[string]PolicyReportListener {
return p.listeners
}
func (p *lifecycleEventPublisher) Publish(event LifecycleEvent) {
g := sync.WaitGroup{}
g.Add(len(p.listeners))
for _, listener := range p.listeners {
go func(li PolicyReportListener, ev LifecycleEvent) {
li(ev)
g.Done()
}(listener, event)
}
g.Wait()
}
func NewEventPublisher() EventPublisher {
return &lifecycleEventPublisher{
listeners: make(map[string]func(LifecycleEvent)),
listenerCount: 0,
}
}