-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.go
63 lines (54 loc) · 1.26 KB
/
signal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2024 Outreach Corporation. All Rights Reserved.
// Description: Signal to notify when certain even occures.
package plumber
import (
"context"
"sync"
)
// Signal is and helper struct that broadcast a notification when certain even occures.
type Signal struct {
sync.Once
ch chan struct{}
}
// Notify send a signal to listener. Signal is send just once.
func (s *Signal) Notify() {
s.Once.Do(
func() {
close(s.ch)
},
)
}
// C returns a channel that can be used to received
func (s *Signal) C() <-chan struct{} {
return s.ch
}
// NewSignal returns a signal notifier
func NewSignal() *Signal {
return &Signal{
ch: make(chan struct{}),
}
}
// forwardErrorSignal forward information that a Runner errored to given signal instance
func forwardErrorSignal(ctx context.Context, runner Runner, closed <-chan struct{}, signal *Signal) {
if notifier, ok := runner.(ErrorNotifier); ok {
select {
case <-closed:
return
case <-ctx.Done():
return
case <-notifier.Errored():
signal.Notify()
}
}
}
// closeOnError when given signal reports error that runner is closed
func closeOnError(ctx context.Context, signal *Signal, runner Closeable) {
go func() {
select {
case <-ctx.Done():
return
case <-signal.C():
runner.Close(ctx)
}
}()
}