diff --git a/setup/base/base.go b/setup/base/base.go index 5cbd7da9c..a37902f80 100644 --- a/setup/base/base.go +++ b/setup/base/base.go @@ -92,6 +92,7 @@ type BaseDendrite struct { const NoListener = "" const HTTPServerTimeout = time.Minute * 5 +const HTTPServerRequestTimeout = HTTPServerTimeout const HTTPClientTimeout = time.Second * 30 type BaseDendriteOptions int @@ -384,6 +385,7 @@ func (b *BaseDendrite) SetupAndServeHTTP( externalAddr, _ := externalHTTPAddr.Address() externalRouter := mux.NewRouter().SkipClean(true).UseEncodedPath() + externalRouter.Use(timeoutMiddleware) internalRouter := externalRouter externalServ := &http.Server{ @@ -521,6 +523,15 @@ func (b *BaseDendrite) SetupAndServeHTTP( logrus.Infof("Stopped HTTP listeners") } +// timeoutMiddleware is a Gorilla middleware that adds a timeout to all request contexts. +func timeoutMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithTimeout(r.Context(), HTTPServerRequestTimeout) + defer cancel() + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} + func (b *BaseDendrite) WaitForShutdown() { sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)