mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
fn: user friendly timeout handling changes (#1021)
* fn: user friendly timeout handling changes Timeout setting in routes now means "maximum amount of time a function can run in a container". Total wait time for a given http request is now expected to be handled by the client. As long as the client waits, the LB, runner or agents will search for resources to schedule it.
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/fnproject/fn/api/models"
|
||||
"github.com/go-openapi/strfmt"
|
||||
@@ -79,16 +78,8 @@ func (ci callInfoImpl) Input() io.Reader {
|
||||
func (ci callInfoImpl) Deadline() strfmt.DateTime {
|
||||
deadline, ok := ci.req.Context().Deadline()
|
||||
if !ok {
|
||||
// In theory deadline must have been set here, but if it wasn't then
|
||||
// at this point it is already too late to raise an error. Set it to
|
||||
// something meaningful.
|
||||
// This assumes StartedAt was set to something other than the default.
|
||||
// If that isn't set either, then how many things have gone wrong?
|
||||
if ci.call.StartedAt == strfmt.NewDateTime() {
|
||||
// We just panic if StartedAt is the default (i.e. not set)
|
||||
panic("No context deadline and zero-value StartedAt - this should never happen")
|
||||
}
|
||||
deadline = ((time.Time)(ci.call.StartedAt)).Add(time.Duration(ci.call.Timeout) * time.Second)
|
||||
// In theory deadline must have been set here
|
||||
panic("No context deadline is set in protocol, should never happen")
|
||||
}
|
||||
return strfmt.DateTime(deadline)
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@ package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/fnproject/fn/api/models"
|
||||
)
|
||||
@@ -16,7 +18,7 @@ type RequestData struct {
|
||||
A string `json:"a"`
|
||||
}
|
||||
|
||||
func setupRequest(data interface{}) *callInfoImpl {
|
||||
func setupRequest(data interface{}) (*callInfoImpl, context.CancelFunc) {
|
||||
req := &http.Request{
|
||||
Method: http.MethodPost,
|
||||
URL: &url.URL{
|
||||
@@ -46,12 +48,14 @@ func setupRequest(data interface{}) *callInfoImpl {
|
||||
// fixup URL in models.Call
|
||||
call.URL = req.URL.String()
|
||||
|
||||
ci := &callInfoImpl{call: call, req: req}
|
||||
return ci
|
||||
ctx, cancel := context.WithTimeout(req.Context(), 1*time.Second)
|
||||
ci := &callInfoImpl{call: call, req: req.WithContext(ctx)}
|
||||
return ci, cancel
|
||||
}
|
||||
|
||||
func TestJSONProtocolwriteJSONInputRequestBasicFields(t *testing.T) {
|
||||
ci := setupRequest(nil)
|
||||
ci, cancel := setupRequest(nil)
|
||||
defer cancel()
|
||||
r, w := io.Pipe()
|
||||
proto := JSONProtocol{w, r}
|
||||
go func() {
|
||||
@@ -88,7 +92,8 @@ func TestJSONProtocolwriteJSONInputRequestBasicFields(t *testing.T) {
|
||||
|
||||
func TestJSONProtocolwriteJSONInputRequestWithData(t *testing.T) {
|
||||
rDataBefore := RequestData{A: "a"}
|
||||
ci := setupRequest(rDataBefore)
|
||||
ci, cancel := setupRequest(rDataBefore)
|
||||
defer cancel()
|
||||
r, w := io.Pipe()
|
||||
proto := JSONProtocol{w, r}
|
||||
go func() {
|
||||
@@ -133,7 +138,8 @@ func TestJSONProtocolwriteJSONInputRequestWithData(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestJSONProtocolwriteJSONInputRequestWithoutData(t *testing.T) {
|
||||
ci := setupRequest(nil)
|
||||
ci, cancel := setupRequest(nil)
|
||||
defer cancel()
|
||||
r, w := io.Pipe()
|
||||
proto := JSONProtocol{w, r}
|
||||
go func() {
|
||||
@@ -177,7 +183,8 @@ func TestJSONProtocolwriteJSONInputRequestWithoutData(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestJSONProtocolwriteJSONInputRequestWithQuery(t *testing.T) {
|
||||
ci := setupRequest(nil)
|
||||
ci, cancel := setupRequest(nil)
|
||||
defer cancel()
|
||||
r, w := io.Pipe()
|
||||
proto := JSONProtocol{w, r}
|
||||
go func() {
|
||||
|
||||
Reference in New Issue
Block a user