Use sync.Once here to instantiate the event handler

This commit is contained in:
Dane Everitt 2020-12-25 17:07:40 -08:00
parent 22c53c365a
commit a4c68eed16
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53

View File

@ -23,7 +23,7 @@ var _ environment.ProcessEnvironment = (*Environment)(nil)
type Environment struct { type Environment struct {
mu sync.RWMutex mu sync.RWMutex
eventMu sync.Mutex eventMu sync.Once
// The public identifier for this environment. In this case it is the Docker container // The public identifier for this environment. In this case it is the Docker container
// name that will be used for all instances created under it. // name that will be used for all instances created under it.
@ -90,13 +90,9 @@ func (e *Environment) IsAttached() bool {
} }
func (e *Environment) Events() *events.EventBus { func (e *Environment) Events() *events.EventBus {
e.eventMu.Lock() e.eventMu.Do(func() {
defer e.eventMu.Unlock()
if e.emitter == nil {
e.emitter = events.New() e.emitter = events.New()
} })
return e.emitter return e.emitter
} }