aboutsummaryrefslogtreecommitdiff
path: root/internal/middleware/timeout/timeout.go
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2026-02-16 15:22:59 +0700
committerShulhan <m.shulhan@gmail.com>2026-03-26 22:52:19 +0700
commit0993f6bbf52f85f137afe17d1c05aa3fe7bd72cd (patch)
treed336cfa19e61768faedb1fe080cd93b557d553cd /internal/middleware/timeout/timeout.go
parentd2f190ed5db7bc5136a4eb8436b021ec014e18c7 (diff)
downloadgo-x-pkgsite-0993f6bbf52f85f137afe17d1c05aa3fe7bd72cd.tar.xz
[DO-NOT-MERGE] all: add option shutdown-idle
The shutdown-idle set the duration to automatically shutdown the HTTP server when no request after specific duration. This is to complement the socket based activation to minimize the resources on local environment.
Diffstat (limited to 'internal/middleware/timeout/timeout.go')
-rw-r--r--internal/middleware/timeout/timeout.go10
1 files changed, 10 insertions, 0 deletions
diff --git a/internal/middleware/timeout/timeout.go b/internal/middleware/timeout/timeout.go
index 93d992da..391919c4 100644
--- a/internal/middleware/timeout/timeout.go
+++ b/internal/middleware/timeout/timeout.go
@@ -10,11 +10,21 @@ import (
"time"
)
+var ShutdownIdleDuration time.Duration
+var ShutdownIdleTimer *time.Timer
+
// Timeout returns a new Middleware that times out each request after the given
// duration.
func Timeout(d time.Duration) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if ShutdownIdleTimer != nil {
+ ok := ShutdownIdleTimer.Reset(ShutdownIdleDuration)
+ if !ok {
+ // Timer had reached or closed
+ return
+ }
+ }
ctx, cancel := context.WithTimeout(r.Context(), d)
defer cancel()
h.ServeHTTP(w, r.WithContext(ctx))