• 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

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