diff --git a/httpserver/monitor.go b/httpserver/monitor.go index d83cbac..ea5ecca 100644 --- a/httpserver/monitor.go +++ b/httpserver/monitor.go @@ -30,16 +30,13 @@ import ( "context" "errors" "fmt" - "net" "runtime" - "time" logent "github.com/nabbar/golib/logger/entry" loglvl "github.com/nabbar/golib/logger/level" libmon "github.com/nabbar/golib/monitor" moninf "github.com/nabbar/golib/monitor/info" montps "github.com/nabbar/golib/monitor/types" - libptc "github.com/nabbar/golib/network/protocol" libver "github.com/nabbar/golib/version" ) @@ -79,7 +76,7 @@ func (o *srv) HealthCheck(ctx context.Context) error { } else if e := r.ErrorsLast(); e != nil { fl(e) return e - } else if e = o.runAndHealthy(ctx); e != nil { + } else if e = PortNotUse(ctx, o.GetBindable()); e != nil { fl(e) return e } else { @@ -88,24 +85,6 @@ func (o *srv) HealthCheck(ctx context.Context) error { } } -func (o *srv) runAndHealthy(ctx context.Context) error { - x, n := context.WithTimeout(ctx, 50*time.Microsecond) - defer n() - - if e := PortNotUse(ctx, o.GetBindable()); e != nil { - return e - } else { - d := &net.Dialer{} - co, ce := d.DialContext(x, libptc.NetworkTCP.Code(), o.GetBindable()) - defer func() { - if co != nil { - _ = co.Close() - } - }() - return ce - } -} - // MonitorName returns the unique monitoring identifier for this server instance. // The identifier includes the server's bind address for uniqueness. func (o *srv) MonitorName() string { diff --git a/httpserver/tools.go b/httpserver/tools.go index 4be1a19..c92a61c 100644 --- a/httpserver/tools.go +++ b/httpserver/tools.go @@ -30,6 +30,7 @@ import ( "context" "net" "strings" + "time" liberr "github.com/nabbar/golib/errors" srvtps "github.com/nabbar/golib/httpserver/types" @@ -62,11 +63,11 @@ func PortNotUse(ctx context.Context, listen string) error { addr := strings.Join(part[:len(part)-1], ":") if strings.HasPrefix(addr, "0") || strings.HasPrefix(addr, "::") { - listen = "127.0.0.1:" + port + listen = "0.0.0.0:" + port } } - if _, ok := ctx.Deadline(); !ok { + if t, ok := ctx.Deadline(); !ok || t.IsZero() || -time.Since(t) < time.Second { var cnl context.CancelFunc ctx, cnl = context.WithTimeout(ctx, srvtps.TimeoutWaitingPortFreeing) defer cnl() @@ -103,11 +104,11 @@ func PortInUse(ctx context.Context, listen string) liberr.Error { addr := strings.Join(part[:len(part)-1], ":") if strings.HasPrefix(addr, "0") || strings.HasPrefix(addr, "::") { - listen = "127.0.0.1:" + port + listen = "0.0.0.0:" + port } } - if _, ok := ctx.Deadline(); !ok { + if t, ok := ctx.Deadline(); !ok || t.IsZero() || -time.Since(t) < time.Second { var cnl context.CancelFunc ctx, cnl = context.WithTimeout(ctx, srvtps.TimeoutWaitingPortFreeing) defer cnl() diff --git a/httpserver/types/const.go b/httpserver/types/const.go index 0d1c549..fbfea7c 100644 --- a/httpserver/types/const.go +++ b/httpserver/types/const.go @@ -31,7 +31,7 @@ import "time" const ( // TimeoutWaitingPortFreeing is the timeout duration for checking if a port becomes available. // Used when verifying port availability before binding. - TimeoutWaitingPortFreeing = 250 * time.Microsecond + TimeoutWaitingPortFreeing = 3 * time.Second // TimeoutWaitingStop is the default timeout for graceful server shutdown. // Servers have 5 seconds to complete ongoing requests before forced termination. diff --git a/httpserver/types/example_test.go b/httpserver/types/example_test.go index 3ae5cee..9c9ae30 100644 --- a/httpserver/types/example_test.go +++ b/httpserver/types/example_test.go @@ -191,7 +191,7 @@ func Example_timeoutConstants() { fmt.Printf("Port freeing timeout: %v\n", portTimeout) fmt.Printf("Stop timeout: %v\n", stopTimeout) // Output: - // Port freeing timeout: 250µs + // Port freeing timeout: 3s // Stop timeout: 5s } diff --git a/httpserver/types/fields_test.go b/httpserver/types/fields_test.go index 80d947d..25b8024 100644 --- a/httpserver/types/fields_test.go +++ b/httpserver/types/fields_test.go @@ -108,7 +108,7 @@ var _ = Describe("[TC-FT] Field Types and Constants", func() { Describe("Timeout Constants", func() { It("[TC-FT-008] should define TimeoutWaitingPortFreeing", func() { - Expect(TimeoutWaitingPortFreeing).To(Equal(250 * time.Microsecond)) + Expect(TimeoutWaitingPortFreeing).To(Equal(3 * time.Second)) }) It("[TC-FT-007] should define TimeoutWaitingStop", func() {