fix(server): reduce kill delay and implement port retry mechanism
- Changed the kill delay from 30 seconds to 2 seconds in the configuration. - Refactored the server startup process to include a retry mechanism for binding to the port, addressing potential issues during hot-reload scenarios. - Added a new function, listenWithRetry, to handle port binding with exponential backoff, improving server reliability during restarts.
This commit is contained in:
@@ -17,7 +17,7 @@ tmp_dir = "tmp"
|
|||||||
include_dir = []
|
include_dir = []
|
||||||
include_ext = ["go", "tpl", "tmpl", "html", "yaml"]
|
include_ext = ["go", "tpl", "tmpl", "html", "yaml"]
|
||||||
include_file = []
|
include_file = []
|
||||||
kill_delay = "30s"
|
kill_delay = "2s"
|
||||||
log = "build-errors.log"
|
log = "build-errors.log"
|
||||||
poll = false
|
poll = false
|
||||||
poll_interval = 0
|
poll_interval = 0
|
||||||
|
|||||||
+31
-4
@@ -25,6 +25,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@@ -61,7 +62,6 @@ func main() {
|
|||||||
) error {
|
) error {
|
||||||
// Create HTTP server
|
// Create HTTP server
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port),
|
|
||||||
Handler: router,
|
Handler: router,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,11 +94,16 @@ func main() {
|
|||||||
done()
|
done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Start server
|
// Start server with retry to handle port not yet released during hot-reload
|
||||||
logger.Infof(context.Background(), "Server is running at %s:%d", cfg.Server.Host, cfg.Server.Port)
|
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
listener, err := listenWithRetry(addr, 10, 300*time.Millisecond)
|
||||||
|
if err != nil {
|
||||||
return fmt.Errorf("failed to start server: %v", err)
|
return fmt.Errorf("failed to start server: %v", err)
|
||||||
}
|
}
|
||||||
|
logger.Infof(context.Background(), "Server is running at %s", addr)
|
||||||
|
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
|
||||||
|
return fmt.Errorf("server error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for shutdown signal
|
// Wait for shutdown signal
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
@@ -108,3 +113,25 @@ func main() {
|
|||||||
logger.Fatalf(context.Background(), "Failed to run application: %v", err)
|
logger.Fatalf(context.Background(), "Failed to run application: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// listenWithRetry retries net.Listen with exponential backoff,
|
||||||
|
// useful during hot-reload when the previous process may not have released the port yet.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user