Skip to content

Commit 0039716

Browse files
committed
FS: Expose Close() methods to prevent FDs leak
1 parent 1e608d9 commit 0039716

File tree

10 files changed

+193
-423
lines changed

10 files changed

+193
-423
lines changed

interfaces_fs.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,32 @@ type FileSystemStats interface {
2424
// to store and retrieve [Error] instances.
2525
type FileSystemErrors interface {
2626
StoreError(ctx context.Context, err Error) error
27-
LoadErrors(ctx context.Context) ([]Error, error)
2827
ErrorsIterator(ctx context.Context) (chan Error, CloseFunc, error)
28+
CloseErrors(ctx context.Context) error
2929
}
3030

3131
// FileSystemMatches defines the behavior expected from a [scan] file system
3232
// to store and retrieve [Match] instances.
3333
type FileSystemMatches interface {
3434
StoreMatch(ctx context.Context, match Match) error
35-
LoadMatches(ctx context.Context) ([]Match, error)
3635
MatchesIterator(ctx context.Context) (chan Match, CloseFunc, error)
36+
CloseMatches(ctx context.Context) error
3737
}
3838

3939
// FileSystemSummaries defines the behavior expected from a [scan] file system
4040
// to store and retrieve [TaskSummary] instances.
4141
type FileSystemSummaries interface {
4242
StoreTaskSummary(ctx context.Context, ts TaskSummary) error
43-
LoadTasksSummaries(ctx context.Context) ([]TaskSummary, error)
4443
TasksSummariesIterator(ctx context.Context) (chan TaskSummary, CloseFunc, error)
44+
CloseTasksSummaries(ctx context.Context) error
4545
}
4646

4747
// FileSystemTemplates defines the behavior expected from a [scan] file system
4848
// to store and retrieve [Template] instances.
4949
type FileSystemTemplates interface {
5050
StoreTemplate(ctx context.Context, tpl Template) error
51-
LoadTemplates(ctx context.Context) ([]Template, error)
52-
53-
// TemplatesIterator returns a channel of Template (or an error),
54-
// so the channel can be used as an iterator.
55-
// The returned channel is closed when the iterator is done (no more elements)
56-
// or when the context is canceled.
57-
// Thus, the context cancellation can also be used to stop the iteration.
58-
TemplatesIterator(ctx context.Context) (chan Template, error)
51+
TemplatesIterator(ctx context.Context) (chan Template, CloseFunc, error)
52+
CloseTemplates(ctx context.Context) error
5953
}
6054

6155
// CloseFunc is a function that can be used to close something that's open.

internal/platform/writer/console.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func (c *Console) WriteMatchesSummary(ctx context.Context, fs gbounty.FileSystem
128128
if err != nil {
129129
return err
130130
}
131+
defer closeIt()
131132

132133
profileTypes := make(map[string]string)
133134
byIssue := make(map[string]map[string]struct{ count int })
@@ -174,8 +175,6 @@ func (c *Console) WriteMatchesSummary(ctx context.Context, fs gbounty.FileSystem
174175
}
175176
}
176177

177-
closeIt()
178-
179178
if !atLeastOne {
180179
_, err := fmt.Fprint(c.writer, infoPrinter().Sprintln(color.Cyan().Sprint("No matches found")))
181180
if err != nil {
@@ -241,6 +240,7 @@ func (c *Console) WriteErrors(ctx context.Context, fs gbounty.FileSystem) error
241240
if err != nil {
242241
return err
243242
}
243+
defer closeIt()
244244

245245
for scanError := range ch {
246246
builder := strings.Builder{}
@@ -279,8 +279,6 @@ func (c *Console) WriteErrors(ctx context.Context, fs gbounty.FileSystem) error
279279
}
280280
}
281281

282-
closeIt()
283-
284282
return nil
285283
}
286284

@@ -376,6 +374,7 @@ func (c *Console) WriteMatches(ctx context.Context, fs gbounty.FileSystem, _ boo
376374
if err != nil {
377375
return err
378376
}
377+
defer closeIt()
379378

380379
for m := range ch {
381380
builder := strings.Builder{}
@@ -423,8 +422,6 @@ func (c *Console) WriteMatches(ctx context.Context, fs gbounty.FileSystem, _ boo
423422
}
424423
}
425424

426-
closeIt()
427-
428425
return nil
429426
}
430427

@@ -443,6 +440,7 @@ func (c *Console) WriteTasks(ctx context.Context, fs gbounty.FileSystem, allRequ
443440
if err != nil {
444441
return err
445442
}
443+
defer closeIt()
446444

447445
for scanTask := range ch {
448446
builder := strings.Builder{}
@@ -480,8 +478,6 @@ func (c *Console) WriteTasks(ctx context.Context, fs gbounty.FileSystem, allRequ
480478
}
481479
}
482480

483-
closeIt()
484-
485481
return nil
486482
}
487483

internal/platform/writer/json.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func (j JSON) WriteMatchesSummary(ctx context.Context, fs gbounty.FileSystem) er
8383
if err != nil {
8484
return err
8585
}
86+
defer closeIt()
8687

8788
profileTypes := make(map[string]string)
8889
byIssue := make(map[string]map[string]struct{ count int })
@@ -145,8 +146,6 @@ func (j JSON) WriteMatchesSummary(ctx context.Context, fs gbounty.FileSystem) er
145146
_, err = fmt.Fprint(j.writer, `
146147
]`)
147148

148-
closeIt()
149-
150149
return err
151150
}
152151

@@ -250,6 +249,7 @@ func (j JSON) WriteErrors(ctx context.Context, fs gbounty.FileSystem) error {
250249
if err != nil {
251250
return err
252251
}
252+
defer closeIt()
253253

254254
first := true
255255

@@ -354,8 +354,6 @@ func (j JSON) WriteErrors(ctx context.Context, fs gbounty.FileSystem) error {
354354
_, err = fmt.Fprint(j.writer, `
355355
]`)
356356

357-
closeIt()
358-
359357
return err
360358
}
361359

@@ -466,6 +464,7 @@ func (j JSON) WriteMatches(ctx context.Context, fs gbounty.FileSystem, includeRe
466464
if err != nil {
467465
return err
468466
}
467+
defer closeIt()
469468

470469
first := true
471470

@@ -576,8 +575,6 @@ func (j JSON) WriteMatches(ctx context.Context, fs gbounty.FileSystem, includeRe
576575
_, err = fmt.Fprint(j.writer, `
577576
]`)
578577

579-
closeIt()
580-
581578
return err
582579
}
583580

internal/platform/writer/markdown.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func (md Markdown) WriteMatchesSummary(ctx context.Context, fs gbounty.FileSyste
8787
if err != nil {
8888
return err
8989
}
90+
defer closeIt()
9091

9192
profileTypes := make(map[string]string)
9293
byIssue := make(map[string]map[string]struct{ count int })
@@ -132,8 +133,6 @@ func (md Markdown) WriteMatchesSummary(ctx context.Context, fs gbounty.FileSyste
132133
}
133134
}
134135

135-
closeIt()
136-
137136
if !atLeastOne {
138137
_, err := fmt.Fprint(md.writer, "**No matches found**\n")
139138
if err != nil {
@@ -193,6 +192,7 @@ func (md Markdown) WriteErrors(ctx context.Context, fs gbounty.FileSystem) error
193192
if err != nil {
194193
return err
195194
}
195+
defer closeIt()
196196

197197
for scanError := range ch {
198198
builder := strings.Builder{}
@@ -234,8 +234,6 @@ func (md Markdown) WriteErrors(ctx context.Context, fs gbounty.FileSystem) error
234234
}
235235
}
236236

237-
closeIt()
238-
239237
return nil
240238
}
241239

@@ -298,6 +296,7 @@ func (md Markdown) WriteMatches(ctx context.Context, fs gbounty.FileSystem, incl
298296
if err != nil {
299297
return err
300298
}
299+
defer closeIt()
301300

302301
for m := range ch {
303302
builder := strings.Builder{}
@@ -348,8 +347,6 @@ func (md Markdown) WriteMatches(ctx context.Context, fs gbounty.FileSystem, incl
348347
}
349348
}
350349

351-
closeIt()
352-
353350
return nil
354351
}
355352

internal/platform/writer/plain.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func (p *Plain) WriteMatchesSummary(ctx context.Context, fs gbounty.FileSystem)
114114
if err != nil {
115115
return err
116116
}
117+
defer closeIt()
117118

118119
profileTypes := make(map[string]string)
119120
byIssue := make(map[string]map[string]struct{ count int })
@@ -158,8 +159,6 @@ func (p *Plain) WriteMatchesSummary(ctx context.Context, fs gbounty.FileSystem)
158159
}
159160
}
160161

161-
closeIt()
162-
163162
if !atLeastOne {
164163
_, err := fmt.Fprint(p.writer, "No matches found\n")
165164
if err != nil {
@@ -225,6 +224,7 @@ func (p *Plain) WriteErrors(ctx context.Context, fs gbounty.FileSystem) error {
225224
if err != nil {
226225
return err
227226
}
227+
defer closeIt()
228228

229229
for scanError := range ch {
230230
builder := strings.Builder{}
@@ -263,8 +263,6 @@ func (p *Plain) WriteErrors(ctx context.Context, fs gbounty.FileSystem) error {
263263
}
264264
}
265265

266-
closeIt()
267-
268266
return nil
269267
}
270268

@@ -333,6 +331,7 @@ func (p *Plain) WriteMatches(ctx context.Context, fs gbounty.FileSystem, include
333331
if err != nil {
334332
return err
335333
}
334+
defer closeIt()
336335

337336
for m := range ch {
338337
builder := strings.Builder{}
@@ -379,8 +378,6 @@ func (p *Plain) WriteMatches(ctx context.Context, fs gbounty.FileSystem, include
379378
}
380379
}
381380

382-
closeIt()
383-
384381
return nil
385382
}
386383

0 commit comments

Comments
 (0)