diff options
| author | Shulhan <ms@kilabit.info> | 2026-02-11 21:43:11 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-02-11 21:43:11 +0700 |
| commit | 5b41090e6c523a1e66047329e51c2b0db280a581 (patch) | |
| tree | 10635c88bc6510a7405259199fa6f99675936240 /lib/http/server_test.go | |
| parent | 37c0e3f259204842c1ac82a04effb6c12e76078d (diff) | |
| download | pakakeh.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.go | 23 |
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) + } +} |
