mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
possible breakages: * `FN_HEADER` on cold are no longer `s/-/_/` -- this is so that cold functions can rebuild the headers as they were when they came in on the request (fdks, specifically), there's no guarantee that a reversal `s/_/-/` is the original header on the request. * app and route config no longer `s/-/_/` -- it seemed really weird to rewrite the users config vars on these. should just pass them exactly as is to env. * headers no longer contain the environment vars (previously, base config; app config, route config, `FN_PATH`, etc.), these are still available in the environment. this gets rid of a lot of the code around headers, specifically the stuff that shoved everything into headers when constructing a call to begin with. now we just store the headers separately and add a few things, like FN_CALL_ID to them, and build a separate 'config' now to store on the call. I thought 'config' was more aptly named, 'env' was confusing, though now 'config' is exactly what 'base_vars' was, which is only the things being put into the env. we weren't storing this field in the db, this doesn't break unless there are messages in a queue from another version, anyway, don't think we're there and don't expect any breakage for anybody with field name changes. this makes the configuration stuff pretty straight forward, there's just two separate buckets of things, and cold just needs to mash them together into the env, and otherwise hot containers just need to put 'config' in the env, and then hot format can shove 'headers' in however they'd like. this seems better than my last idea about making this easier but worse (RIP). this means: * headers no longer contain all vars, the set of base vars can only be found in the environment. * headers is only the headers from request + call_id, deadline, method, url * for cold, we simply add the headers to the environment, prepending `FN_HEADER_` to them, BUT NOT upper casing or `s/-/_/` * fixes issue where async hot functions would end up with `Fn_header_` prefixed headers * removes idea of 'base' vars and 'env'. this was a strange concept. now we just have 'config' which was base vars, and headers, which was base_env+headers; i.e. they are disjoint now. * casing for all headers will lean to be `My-Header` style, which should help with consistency. notable exceptions for cold only are FN_CALL_ID, FN_METHOD, and FN_REQUEST_URL -- this is simply to avoid breakage, in either hot format they appear as `Fn_call_id` still. * removes FN_PARAM stuff * updated doc with behavior weird things left: `Fn_call_id` e.g. isn't a correctly formatted http header, it should likely be `Fn-Call-Id` but I wanted to live to fight another day on this one, it would add some breakage. examples to be posted of each format below closes #329
117 lines
3.4 KiB
Go
117 lines
3.4 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"path"
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
// handleFunctionCall executes the function, for router handlers
|
|
func (s *Server) handleFunctionCall(c *gin.Context) {
|
|
err := s.handleFunctionCall2(c)
|
|
if err != nil {
|
|
handleErrorResponse(c, err)
|
|
}
|
|
}
|
|
|
|
// handleFunctionCall2 executes the function and returns an error
|
|
// Requires the following in the context:
|
|
// * "app_name"
|
|
// * "path"
|
|
func (s *Server) handleFunctionCall2(c *gin.Context) error {
|
|
ctx := c.Request.Context()
|
|
var p string
|
|
r := ctx.Value(api.Path)
|
|
if r == nil {
|
|
p = "/"
|
|
} else {
|
|
p = r.(string)
|
|
}
|
|
|
|
var a string
|
|
ai := ctx.Value(api.AppName)
|
|
if ai == nil {
|
|
err := models.ErrAppsMissingName
|
|
return err
|
|
}
|
|
a = ai.(string)
|
|
|
|
// gin sets this to 404 on NoRoute, so we'll just ensure it's 200 by default.
|
|
c.Status(200) // this doesn't write the header yet
|
|
|
|
return s.serve(c, a, path.Clean(p))
|
|
}
|
|
|
|
// TODO it would be nice if we could make this have nothing to do with the gin.Context but meh
|
|
// TODO make async store an *http.Request? would be sexy until we have different api format...
|
|
func (s *Server) serve(c *gin.Context, appName, path string) error {
|
|
// GetCall can mod headers, assign an id, look up the route/app (cached),
|
|
// strip params, etc.
|
|
call, err := s.agent.GetCall(
|
|
agent.WithWriter(c.Writer), // XXX (reed): order matters [for now]
|
|
agent.FromRequest(appName, path, c.Request),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
model := call.Model()
|
|
{ // scope this, to disallow ctx use outside of this scope. add id for handleErrorResponse logger
|
|
ctx, _ := common.LoggerWithFields(c.Request.Context(), logrus.Fields{"id": model.ID})
|
|
c.Request = c.Request.WithContext(ctx)
|
|
}
|
|
|
|
if model.Type == "async" {
|
|
// TODO we should push this into GetCall somehow (CallOpt maybe) or maybe agent.Queue(Call) ?
|
|
contentLength := c.Request.ContentLength
|
|
if contentLength < 128 { // contentLength could be -1 or really small, sanitize
|
|
contentLength = 128
|
|
}
|
|
buf := bytes.NewBuffer(make([]byte, int(contentLength))[:0]) // TODO sync.Pool me
|
|
_, err := buf.ReadFrom(c.Request.Body)
|
|
if err != nil {
|
|
return models.ErrInvalidPayload
|
|
}
|
|
model.Payload = buf.String()
|
|
|
|
// TODO idk where to put this, but agent is all runner really has...
|
|
err = s.agent.Enqueue(c.Request.Context(), model)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c.JSON(http.StatusAccepted, map[string]string{"call_id": model.ID})
|
|
return nil
|
|
}
|
|
|
|
err = s.agent.Submit(call)
|
|
if err != nil {
|
|
// NOTE if they cancel the request then it will stop the call (kind of cool),
|
|
// we could filter that error out here too as right now it yells a little
|
|
if err == models.ErrCallTimeoutServerBusy || err == models.ErrCallTimeout {
|
|
// TODO maneuver
|
|
// add this, since it means that start may not have been called [and it's relevant]
|
|
c.Writer.Header().Add("XXX-FXLB-WAIT", time.Now().Sub(time.Time(model.CreatedAt)).String())
|
|
}
|
|
// NOTE: if the task wrote the headers already then this will fail to write
|
|
// a 5xx (and log about it to us) -- that's fine (nice, even!)
|
|
return err
|
|
}
|
|
|
|
// TODO plumb FXLB-WAIT somehow (api?)
|
|
|
|
// TODO we need to watch the response writer and if no bytes written
|
|
// then write a 200 at this point?
|
|
// c.Data(http.StatusOK)
|
|
|
|
return nil
|
|
}
|