Skip to content

Commit 04f6d63

Browse files
committed
Register signal SIGUSR1 to logging connection number or release resource
Signed-off-by: Chenyang Yan <[email protected]>
1 parent 26c419f commit 04f6d63

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ gotty
22
builds
33
js/dist
44
js/node_modules/*
5+
.idea/*

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ To share your current session with others by a shortcut key, you can add a line
141141
bind-key C-t new-window "gotty tmux attach -t `tmux display -p '#S'`"
142142
```
143143

144+
### Signal Notify
145+
146+
GoTTY have registered `SIGINT`, `SIGTERM` and `SIGUSR1` signal.
147+
148+
* `SIGINT`, `SIGTERM`: will terminate process or terminate it gracefully.
149+
* `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.
150+
144151
## Playing with Docker
145152

146153
When you want to create a jailed environment for each client, you can use Docker containers like following:

server/server.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import (
1010
"log"
1111
"net"
1212
"net/http"
13+
"os"
14+
"os/signal"
1315
"regexp"
1416
"strings"
17+
"syscall"
1518
noesctmpl "text/template"
1619
"time"
1720

@@ -177,6 +180,24 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error {
177180
}
178181
}()
179182

183+
signalUsr1 := make(chan os.Signal)
184+
signal.Notify(signalUsr1, syscall.SIGUSR1)
185+
go func() {
186+
for {
187+
value := <-signalUsr1
188+
number := counter.count()
189+
if number != 0 {
190+
log.Printf("signal: %s current connection number: %d/%d", value.String(), number, server.options.MaxConnection)
191+
continue
192+
}
193+
log.Printf("signal: %s current connection number is 0 and then will interrupt process", value.String())
194+
e := syscall.Kill(os.Getpid(), syscall.SIGINT)
195+
if e != nil && !errors.Is(e, syscall.ESRCH) {
196+
log.Printf("signal: %s kill process error: %s", syscall.SIGINT.String(), e)
197+
}
198+
break
199+
}
200+
}()
180201
select {
181202
case err = <-srvErr:
182203
if err == http.ErrServerClosed { // by gracefull ctx

0 commit comments

Comments
 (0)