aboutsummaryrefslogtreecommitdiff
path: root/lib/http/server_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2026-02-11 21:43:11 +0700
committerShulhan <ms@kilabit.info>2026-02-11 21:43:11 +0700
commit5b41090e6c523a1e66047329e51c2b0db280a581 (patch)
tree10635c88bc6510a7405259199fa6f99675936240 /lib/http/server_test.go
parent37c0e3f259204842c1ac82a04effb6c12e76078d (diff)
downloadpakakeh.go-5b41090e6c523a1e66047329e51c2b0db280a581.tar.xz
lib/http: implement server auto shutdown when idle
In the `ServerOptions`, we add option `ShutdownIdleDuration` when set to non-zero value it will start a timer. When the timer expired, the server will stop accepting new connection and then shutting down. This allow de-activating HTTP server when no connections received after specific duration to reduce the system resources.
Diffstat (limited to 'lib/http/server_test.go')
-rw-r--r--lib/http/server_test.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/http/server_test.go b/lib/http/server_test.go
index a333628c..0bdcce36 100644
--- a/lib/http/server_test.go
+++ b/lib/http/server_test.go
@@ -1569,3 +1569,26 @@ func runServerFS(t *testing.T, address, dir string) (srv *Server) {
return srv
}
+
+func TestServer_ShutdownIdleDuration(t *testing.T) {
+ opts := ServerOptions{
+ Address: `127.0.0.1:21000`,
+ ShutdownIdleDuration: 500 * time.Millisecond,
+ }
+ srv, err := NewServer(opts)
+ if err != nil {
+ t.Fatal(err)
+ }
+ waitq := make(chan struct{}, 1)
+ waitTimer := time.NewTimer(time.Second)
+ go func() {
+ err = srv.Start()
+ waitq <- struct{}{}
+ }()
+ select {
+ case <-waitq:
+ // Server shutdown after idle.
+ case <-waitTimer.C:
+ t.Fatalf(`expecting shutdown after %v`, opts.ShutdownIdleDuration)
+ }
+}