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.(net.Error).Timeout() {
log.Println("Three minutes have passed without any connections. Exiting...")
listener.Close()
os.Exit(0)
}
log.Printf("Accept error: %v", err)
+20 -8
View File
@@ -8,6 +8,7 @@ import (
"net"
"net/rpc"
"os"
"time"
"github.com/Microsoft/go-winio" // For Windows named pipes
"github.com/rwinkhart/peercred-mini"
@@ -44,21 +45,32 @@ func Run() {
defer listener.Close()
log.Printf("RPC daemon listening on %s", socketPath)
// accept connections (timeout after 3 minutes of inactivity)
for {
// set deadline for accepting new connections
//listener.SetDeadline(time.Now().Add(3 * time.Minute)) TODO FIX
// create 3-minute inactivity timer
timer := time.NewTimer(3 * time.Minute)
killTimer := make(chan struct{})
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()
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)
close(killTimer)
continue
}
// reset timer after connection is accepted
timer.Reset(3 * time.Minute)
// use a goroutine to check the client's identity
go handleConn(conn)
}