Register signal SIGUSR1 to logging connection number or release resource
Signed-off-by: Chenyang Yan <memory.yancy@gmail.com>
This commit is contained in:
parent
26c419f564
commit
04f6d63a8c
|
@ -2,3 +2,4 @@ gotty
|
|||
builds
|
||||
js/dist
|
||||
js/node_modules/*
|
||||
.idea/*
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue