-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
58 lines (47 loc) · 1.27 KB
/
notify.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
package wrap
import (
"sync"
"time"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)
type Notifee interface {
DialStarted(trpt string, raddr ma.Multiaddr, p peer.ID, start time.Time)
DialEnded(trpt string, raddr ma.Multiaddr, p peer.ID, start time.Time, end time.Time, err error)
}
type Notifier struct {
trpt string
notiflk sync.RWMutex
notifees map[Notifee]struct{}
}
func newNotifier(trpt string) *Notifier {
return &Notifier{
trpt: trpt,
notiflk: sync.RWMutex{},
notifees: map[Notifee]struct{}{},
}
}
func (n *Notifier) notifyDialStarted(raddr ma.Multiaddr, p peer.ID, start time.Time) {
n.notiflk.RLock()
defer n.notiflk.RUnlock()
for notifee := range n.notifees {
notifee.DialStarted(n.trpt, raddr, p, start)
}
}
func (n *Notifier) notifyDialEnded(raddr ma.Multiaddr, p peer.ID, start time.Time, end time.Time, err error) {
n.notiflk.RLock()
defer n.notiflk.RUnlock()
for notifee := range n.notifees {
notifee.DialEnded(n.trpt, raddr, p, start, end, err)
}
}
func (n *Notifier) Notify(notifee Notifee) {
n.notiflk.Lock()
defer n.notiflk.Unlock()
n.notifees[notifee] = struct{}{}
}
func (n *Notifier) StopNotify(notifee Notifee) {
n.notiflk.Lock()
defer n.notiflk.Unlock()
delete(n.notifees, notifee)
}