Skip to content

Commit

Permalink
timeout if configured
Browse files Browse the repository at this point in the history
error handle request timeout
  • Loading branch information
danthegoodman1 committed Dec 24, 2023
1 parent 3855843 commit d813bcf
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions http_server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ var handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return c.Str("proto", r.Proto)
})

ctx, cancel := context.WithTimeout(ctx, time.Second*time.Duration(utils.Env_HTTPTimeoutSec))
defer cancel()
if utils.Env_HTTPTimeoutSec > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(utils.Env_HTTPTimeoutSec))
defer cancel()
}

// Check for an ACME challenge
if strings.HasPrefix(r.URL.Path, ACMEPathPrefix) || strings.HasPrefix(r.URL.Path, ACMETestPathPrefix) || strings.HasPrefix(r.URL.Path, ZeroSSLPathPrefix) {
Expand Down Expand Up @@ -120,6 +123,13 @@ var handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return
}

if dest.TimeoutSec != nil {
// If we have a timeout override, set it
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(*dest.TimeoutSec))
defer cancel()
}

// Proxy the request
originReq, err := makeOriginRequest(ctx, fqdn, dest.URL+path, isTLS, r)
if err != nil {
Expand Down Expand Up @@ -361,13 +371,21 @@ func Shutdown(ctx context.Context) error {
return g.Wait()
}

// handles writing the error, should always return after calling this
// handles writing the error, should always return after calling this. Has overrides for common errors
// like context.DeadlineExceeded
func respondServerError(ctx context.Context, span trace.Span, w http.ResponseWriter, status int, e error, msg string) {
logger := zerolog.Ctx(ctx)
logger.Error().Err(e).Msg(msg)
w.WriteHeader(status)
span.SetAttributes(attribute.Int("status", status))
_, err := fmt.Fprint(w, "internal error")
logger := zerolog.Ctx(ctx)
var err error
if errors.Is(e, context.DeadlineExceeded) {
logger.Warn().Err(e).Msg("request deadline exceeded")
w.WriteHeader(http.StatusRequestTimeout)
_, err = fmt.Fprint(w, "internal error")
} else {
logger.Error().Err(e).Msg(msg)
w.WriteHeader(status)
_, err = fmt.Fprint(w, "internal error")
}
if err != nil {
logger.Error().Err(err).Msg("error writing internal error to HTTP request")
}
Expand Down

0 comments on commit d813bcf

Please sign in to comment.