Skip to content

Commit

Permalink
Merge pull request #156 from devopsfaith/endpoint_timeout
Browse files Browse the repository at this point in the history
Fix: endpoint timeouts
  • Loading branch information
kpacha committed Oct 1, 2018
2 parents 95c06d9 + 34a5472 commit d0a32a9
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 8 deletions.
4 changes: 1 addition & 3 deletions router/gin/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/gin-gonic/gin"

Expand All @@ -24,14 +23,13 @@ func EndpointHandler(configuration *config.EndpointConfig, proxy proxy.Proxy) gi

// CustomErrorEndpointHandler implements the HandleFactory interface
func CustomErrorEndpointHandler(configuration *config.EndpointConfig, prxy proxy.Proxy, errF router.ToHTTPError) gin.HandlerFunc {
endpointTimeout := time.Duration(configuration.Timeout) * time.Millisecond
cacheControlHeaderValue := fmt.Sprintf("public, max-age=%d", int(configuration.CacheTTL.Seconds()))
isCacheEnabled := configuration.CacheTTL.Seconds() != 0
requestGenerator := NewRequest(configuration.HeadersToPass)
render := getRender(configuration)

return func(c *gin.Context) {
requestCtx, cancel := context.WithTimeout(c, endpointTimeout)
requestCtx, cancel := context.WithTimeout(c, configuration.Timeout)

c.Header(core.KrakendHeaderName, core.KrakendHeaderValue)

Expand Down
2 changes: 1 addition & 1 deletion router/gin/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestEndpointHandler_cancel(t *testing.T) {
}

func TestEndpointHandler_noop(t *testing.T) {
testEndpointHandler(t, 10, proxy.NoopProxy, "{}", "", "application/json; charset=utf-8", http.StatusOK, false)
testEndpointHandler(t, time.Minute, proxy.NoopProxy, "{}", "", "application/json; charset=utf-8", http.StatusOK, false)
}

func testEndpointHandler(t *testing.T, timeout time.Duration, p proxy.Proxy, expectedBody, expectedCache,
Expand Down
4 changes: 1 addition & 3 deletions router/mux/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"time"

"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/core"
Expand All @@ -27,7 +26,6 @@ func CustomEndpointHandler(rb RequestBuilder) HandlerFactory {
// CustomEndpointHandlerWithHTTPError returns a HandlerFactory with the received RequestBuilder
func CustomEndpointHandlerWithHTTPError(rb RequestBuilder, errF router.ToHTTPError) HandlerFactory {
return func(configuration *config.EndpointConfig, prxy proxy.Proxy) http.HandlerFunc {
endpointTimeout := time.Duration(configuration.Timeout) * time.Millisecond
cacheControlHeaderValue := fmt.Sprintf("public, max-age=%d", int(configuration.CacheTTL.Seconds()))
isCacheEnabled := configuration.CacheTTL.Seconds() != 0
render := getRender(configuration)
Expand All @@ -45,7 +43,7 @@ func CustomEndpointHandlerWithHTTPError(rb RequestBuilder, errF router.ToHTTPErr
return
}

requestCtx, cancel := context.WithTimeout(context.Background(), endpointTimeout)
requestCtx, cancel := context.WithTimeout(context.Background(), configuration.Timeout)

response, err := prxy(requestCtx, rb(r, configuration.QueryString, headersToSend))

Expand Down
2 changes: 1 addition & 1 deletion router/mux/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestEndpointHandler_cancelEmpty(t *testing.T) {
}

func TestEndpointHandler_noop(t *testing.T) {
testEndpointHandler(t, 10, proxy.NoopProxy, "GET", "{}", "", "application/json", http.StatusOK, false)
testEndpointHandler(t, time.Minute, proxy.NoopProxy, "GET", "{}", "", "application/json", http.StatusOK, false)
time.Sleep(5 * time.Millisecond)
}

Expand Down

0 comments on commit d0a32a9

Please sign in to comment.