This repository has been archived by the owner on Sep 4, 2021. It is now read-only.
/
stream.go
153 lines (141 loc) · 3.32 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package httpclient
import (
"bufio"
"io"
"net/http"
"reflect"
"sync"
"time"
"github.com/flynn/flynn/pkg/attempt"
"github.com/flynn/flynn/pkg/sse"
"github.com/flynn/flynn/pkg/stream"
)
/*
Stream manufactures a `pkg/stream.Stream`, starts a worker pumping events out of decoding, and returns that.
The 'outputCh' parameter must be a sendable channel. The "zero"-values of channel's content type will be created and used in the deserialization, then sent.
The return values from `httpclient.RawReq` are probably a useful starting point for the 'res' parameter.
Closing the returned `stream.Stream` shuts down the worker.
*/
func Stream(res *http.Response, outputCh interface{}) stream.Stream {
stream := stream.New()
var chanValue reflect.Value
if v, ok := outputCh.(reflect.Value); ok {
chanValue = v
} else {
chanValue = reflect.ValueOf(outputCh)
}
stopChanValue := reflect.ValueOf(stream.StopCh)
msgType := chanValue.Type().Elem().Elem()
go func() {
done := make(chan struct{})
defer func() {
chanValue.Close()
close(done)
}()
go func() {
select {
case <-stream.StopCh:
case <-done:
}
res.Body.Close()
}()
r := bufio.NewReader(res.Body)
dec := sse.NewDecoder(r)
for {
msg := reflect.New(msgType)
if err := dec.Decode(msg.Interface()); err != nil {
if err != io.EOF {
stream.Error = err
}
break
}
chosen, _, _ := reflect.Select([]reflect.SelectCase{
{
Dir: reflect.SelectRecv,
Chan: stopChanValue,
},
{
Dir: reflect.SelectSend,
Chan: chanValue,
Send: msg,
},
})
switch chosen {
case 0:
return
default:
}
}
}()
return stream
}
var connectAttempts = attempt.Strategy{
Total: 20 * time.Second,
Delay: 100 * time.Millisecond,
}
func ResumingStream(connect func(int64) (*http.Response, error, bool), outputCh interface{}) (stream.Stream, error) {
stream := stream.New()
firstErr := make(chan error)
go func() {
var once sync.Once
var lastID int64
stopChanValue := reflect.ValueOf(stream.StopCh)
outValue := reflect.ValueOf(outputCh)
defer outValue.Close()
for {
var res *http.Response
// nonRetryableErr will be set if a connection attempt should not
// be retried (for example if a 404 is returned).
var nonRetryableErr error
err := connectAttempts.Run(func() (err error) {
var retry bool
res, err, retry = connect(lastID)
if !retry {
nonRetryableErr = err
return nil
}
return
})
if nonRetryableErr != nil {
err = nonRetryableErr
}
once.Do(func() { firstErr <- err })
if err != nil {
stream.Error = err
return
}
chanValue := reflect.MakeChan(outValue.Type(), 0)
s := Stream(res, chanValue)
loop:
for {
chosen, v, ok := reflect.Select([]reflect.SelectCase{
{
Dir: reflect.SelectRecv,
Chan: stopChanValue,
},
{
Dir: reflect.SelectRecv,
Chan: chanValue,
},
})
switch chosen {
case 0:
s.Close()
return
default:
if !ok {
// TODO: check s.Err() for a special error sent from the
// server indicating the stream should not be retried
break loop
}
id := v.Elem().FieldByName("ID")
if id.Kind() == reflect.Int64 {
lastID = id.Int()
}
outValue.Send(v)
}
}
}
}()
return stream, <-firstErr
}