Make server timeout configurable at compile time

This commit is contained in:
2025-05-19 20:22:46 -04:00
parent 8907fdc36e
commit 4210673b84
3 changed files with 9 additions and 6 deletions
+2
View File
@@ -8,6 +8,8 @@ import (
"github.com/rwinkhart/rcw/wrappers" "github.com/rwinkhart/rcw/wrappers"
) )
var Timeout = 300 // seconds for RPC server timeout; configurable
var daemonHash []byte var daemonHash []byte
var globalPassphrase []byte var globalPassphrase []byte
+3 -3
View File
@@ -47,14 +47,14 @@ func Start(passphrase []byte) {
os.Exit(0) os.Exit(0)
}() }()
// accept connections (timeout after 3 minutes of inactivity) // accept connections until Timeout takes effect
for { for {
listener.(*net.UnixListener).SetDeadline(time.Now().Add(3 * time.Minute)) listener.(*net.UnixListener).SetDeadline(time.Now().Add(time.Duration(Timeout) * time.Second))
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
if err.(net.Error).Timeout() { if err.(net.Error).Timeout() {
log.Println("Three minutes have passed without any connections. Exiting...") log.Println(strconv.Itoa(Timeout) + " seconds have passed without any connections. Exiting...")
listener.Close() listener.Close()
os.Exit(0) os.Exit(0)
} }
+4 -3
View File
@@ -9,6 +9,7 @@ import (
"net/rpc" "net/rpc"
"os" "os"
"os/signal" "os/signal"
"strconv"
"syscall" "syscall"
"time" "time"
@@ -54,13 +55,13 @@ func Start(passphrase []byte) {
// capture sigterms to ensure listener is closed // capture sigterms to ensure listener is closed
sigChan := make(chan os.Signal, 1) sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
// create 3-minute inactivity timer // create inactivity timer
timer := time.NewTimer(3 * time.Minute) timer := time.NewTimer(time.Duration(Timeout) * time.Second)
killTimer := make(chan struct{}) killTimer := make(chan struct{})
go func() { go func() {
select { select {
case <-timer.C: case <-timer.C:
log.Println("Three minutes have passed without any connections. Exiting...") log.Println(strconv.Itoa(Timeout) + " seconds have passed without any connections. Exiting...")
listener.Close() listener.Close()
os.Exit(0) os.Exit(0)
case <-killTimer: case <-killTimer: