• 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

0.0
/build/handler_sets.go
1
package build
2

3
import (
4
        "context"
5
        "log/slog"
6

7
        btclogv1 "github.com/btcsuite/btclog"
8
        "github.com/btcsuite/btclog/v2"
9
)
10

11
// handlerSet is an implementation of Handler that abstracts away multiple
12
// Handlers.
13
type handlerSet struct {
14
        level btclogv1.Level
15
        set   []btclog.Handler
16
}
17

18
// newHandlerSet constructs a new HandlerSet.
19
func newHandlerSet(level btclogv1.Level, set ...btclog.Handler) *handlerSet {
×
20
        h := &handlerSet{
×
21
                set:   set,
×
22
                level: level,
×
23
        }
×
24
        h.SetLevel(level)
×
25

×
26
        return h
×
27
}
×
28

29
// Enabled reports whether the handler handles records at the given level.
30
//
31
// NOTE: this is part of the slog.Handler interface.
32
func (h *handlerSet) Enabled(ctx context.Context, level slog.Level) bool {
×
33
        for _, handler := range h.set {
×
34
                if !handler.Enabled(ctx, level) {
×
35
                        return false
×
36
                }
×
37
        }
38

39
        return true
×
40
}
41

42
// Handle handles the Record.
43
//
44
// NOTE: this is part of the slog.Handler interface.
45
func (h *handlerSet) Handle(ctx context.Context, record slog.Record) error {
×
46
        for _, handler := range h.set {
×
47
                if err := handler.Handle(ctx, record); err != nil {
×
48
                        return err
×
49
                }
×
50
        }
51

52
        return nil
×
53
}
54

55
// WithAttrs returns a new Handler whose attributes consist of both the
56
// receiver's attributes and the arguments.
57
//
58
// NOTE: this is part of the slog.Handler interface.
59
func (h *handlerSet) WithAttrs(attrs []slog.Attr) slog.Handler {
×
60
        newSet := &reducedSet{set: make([]slog.Handler, len(h.set))}
×
61
        for i, handler := range h.set {
×
62
                newSet.set[i] = handler.WithAttrs(attrs)
×
63
        }
×
64

65
        return newSet
×
66
}
67

68
// WithGroup returns a new Handler with the given group appended to the
69
// receiver's existing groups.
70
//
71
// NOTE: this is part of the slog.Handler interface.
72
func (h *handlerSet) WithGroup(name string) slog.Handler {
×
73
        newSet := &reducedSet{set: make([]slog.Handler, len(h.set))}
×
74
        for i, handler := range h.set {
×
75
                newSet.set[i] = handler.WithGroup(name)
×
76
        }
×
77

78
        return newSet
×
79
}
80

81
// SubSystem creates a new Handler with the given sub-system tag.
82
//
83
// NOTE: this is part of the Handler interface.
84
func (h *handlerSet) SubSystem(tag string) btclog.Handler {
×
85
        newSet := &handlerSet{set: make([]btclog.Handler, len(h.set))}
×
86
        for i, handler := range h.set {
×
87
                newSet.set[i] = handler.SubSystem(tag)
×
88
        }
×
89

90
        return newSet
×
91
}
92

93
// SetLevel changes the logging level of the Handler to the passed
94
// level.
95
//
96
// NOTE: this is part of the btclog.Handler interface.
97
func (h *handlerSet) SetLevel(level btclogv1.Level) {
×
98
        for _, handler := range h.set {
×
99
                handler.SetLevel(level)
×
100
        }
×
101
        h.level = level
×
102
}
103

104
// Level returns the current logging level of the Handler.
105
//
106
// NOTE: this is part of the btclog.Handler interface.
107
func (h *handlerSet) Level() btclogv1.Level {
×
108
        return h.level
×
109
}
×
110

111
// WithPrefix returns a copy of the Handler but with the given string prefixed
112
// to each log message.
113
//
114
// NOTE: this is part of the btclog.Handler interface.
115
func (h *handlerSet) WithPrefix(prefix string) btclog.Handler {
×
116
        newSet := &handlerSet{set: make([]btclog.Handler, len(h.set))}
×
117
        for i, handler := range h.set {
×
118
                newSet.set[i] = handler.WithPrefix(prefix)
×
119
        }
×
120

121
        return newSet
×
122
}
123

124
// A compile-time check to ensure that handlerSet implements btclog.Handler.
125
var _ btclog.Handler = (*handlerSet)(nil)
126

127
// reducedSet is an implementation of the slog.Handler interface which is
128
// itself backed by multiple slog.Handlers. This is used by the handlerSet
129
// WithGroup and WithAttrs methods so that we can apply the WithGroup and
130
// WithAttrs to the underlying handlers in the set. These calls, however,
131
// produce slog.Handlers and not btclog.Handlers. So the reducedSet represents
132
// the resulting set produced.
133
type reducedSet struct {
134
        set []slog.Handler
135
}
136

137
// Enabled reports whether the handler handles records at the given level.
138
//
139
// NOTE: this is part of the slog.Handler interface.
140
func (r *reducedSet) Enabled(ctx context.Context, level slog.Level) bool {
×
141
        for _, handler := range r.set {
×
142
                if !handler.Enabled(ctx, level) {
×
143
                        return false
×
144
                }
×
145
        }
146

147
        return true
×
148
}
149

150
// Handle handles the Record.
151
//
152
// NOTE: this is part of the slog.Handler interface.
153
func (r *reducedSet) Handle(ctx context.Context, record slog.Record) error {
×
154
        for _, handler := range r.set {
×
155
                if err := handler.Handle(ctx, record); err != nil {
×
156
                        return err
×
157
                }
×
158
        }
159

160
        return nil
×
161
}
162

163
// WithAttrs returns a new Handler whose attributes consist of both the
164
// receiver's attributes and the arguments.
165
//
166
// NOTE: this is part of the slog.Handler interface.
167
func (r *reducedSet) WithAttrs(attrs []slog.Attr) slog.Handler {
×
168
        newSet := &reducedSet{set: make([]slog.Handler, len(r.set))}
×
169
        for i, handler := range r.set {
×
170
                newSet.set[i] = handler.WithAttrs(attrs)
×
171
        }
×
172

173
        return newSet
×
174
}
175

176
// WithGroup returns a new Handler with the given group appended to the
177
// receiver's existing groups.
178
//
179
// NOTE: this is part of the slog.Handler interface.
180
func (r *reducedSet) WithGroup(name string) slog.Handler {
×
181
        newSet := &reducedSet{set: make([]slog.Handler, len(r.set))}
×
182
        for i, handler := range r.set {
×
183
                newSet.set[i] = handler.WithGroup(name)
×
184
        }
×
185

186
        return newSet
×
187
}
188

189
// A compile-time check to ensure that handlerSet implements slog.Handler.
190
var _ slog.Handler = (*reducedSet)(nil)
191

192
// subLogGenerator implements the SubLogCreator backed by a Handler.
193
type subLogGenerator struct {
194
        handler btclog.Handler
195
}
196

197
// newSubLogGenerator constructs a new subLogGenerator from a Handler.
198
func newSubLogGenerator(handler btclog.Handler) *subLogGenerator {
×
199
        return &subLogGenerator{
×
200
                handler: handler,
×
201
        }
×
202
}
×
203

204
// Logger returns a new logger for a particular sub-system.
205
//
206
// NOTE: this is part of the SubLogCreator interface.
207
func (b *subLogGenerator) Logger(subsystemTag string) btclog.Logger {
×
208
        handler := b.handler.SubSystem(subsystemTag)
×
209

×
210
        return btclog.NewSLogger(handler)
×
211
}
×
212

213
// A compile-time check to ensure that handlerSet implements slog.Handler.
214
var _ SubLogCreator = (*subLogGenerator)(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