• 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

21.74
/build/version.go
1
// Copyright (c) 2013-2017 The btcsuite developers
2
// Copyright (c) 2015-2016 The Decred developers
3
// Heavily inspired by https://github.com/btcsuite/btcd/blob/master/version.go
4
// Copyright (C) 2015-2022 The Lightning Network Developers
5

6
package build
7

8
import (
9
        "context"
10
        "encoding/hex"
11
        "fmt"
12
        "runtime/debug"
13
        "strings"
14

15
        "github.com/btcsuite/btclog/v2"
16
)
17

18
var (
19
        // Commit stores the current commit of this build, which includes the
20
        // most recent tag, the number of commits since that tag (if non-zero),
21
        // the commit hash, and a dirty marker. This should be set using the
22
        // -ldflags during compilation.
23
        Commit string
24

25
        // CommitHash stores the current commit hash of this build.
26
        CommitHash string
27

28
        // RawTags contains the raw set of build tags, separated by commas.
29
        RawTags string
30

31
        // GoVersion stores the go version that the executable was compiled
32
        // with.
33
        GoVersion string
34
)
35

36
// semanticAlphabet is the set of characters that are permitted for use in an
37
// AppPreRelease.
38
const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-."
39

40
// These constants define the application version and follow the semantic
41
// versioning 2.0.0 spec (http://semver.org/).
42
const (
43
        // AppMajor defines the major version of this binary.
44
        AppMajor uint = 0
45

46
        // AppMinor defines the minor version of this binary.
47
        AppMinor uint = 18
48

49
        // AppPatch defines the application patch for this binary.
50
        AppPatch uint = 99
51

52
        // AppPreRelease MUST only contain characters from semanticAlphabet per
53
        // the semantic versioning spec.
54
        AppPreRelease = "beta"
55
)
56

57
func init() {
74✔
58
        // Assert that AppPreRelease is valid according to the semantic
74✔
59
        // versioning guidelines for pre-release version and build metadata
74✔
60
        // strings. In particular it MUST only contain characters in
74✔
61
        // semanticAlphabet.
74✔
62
        for _, r := range AppPreRelease {
370✔
63
                if !strings.ContainsRune(semanticAlphabet, r) {
296✔
64
                        panic(fmt.Errorf("rune: %v is not in the semantic "+
×
65
                                "alphabet", r))
×
66
                }
67
        }
68

69
        // Get build information from the runtime.
70
        if info, ok := debug.ReadBuildInfo(); ok {
148✔
71
                GoVersion = info.GoVersion
74✔
72
                for _, setting := range info.Settings {
74✔
73
                        switch setting.Key {
×
74
                        case "vcs.revision":
×
75
                                CommitHash = setting.Value
×
76

77
                        case "-tags":
×
78
                                RawTags = setting.Value
×
79
                        }
80
                }
81
        }
82
}
83

84
// Version returns the application version as a properly formed string per the
85
// semantic versioning 2.0.0 spec (http://semver.org/).
86
func Version() string {
×
87
        // Start with the major, minor, and patch versions.
×
88
        version := fmt.Sprintf("%d.%d.%d", AppMajor, AppMinor, AppPatch)
×
89

×
90
        // Append pre-release version if there is one. The hyphen called for by
×
91
        // the semantic versioning spec is automatically appended and should not
×
92
        // be contained in the pre-release string.
×
93
        if AppPreRelease != "" {
×
94
                version = fmt.Sprintf("%s-%s", version, AppPreRelease)
×
95
        }
×
96

97
        return version
×
98
}
99

100
// Tags returns the list of build tags that were compiled into the executable.
101
func Tags() []string {
×
102
        if len(RawTags) == 0 {
×
103
                return nil
×
104
        }
×
105

106
        return strings.Split(RawTags, ",")
×
107
}
108

109
// WithBuildInfo derives a child context with the build information attached as
110
// attributes. At the moment, this only includes the current build's commit
111
// hash.
112
func WithBuildInfo(ctx context.Context, cfg *LogConfig) (context.Context,
113
        error) {
×
114

×
115
        if cfg.NoCommitHash {
×
116
                return ctx, nil
×
117
        }
×
118

119
        // Convert the commit hash to a byte slice.
120
        commitHash, err := hex.DecodeString(CommitHash)
×
121
        if err != nil {
×
122
                return nil, fmt.Errorf("unable to decode commit hash: %w", err)
×
123
        }
×
124

125
        // Include the first 3 bytes of the commit hash in the context as an
126
        // slog attribute.
127
        if len(commitHash) > 3 {
×
128
                commitHash = commitHash[:3]
×
129
        }
×
130

131
        return btclog.WithCtx(ctx, btclog.Hex("rev", commitHash)), nil
×
132
}
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