forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.go
67 lines (57 loc) · 1.67 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
package api
import (
"fmt"
"io"
"net/http"
"github.com/puppetlabs/wash/activity"
"github.com/puppetlabs/wash/plugin"
)
// swagger:route GET /fs/stream stream streamUpdates
//
// Stream updates
//
// Get a stream of new updates to the specified entry.
//
// Produces:
// - application/json
// - application/octet-stream
//
// Schemes: http
//
// Responses:
// 200: octetResponse
// 404: errorResp
// 500: errorResp
var streamHandler = handler{fn: func(w http.ResponseWriter, r *http.Request) *errorResponse {
entry, path, errResp := getEntryFromRequest(r)
if errResp != nil {
return errResp
}
if !plugin.StreamAction().IsSupportedOn(entry) {
return unsupportedActionResponse(path, plugin.StreamAction())
}
f, ok := w.(flushableWriter)
if !ok {
return unknownErrorResponse(fmt.Errorf("Cannot stream %v, response handler does not support flushing", path))
}
ctx := r.Context()
rdr, err := plugin.StreamWithAnalytics(ctx, entry.(plugin.Streamable))
if err != nil {
return erroredActionResponse(path, plugin.StreamAction(), err.Error())
}
activity.Record(ctx, "API: Streaming %v", path)
// Do an initial flush to send the header.
w.WriteHeader(http.StatusOK)
f.Flush()
// Ensure it's closed when the context is cancelled.
go func() {
<-ctx.Done()
activity.Record(ctx, "API: Stream %v closed by completed context: %v", path, rdr.Close())
}()
// Ensure every write is a flush with streamableResponseWriter.
if _, err := io.Copy(&streamableResponseWriter{f}, rdr); err != nil {
// Common for copy to error when the caller closes the connection.
activity.Record(ctx, "API: Streaming %v errored: %v", path, err)
}
return nil
}}