• 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

12.5
/watchtower/conf.go
1
package watchtower
2

3
import (
4
        "strconv"
5
        "time"
6
)
7

8
// Conf specifies the watchtower options that can be configured from the command
9
// line or configuration file.
10
type Conf struct {
11
        // RawListeners configures the watchtower's listening ports/interfaces.
12
        RawListeners []string `long:"listen" description:"Add interfaces/ports to listen for peer connections"`
13

14
        // RawExternalIPs configures the watchtower's external ports/interfaces.
15
        RawExternalIPs []string `long:"externalip" description:"Add interfaces/ports where the watchtower can accept peer connections"`
16

17
        // ReadTimeout specifies the duration the tower will wait when trying to
18
        // read a message from a client before hanging up.
19
        ReadTimeout time.Duration `long:"readtimeout" description:"Duration the watchtower server will wait for messages to be received before hanging up on clients"`
20

21
        // WriteTimeout specifies the duration the tower will wait when trying
22
        // to write a message from a client before hanging up.
23
        WriteTimeout time.Duration `long:"writetimeout" description:"Duration the watchtower server will wait for messages to be written before hanging up on client connections"`
24
}
25

26
// DefaultConf returns a Conf with some default values filled in.
27
func DefaultConf() *Conf {
1✔
28
        return &Conf{
1✔
29
                ReadTimeout:  DefaultReadTimeout,
1✔
30
                WriteTimeout: DefaultWriteTimeout,
1✔
31
        }
1✔
32
}
1✔
33

34
// Apply completes the passed Config struct by applying any parsed Conf options.
35
// If the corresponding values parsed by Conf are already set in the Config,
36
// those fields will be not be modified.
37
func (c *Conf) Apply(cfg *Config,
38
        normalizer AddressNormalizer) (*Config, error) {
×
39

×
40
        // Set the Config's listening addresses if they are empty.
×
41
        if cfg.ListenAddrs == nil {
×
42
                // Without a network, we will be unable to resolve the listening
×
43
                // addresses.
×
44
                if cfg.Net == nil {
×
45
                        return nil, ErrNoNetwork
×
46
                }
×
47

48
                // If no addresses are specified by the Config, we will resort
49
                // to the default peer port.
50
                if len(c.RawListeners) == 0 {
×
51
                        addr := DefaultListenAddr
×
52
                        c.RawListeners = append(c.RawListeners, addr)
×
53
                }
×
54

55
                // Normalize the raw listening addresses so that they can be
56
                // used by the brontide listener.
57
                var err error
×
58
                cfg.ListenAddrs, err = normalizer(
×
59
                        c.RawListeners, strconv.Itoa(DefaultPeerPort),
×
60
                        cfg.Net.ResolveTCPAddr,
×
61
                )
×
62
                if err != nil {
×
63
                        return nil, err
×
64
                }
×
65
        }
66

67
        // Set the Config's external ips if they are empty.
68
        if cfg.ExternalIPs == nil {
×
69
                // Without a network, we will be unable to resolve the external
×
70
                // IP addresses.
×
71
                if cfg.Net == nil {
×
72
                        return nil, ErrNoNetwork
×
73
                }
×
74

75
                var err error
×
76
                cfg.ExternalIPs, err = normalizer(
×
77
                        c.RawExternalIPs, strconv.Itoa(DefaultPeerPort),
×
78
                        cfg.Net.ResolveTCPAddr,
×
79
                )
×
80
                if err != nil {
×
81
                        return nil, err
×
82
                }
×
83
        }
84

85
        // If the Config has no read timeout, we will use the parsed Conf
86
        // value.
87
        if cfg.ReadTimeout == 0 && c.ReadTimeout != 0 {
×
88
                cfg.ReadTimeout = c.ReadTimeout
×
89
        }
×
90

91
        // If the Config has no write timeout, we will use the parsed Conf
92
        // value.
93
        if cfg.WriteTimeout == 0 && c.WriteTimeout != 0 {
×
94
                cfg.WriteTimeout = c.WriteTimeout
×
95
        }
×
96

97
        return cfg, nil
×
98
}
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