Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to get current number of WebSocket connections #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ gotty
builds
js/dist
js/node_modules/*
.idea/*
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
"log"
"net"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
noesctmpl "text/template"
"time"

Expand Down Expand Up @@ -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
Expand Down