feat: add server listening functionality with retry mechanism and platform-specific shutdown signal handling for improved robustness

This commit is contained in:
wizardchen
2026-04-11 19:08:17 +08:00
committed by lyingbug
parent 4af7885d13
commit ae95cb1a7a
5 changed files with 52 additions and 3 deletions
+29
View File
@@ -0,0 +1,29 @@
package main
import (
"context"
"net"
"time"
"github.com/Tencent/WeKnora/internal/logger"
)
func listenWithRetry(addr string, maxRetries int, baseDelay time.Duration) (net.Listener, error) {
var lastErr error
for i := 0; i < maxRetries; i++ {
listener, err := net.Listen("tcp", addr)
if err == nil {
return listener, nil
}
lastErr = err
if i < maxRetries-1 {
delay := baseDelay * time.Duration(1<<uint(i))
if delay > 3*time.Second {
delay = 3 * time.Second
}
logger.Warnf(context.Background(), "Port %s in use, retrying in %v... (%d/%d)", addr, delay, i+1, maxRetries)
time.Sleep(delay)
}
}
return nil, lastErr
}
+10
View File
@@ -0,0 +1,10 @@
//go:build !windows
package main
import (
"os"
"syscall"
)
var shutdownSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP}
+10
View File
@@ -0,0 +1,10 @@
//go:build windows
package main
import (
"os"
"syscall"
)
var shutdownSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM}