-
Notifications
You must be signed in to change notification settings - Fork 5
/
stream.go
61 lines (52 loc) · 1.27 KB
/
stream.go
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
55
56
57
58
59
60
61
package main
import (
"errors"
"fmt"
"io"
"net/http"
)
func streamTs(c *http.Client, ts <-chan string, out io.Writer, done chan<- error) {
for url := range ts {
for {
err := func() error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return &retryError{fmt.Errorf("couldn't create ts request: %w", err)}
}
res, err := c.Do(req)
if err != nil {
return &retryError{fmt.Errorf("couldn't get ts: %w", err)}
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode >= 300 {
return &skipError{fmt.Errorf("got non-2xx http status %s", res.Status)}
}
_, err = io.Copy(&writerError{out}, &readerError{res.Body})
if err != nil && !errors.Is(err, io.EOF) {
if wErr, ok := err.(*writeError); ok {
return &fatalError{fmt.Errorf("error while writing ts to output: %w", wErr.Unwrap())}
}
return &skipError{fmt.Errorf("couldn't copy ts to output: %w", err)}
}
return nil
}()
if err != nil {
if _, ok := err.(*fatalError); ok {
done <- err
for range ts {
}
return
}
stdErr.Printf("%v\n", err)
if _, ok := err.(*skipError); ok {
break
}
if _, ok := err.(*retryError); ok {
continue
}
}
break
}
}
done <- nil
}