• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12312390362

13 Dec 2024 08:44AM UTC coverage: 57.458% (+8.5%) from 48.92%
12312390362

Pull #9343

github

ellemouton
fn: rework the ContextGuard and add tests

In this commit, the ContextGuard struct is re-worked such that the
context that its new main WithCtx method provides is cancelled in sync
with a parent context being cancelled or with it's quit channel being
cancelled. Tests are added to assert the behaviour. In order for the
close of the quit channel to be consistent with the cancelling of the
derived context, the quit channel _must_ be contained internal to the
ContextGuard so that callers are only able to close the channel via the
exposed Quit method which will then take care to first cancel any
derived context that depend on the quit channel before returning.
Pull Request #9343: fn: expand the ContextGuard and add tests

101853 of 177264 relevant lines covered (57.46%)

24972.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/peernotifier/peernotifier.go
1
package peernotifier
2

3
import (
4
        "sync"
5

6
        "github.com/lightningnetwork/lnd/subscribe"
7
)
8

9
// PeerNotifier is a subsystem which observes peer offline and online events.
10
// It takes subscriptions for its events, and whenever it observes a new event
11
// it notifies its subscribers over the proper channel.
12
type PeerNotifier struct {
13
        started sync.Once
14
        stopped sync.Once
15

16
        ntfnServer *subscribe.Server
17
}
18

19
// PeerOnlineEvent represents a new event where a peer comes online.
20
type PeerOnlineEvent struct {
21
        // PubKey is the peer's compressed public key.
22
        PubKey [33]byte
23
}
24

25
// PeerOfflineEvent represents a new event where a peer goes offline.
26
type PeerOfflineEvent struct {
27
        // PubKey is the peer's compressed public key.
28
        PubKey [33]byte
29
}
30

31
// New creates a new peer notifier which notifies clients of peer online
32
// and offline events.
33
func New() *PeerNotifier {
×
34
        return &PeerNotifier{
×
35
                ntfnServer: subscribe.NewServer(),
×
36
        }
×
37
}
×
38

39
// Start starts the PeerNotifier's subscription server.
40
func (p *PeerNotifier) Start() error {
×
41
        var err error
×
42

×
43
        p.started.Do(func() {
×
44
                log.Info("PeerNotifier starting")
×
45
                err = p.ntfnServer.Start()
×
46
        })
×
47

48
        return err
×
49
}
50

51
// Stop signals the notifier for a graceful shutdown.
52
func (p *PeerNotifier) Stop() error {
×
53
        var err error
×
54
        p.stopped.Do(func() {
×
55
                log.Info("PeerNotifier shutting down...")
×
56
                defer log.Debug("PeerNotifier shutdown complete")
×
57

×
58
                err = p.ntfnServer.Stop()
×
59
        })
×
60
        return err
×
61
}
62

63
// SubscribePeerEvents returns a subscribe.Client that will receive updates
64
// any time the Server is informed of a peer event.
65
func (p *PeerNotifier) SubscribePeerEvents() (*subscribe.Client, error) {
×
66
        return p.ntfnServer.Subscribe()
×
67
}
×
68

69
// NotifyPeerOnline sends a peer online event to all clients subscribed to the
70
// peer notifier.
71
func (p *PeerNotifier) NotifyPeerOnline(pubKey [33]byte) {
×
72
        event := PeerOnlineEvent{PubKey: pubKey}
×
73

×
74
        log.Debugf("PeerNotifier notifying peer: %x online", pubKey)
×
75

×
76
        if err := p.ntfnServer.SendUpdate(event); err != nil {
×
77
                log.Warnf("Unable to send peer online update: %v", err)
×
78
        }
×
79
}
80

81
// NotifyPeerOffline sends a peer offline event to all the clients subscribed
82
// to the peer notifier.
83
func (p *PeerNotifier) NotifyPeerOffline(pubKey [33]byte) {
×
84
        event := PeerOfflineEvent{PubKey: pubKey}
×
85

×
86
        log.Debugf("PeerNotifier notifying peer: %x offline", pubKey)
×
87

×
88
        if err := p.ntfnServer.SendUpdate(event); err != nil {
×
89
                log.Warnf("Unable to send peer offline update: %v", err)
×
90
        }
×
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc