Skip to content

Commit

Permalink
make init explicitly callable
Browse files Browse the repository at this point in the history
  • Loading branch information
d-led committed Jun 23, 2024
1 parent f660275 commit cbb1ef8
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions emergency_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ type EmergencyPersistence struct {
signals chan os.Signal
}

func (s *EmergencyPersistence) AddForPersistence(p Persistent) {
s.Act(s, func() {
if len(s.toPersist) == 0 {
s.signals = make(chan os.Signal, 1)
signal.Notify(s.signals, syscall.SIGINT, syscall.SIGTERM)
func (s *EmergencyPersistence) Init() {
phony.Block(s, func() {
if len(s.toPersist) > 0 {
// already initialized
return
}

// initialize on demand or upon first add
s.signals = make(chan os.Signal, 1)
signal.Notify(s.signals, syscall.SIGINT, syscall.SIGTERM)
})
}

func (s *EmergencyPersistence) AddForPersistence(p Persistent) {
s.Init()
phony.Block(s, func() {
s.toPersist = append(s.toPersist, p)
s.wg.Add(1)
})
Expand Down

0 comments on commit cbb1ef8

Please sign in to comment.