Merge 04f6d63a8c8c938aa89e01d022def5678b6285a7 into bf3eae1d9a7c882dc80888ce7374a5c47608c2dd

This commit is contained in:
uddmorningsun 2023-09-01 20:18:40 -07:00 committed by GitHub
commit 002da2d15e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

@ -143,6 +143,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