Register signal SIGUSR1 to logging connection number or release resource

Signed-off-by: Chenyang Yan <memory.yancy@gmail.com>
This commit is contained in:
Chenyang Yan 2022-09-03 20:42:33 +08:00
parent 26c419f564
commit 04f6d63a8c
3 changed files with 29 additions and 0 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ gotty
builds
js/dist
js/node_modules/*
.idea/*

View File

@ -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:

View File

@ -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