-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscanner.go
More file actions
54 lines (48 loc) · 1.16 KB
/
scanner.go
File metadata and controls
54 lines (48 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"bufio"
"context"
"fmt"
"io"
)
// scanner is a wrapper around [bufio.Scanner] that allows reading input with context cancellation.
type scanner struct {
*bufio.Scanner
}
func newScanner(r io.Reader) *scanner {
return &scanner{
Scanner: bufio.NewScanner(r),
}
}
// Read reads input from the scanner, respecting the provided context.
// It returns an error if the context is canceled or if there is an error scanning the input.
func (s *scanner) Read(ctx context.Context) error {
scanned := make(chan struct{})
errChan := make(chan error)
defer func() {
// clean up channels when done
close(errChan)
close(scanned)
}()
// because bufio.Scanner does not support context cancellation directly,
// we run the scanning in a goroutine and use channels to communicate completion or errors.
go func() {
s.Scan()
err := s.Err()
if err != nil {
errChan <- fmt.Errorf("error scanning input: %v", err)
return
}
// this will signal that scanning is done
scanned <- struct{}{}
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errChan:
return err
case <-scanned:
// Successfully scanned input
return nil
}
}