talos/internal/pkg/event/event.go
Andrew Rynhard 5b5d171c07 fix: block when handling bus event
If we don't block, there is the potential for multiple shutdown,
reboot, and upgrade requests to be processed.

Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
2020-01-20 09:19:50 -08:00

76 lines
1.6 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package event implements an embeddable type that uses the observation
// pattern to facilitate an event bus.
package event
// Type is event type.
type Type int
const (
// Shutdown is the shutdown event.
Shutdown = Type(iota)
// Reboot is the reboot event.
Reboot
// Upgrade is the upgrade event.
Upgrade
)
// Event represents an event in the observer pattern.
type Event struct {
Type Type
Data interface{}
}
// Channel is a channel for sending events.
type Channel chan Event
// Listeners is a slice of listeners to send events to.
type Listeners []Channel
// Observer is a component of the observer design pattern.
type Observer interface {
Channel() Channel
Types() []Type
}
// Notifier is a component of the observer design pattern.
type Notifier interface {
Notify(Event)
Register(Observer, ...Type)
Unregister(Observer)
}
// ObserveNotifier is a composite interface consisting of the Observer, and
// Notifier interfaces.
type ObserveNotifier interface {
Observer
Notifier
}
// Embeddable is a type that implements sane defaults as an observer.
type Embeddable struct {
Chan Channel
types []Type
}
// Channel implements the Observer interface.
func (e *Embeddable) Channel() Channel {
if e.Chan == nil {
e.Chan = make(Channel)
}
return e.Chan
}
// Types implements the Observer interface.
func (e *Embeddable) Types() []Type {
if e.types == nil {
e.types = []Type{Shutdown, Reboot, Upgrade}
}
return e.types
}