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

lightningnetwork / lnd / 12583319996

02 Jan 2025 01:38PM UTC coverage: 57.522% (-1.1%) from 58.598%
12583319996

Pull #9361

github

starius
fn/ContextGuard: use context.AfterFunc to wait

Simplifies context cancellation handling by using context.AfterFunc instead of a
goroutine to wait for context cancellation. This approach avoids the overhead of
a goroutine during the waiting period.

For ctxQuitUnsafe, since g.quit is closed only in the Quit method (which also
cancels all associated contexts), waiting on context cancellation ensures the
same behavior without unnecessary dependency on g.quit.

Added a test to ensure that the Create method does not launch any goroutines.
Pull Request #9361: fn: optimize context guard

102587 of 178344 relevant lines covered (57.52%)

24734.33 hits per line

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

29.03
/lncfg/cluster.go
1
package lncfg
2

3
import (
4
        "context"
5
        "fmt"
6
        "os"
7

8
        "github.com/lightningnetwork/lnd/cluster"
9
)
10

11
const (
12
        // DefaultEtcdElectionPrefix is used as election prefix if none is provided
13
        // through the config.
14
        DefaultEtcdElectionPrefix = "/leader/"
15
)
16

17
// Cluster holds configuration for clustered LND.
18
type Cluster struct {
19
        EnableLeaderElection bool `long:"enable-leader-election" description:"Enables leader election if set."`
20

21
        LeaderElector string `long:"leader-elector" choice:"etcd" description:"Leader elector to use. Valid values: \"etcd\"."`
22

23
        EtcdElectionPrefix string `long:"etcd-election-prefix" description:"Election key prefix when using etcd leader elector."`
24

25
        ID string `long:"id" description:"Identifier for this node inside the cluster (used in leader election). Defaults to the hostname."`
26

27
        LeaderSessionTTL int `long:"leader-session-ttl" description:"The TTL in seconds to use for the leader election session."`
28
}
29

30
// DefaultCluster creates and returns a new default DB config.
31
func DefaultCluster() *Cluster {
1✔
32
        hostname, _ := os.Hostname()
1✔
33
        return &Cluster{
1✔
34
                LeaderElector:      cluster.EtcdLeaderElector,
1✔
35
                EtcdElectionPrefix: DefaultEtcdElectionPrefix,
1✔
36
                LeaderSessionTTL:   90,
1✔
37
                ID:                 hostname,
1✔
38
        }
1✔
39
}
1✔
40

41
// MakeLeaderElector is a helper method to construct the concrete leader elector
42
// based on the current configuration.
43
func (c *Cluster) MakeLeaderElector(electionCtx context.Context, db *DB) (
44
        cluster.LeaderElector, error) {
×
45

×
46
        if c.LeaderElector == cluster.EtcdLeaderElector {
×
47
                return cluster.MakeLeaderElector(
×
48
                        electionCtx, c.LeaderElector, c.ID,
×
49
                        c.EtcdElectionPrefix, c.LeaderSessionTTL, db.Etcd,
×
50
                )
×
51
        }
×
52

53
        return nil, fmt.Errorf("unsupported leader elector")
×
54
}
55

56
// Validate validates the Cluster config.
57
func (c *Cluster) Validate() error {
×
58
        if !c.EnableLeaderElection {
×
59
                return nil
×
60
        }
×
61

62
        switch c.LeaderElector {
×
63
        case cluster.EtcdLeaderElector:
×
64
                if c.EtcdElectionPrefix == "" {
×
65
                        return fmt.Errorf("etcd-election-prefix must be set")
×
66
                }
×
67
                return nil
×
68

69
        default:
×
70
                return fmt.Errorf("unknown leader elector, valid values are: "+
×
71
                        "\"%v\"", cluster.EtcdLeaderElector)
×
72
        }
73
}
74

75
// Compile-time constraint to ensure Workers implements the Validator interface.
76
var _ Validator = (*Cluster)(nil)
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