diff --git a/.gitignore b/.gitignore index 503eb0c..7905d23 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ gotty builds js/dist js/node_modules/* +.idea/* diff --git a/README.md b/README.md index e4a2f36..f3fd427 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,13 @@ To share your current session with others by a shortcut key, you can add a line bind-key C-t new-window "gotty tmux attach -t `tmux display -p '#S'`" ``` +### Signal Notify + +GoTTY have registered `SIGINT`, `SIGTERM` and `SIGUSR1` signal. + +* `SIGINT`, `SIGTERM`: will terminate process or terminate it gracefully. +* `SIGUSR1`: logging current connection number if activated number is not 0, or will send `SIGINT` signal. Especially useful in releasing resource according to your requirement. + ## Playing with Docker When you want to create a jailed environment for each client, you can use Docker containers like following: diff --git a/server/server.go b/server/server.go index c2c1a86..4544ec7 100644 --- a/server/server.go +++ b/server/server.go @@ -10,8 +10,11 @@ import ( "log" "net" "net/http" + "os" + "os/signal" "regexp" "strings" + "syscall" noesctmpl "text/template" "time" @@ -177,6 +180,24 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error { } }() + signalUsr1 := make(chan os.Signal) + signal.Notify(signalUsr1, syscall.SIGUSR1) + go func() { + for { + value := <-signalUsr1 + number := counter.count() + if number != 0 { + log.Printf("signal: %s current connection number: %d/%d", value.String(), number, server.options.MaxConnection) + continue + } + log.Printf("signal: %s current connection number is 0 and then will interrupt process", value.String()) + e := syscall.Kill(os.Getpid(), syscall.SIGINT) + if e != nil && !errors.Is(e, syscall.ESRCH) { + log.Printf("signal: %s kill process error: %s", syscall.SIGINT.String(), e) + } + break + } + }() select { case err = <-srvErr: if err == http.ErrServerClosed { // by gracefull ctx