Implement inactivity timer on Windows

This commit is contained in:
2025-04-12 19:53:02 -04:00
parent 1f6c25f541
commit a5108bc6cc
2 changed files with 21 additions and 8 deletions
+1
View File
@@ -47,6 +47,7 @@ func Run() {
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("Three minutes have passed without any connections. Exiting...")
listener.Close()
os.Exit(0) os.Exit(0)
} }
log.Printf("Accept error: %v", err) log.Printf("Accept error: %v", err)
+20 -8
View File
@@ -8,6 +8,7 @@ import (
"net" "net"
"net/rpc" "net/rpc"
"os" "os"
"time"
"github.com/Microsoft/go-winio" // For Windows named pipes "github.com/Microsoft/go-winio" // For Windows named pipes
"github.com/rwinkhart/peercred-mini" "github.com/rwinkhart/peercred-mini"
@@ -44,21 +45,32 @@ func Run() {
defer listener.Close() defer listener.Close()
log.Printf("RPC daemon listening on %s", socketPath) log.Printf("RPC daemon listening on %s", socketPath)
// accept connections (timeout after 3 minutes of inactivity) // create 3-minute inactivity timer
for { timer := time.NewTimer(3 * time.Minute)
// set deadline for accepting new connections killTimer := make(chan struct{})
//listener.SetDeadline(time.Now().Add(3 * time.Minute)) TODO FIX go func() {
select {
case <-timer.C:
log.Println("Three minutes have passed without any connections. Exiting...")
listener.Close()
os.Exit(0)
case <-killTimer:
return
}
}()
// accept connections
for {
conn, err := listener.Accept() conn, err := listener.Accept()
if err != nil { if err != nil {
if os.IsTimeout(err) {
log.Println("Three minutes have passed without any connections. Exiting...")
os.Exit(0)
}
log.Printf("Accept error: %v", err) log.Printf("Accept error: %v", err)
close(killTimer)
continue continue
} }
// reset timer after connection is accepted
timer.Reset(3 * time.Minute)
// use a goroutine to check the client's identity // use a goroutine to check the client's identity
go handleConn(conn) go handleConn(conn)
} }