Skip to content

Commit

Permalink
Merge pull request joewalnes#212 from asergeyev/timetokill
Browse files Browse the repository at this point in the history
Adding --closems feature discussed in joewalnes#207
  • Loading branch information
asergeyev committed Jun 7, 2016
2 parents 709c499 + 47af58f commit 0e8dc6e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func parseCommandLine() *Config {
sslCert := flag.String("sslcert", "", "Should point to certificate PEM file when --ssl is used")
sslKey := flag.String("sslkey", "", "Should point to certificate private key file when --ssl is used")
maxForksFlag := flag.Int("maxforks", 0, "Max forks, zero means unlimited")
closeMsFlag := flag.Uint("closems", 0, "Time to start sending signals (0 never)")
redirPortFlag := flag.Int("redirport", 0, "HTTP port to redirect to canonical --port address")

// lib config options
Expand Down Expand Up @@ -133,6 +134,7 @@ func parseCommandLine() *Config {
config.HeadersWs = []string(headersWs)
config.HeadersHTTP = []string(headersHttp)

config.CloseMs = *closeMsFlag
config.Binary = *binaryFlag
config.ReverseLookup = *reverseLookupFlag
config.Ssl = *sslFlag
Expand Down
5 changes: 5 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ Options:
another process (unlimited when 0 or negative).
Default: 0
--closems=milliseconds Specifies additional time process needs to gracefully
finish before websocketd will send termination signals
to it. Default: 0 (signals sent after 100ms, 250ms,
and 500ms of waiting)
--header="..." Set custom HTTP header to each answer. For
example: --header="Server: someserver/0.0.1"
Expand Down
1 change: 1 addition & 0 deletions libwebsocketd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Config struct {
CommandName string // Command to execute.
CommandArgs []string // Additional args to pass to command.
ServerSoftware string // Value to pass to SERVER_SOFTWARE environment variable (e.g. websocketd/1.2.3).
CloseMs uint // Milliseconds to start sending signals

// settings
Binary bool // Use binary communication (send data in chunks they are read from process)
Expand Down
3 changes: 3 additions & 0 deletions libwebsocketd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (wsh *WebsocketdHandler) accept(ws *websocket.Conn, log *LogScope) {

binary := wsh.server.Config.Binary
process := NewProcessEndpoint(launched, binary, log)
if cms := wsh.server.Config.CloseMs; cms != 0 {
process.closetime += time.Duration(cms) * time.Millisecond
}
wsEndpoint := NewWebSocketEndpoint(ws, binary, log)

PipeEndpoints(process, wsEndpoint)
Expand Down
15 changes: 8 additions & 7 deletions libwebsocketd/process_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
)

type ProcessEndpoint struct {
process *LaunchedProcess
output chan []byte
log *LogScope
bin bool
process *LaunchedProcess
closetime time.Duration
output chan []byte
log *LogScope
bin bool
}

func NewProcessEndpoint(process *LaunchedProcess, bin bool, log *LogScope) *ProcessEndpoint {
Expand All @@ -40,7 +41,7 @@ func (pe *ProcessEndpoint) Terminate() {
case <-terminated:
pe.log.Debug("process", "Process %v terminated after stdin was closed", pe.process.cmd.Process.Pid)
return // means process finished
case <-time.After(100 * time.Millisecond):
case <-time.After(100*time.Millisecond + pe.closetime):
}

err := pe.process.cmd.Process.Signal(syscall.SIGINT)
Expand All @@ -53,7 +54,7 @@ func (pe *ProcessEndpoint) Terminate() {
case <-terminated:
pe.log.Debug("process", "Process %v terminated after SIGINT", pe.process.cmd.Process.Pid)
return // means process finished
case <-time.After(250 * time.Millisecond):
case <-time.After(250*time.Millisecond + pe.closetime):
}

err = pe.process.cmd.Process.Signal(syscall.SIGTERM)
Expand All @@ -66,7 +67,7 @@ func (pe *ProcessEndpoint) Terminate() {
case <-terminated:
pe.log.Debug("process", "Process %v terminated after SIGTERM", pe.process.cmd.Process.Pid)
return // means process finished
case <-time.After(500 * time.Millisecond):
case <-time.After(500*time.Millisecond + pe.closetime):
}

err = pe.process.cmd.Process.Kill()
Expand Down
2 changes: 1 addition & 1 deletion release/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LAST_PATCH_VERSION:=$(shell git ls-remote git@github.com:joewalnes/websocketd.gi
VERSION_PATCH:=$(if $(LAST_PATCH_VERSION),$(shell expr $(LAST_PATCH_VERSION)),0)
RELEASE_VERSION=$(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)

GO_VERSION=1.6
GO_VERSION=1.6.2
PLATFORMS=linux_amd64 linux_386 linux_arm linux_arm64 darwin_amd64 freebsd_amd64 freebsd_386 windows_386 windows_amd64 openbsd_386 openbsd_amd64 solaris_amd64


Expand Down

0 comments on commit 0e8dc6e

Please sign in to comment.