• 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

5.45
/build/logrotator.go
1
package build
2

3
import (
4
        "compress/gzip"
5
        "fmt"
6
        "io"
7
        "os"
8
        "path/filepath"
9

10
        "github.com/jrick/logrotate/rotator"
11
        "github.com/klauspost/compress/zstd"
12
)
13

14
// RotatingLogWriter is a wrapper around the LogWriter that supports log file
15
// rotation.
16
type RotatingLogWriter struct {
17
        // pipe is the write-end pipe for writing to the log rotator.
18
        pipe *io.PipeWriter
19

20
        rotator *rotator.Rotator
21
}
22

23
// NewRotatingLogWriter creates a new file rotating log writer.
24
//
25
// NOTE: `InitLogRotator` must be called to set up log rotation after creating
26
// the writer.
27
func NewRotatingLogWriter() *RotatingLogWriter {
1✔
28
        return &RotatingLogWriter{}
1✔
29
}
1✔
30

31
// InitLogRotator initializes the log file rotator to write logs to logFile and
32
// create roll files in the same directory. It should be called as early on
33
// startup and possible and must be closed on shutdown by calling `Close`.
34
func (r *RotatingLogWriter) InitLogRotator(cfg *FileLoggerConfig,
35
        logFile string) error {
×
36

×
37
        logDir, _ := filepath.Split(logFile)
×
38
        err := os.MkdirAll(logDir, 0700)
×
39
        if err != nil {
×
40
                return fmt.Errorf("failed to create log directory: %w", err)
×
41
        }
×
42

43
        r.rotator, err = rotator.New(
×
44
                logFile, int64(cfg.MaxLogFileSize*1024), false, cfg.MaxLogFiles,
×
45
        )
×
46
        if err != nil {
×
47
                return fmt.Errorf("failed to create file rotator: %w", err)
×
48
        }
×
49

50
        // Reject unknown compressors.
51
        if !SupportedLogCompressor(cfg.Compressor) {
×
52
                return fmt.Errorf("unknown log compressor: %v", cfg.Compressor)
×
53
        }
×
54

55
        var c rotator.Compressor
×
56
        switch cfg.Compressor {
×
57
        case Gzip:
×
58
                c = gzip.NewWriter(nil)
×
59

60
        case Zstd:
×
61
                c, err = zstd.NewWriter(nil)
×
62
                if err != nil {
×
63
                        return fmt.Errorf("failed to create zstd compressor: "+
×
64
                                "%w", err)
×
65
                }
×
66
        }
67

68
        // Apply the compressor and its file suffix to the log rotator.
69
        r.rotator.SetCompressor(c, logCompressors[cfg.Compressor])
×
70

×
71
        // Run rotator as a goroutine now but make sure we catch any errors
×
72
        // that happen in case something with the rotation goes wrong during
×
73
        // runtime (like running out of disk space or not being allowed to
×
74
        // create a new logfile for whatever reason).
×
75
        pr, pw := io.Pipe()
×
76
        go func() {
×
77
                err := r.rotator.Run(pr)
×
78
                if err != nil {
×
79
                        _, _ = fmt.Fprintf(os.Stderr,
×
80
                                "failed to run file rotator: %v\n", err)
×
81
                }
×
82
        }()
83

84
        r.pipe = pw
×
85

×
86
        return nil
×
87
}
88

89
// Write writes the byte slice to the log rotator, if present.
90
func (r *RotatingLogWriter) Write(b []byte) (int, error) {
×
91
        if r.rotator != nil {
×
92
                return r.rotator.Write(b)
×
93
        }
×
94

95
        return len(b), nil
×
96
}
97

98
// Close closes the underlying log rotator if it has already been created.
99
func (r *RotatingLogWriter) Close() error {
×
100
        if r.rotator != nil {
×
101
                return r.rotator.Close()
×
102
        }
×
103

104
        return nil
×
105
}
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