-
Notifications
You must be signed in to change notification settings - Fork 405
/
runner_fninvoke.go
152 lines (128 loc) · 4.38 KB
/
runner_fninvoke.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
package server
import (
"bytes"
"io"
"net/http"
"strconv"
"sync"
"github.com/fnproject/fn/api"
"github.com/fnproject/fn/api/agent"
"github.com/fnproject/fn/api/common"
"github.com/fnproject/fn/api/models"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"go.opencensus.io/tag"
)
var (
bufPool = &sync.Pool{New: func() interface{} { return new(bytes.Buffer) }}
)
// ResponseBuffer implements http.ResponseWriter
type ResponseBuffer interface {
http.ResponseWriter
Status() int
}
// implements http.ResponseWriter
// this little guy buffers responses from user containers and lets them still
// set headers and such without us risking writing partial output [as much, the
// server could still die while we're copying the buffer]. this lets us set
// content length and content type nicely, as a bonus. it is sad, yes.
type syncResponseWriter struct {
headers http.Header
status int
*bytes.Buffer
}
var _ http.ResponseWriter = new(syncResponseWriter) // nice compiler errors
func (s *syncResponseWriter) Header() http.Header { return s.headers }
func (s *syncResponseWriter) WriteHeader(code int) { s.status = code }
func (s *syncResponseWriter) Status() int { return s.status }
// handleFnInvokeCall executes the function, for router handlers
func (s *Server) handleFnInvokeCall(c *gin.Context) {
fnID := c.Param(api.FnID)
ctx, _ := common.LoggerWithFields(c.Request.Context(), logrus.Fields{"fn_id": fnID})
c.Request = c.Request.WithContext(ctx)
err := s.handleFnInvokeCall2(c)
if err != nil {
handleErrorResponse(c, err)
}
}
// handleTriggerHTTPFunctionCall2 executes the function and returns an error
// Requires the following in the context:
func (s *Server) handleFnInvokeCall2(c *gin.Context) error {
ctx := c.Request.Context()
fn, err := s.lbReadAccess.GetFnByID(ctx, c.Param(api.FnID))
if err != nil {
return err
}
app, err := s.lbReadAccess.GetAppByID(ctx, fn.AppID)
if err != nil {
return err
}
err = s.ServeFnInvoke(c, app, fn)
if models.IsFuncError(err) || err == nil {
// report all user-directed errors and function responses from here, after submit has run.
// this is our never ending attempt to distinguish user and platform errors.
ctx, err := tag.New(ctx,
tag.Insert(whodunitKey, "user"),
)
if err != nil {
panic(err)
}
c.Request = c.Request.WithContext(ctx)
}
return err
}
func (s *Server) ServeFnInvoke(c *gin.Context, app *models.App, fn *models.Fn) error {
return s.fnInvoke(c.Writer, c.Request, app, fn, nil)
}
func (s *Server) fnInvoke(resp http.ResponseWriter, req *http.Request, app *models.App, fn *models.Fn, trig *models.Trigger) error {
// TODO: we should get rid of the buffers, and stream back (saves memory (+splice), faster (splice), allows streaming, don't have to cap resp size)
// buffer the response before writing it out to client to prevent partials from trying to stream
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
var writer ResponseBuffer
isDetached := req.Header.Get("Fn-Invoke-Type") == models.TypeDetached
if isDetached {
writer = agent.NewDetachedResponseWriter(resp.Header(), 202)
} else {
writer = &syncResponseWriter{
headers: resp.Header(),
status: 200,
Buffer: buf,
}
}
opts := getCallOptions(req, app, fn, trig, writer)
call, err := s.agent.GetCall(opts...)
if err != nil {
return err
}
// add this before submit, always tie a call id to the response at this point
writer.Header().Add("Fn-Call-Id", call.Model().ID)
err = s.agent.Submit(call)
if err != nil {
return err
}
// because we can...
writer.Header().Set("Content-Length", strconv.Itoa(int(buf.Len())))
// buffered response writer traps status (so we can add headers), we need to write it still
if writer.Status() > 0 {
resp.WriteHeader(writer.Status())
}
if isDetached {
return nil
}
io.Copy(resp, buf)
bufPool.Put(buf) // at this point, submit returned without timing out, so we can re-use this one
return nil
}
func getCallOptions(req *http.Request, app *models.App, fn *models.Fn, trig *models.Trigger, rw http.ResponseWriter) []agent.CallOpt {
var opts []agent.CallOpt
opts = append(opts, agent.WithWriter(rw)) // XXX (reed): order matters [for now]
opts = append(opts, agent.FromHTTPFnRequest(app, fn, req))
if req.Header.Get("Fn-Invoke-Type") == models.TypeDetached {
opts = append(opts, agent.InvokeDetached())
}
if trig != nil {
opts = append(opts, agent.WithTrigger(trig))
}
return opts
}