Package HTTPServer:

- Adjust delay to freeing port / checking alive with slow system
This commit is contained in:
nabbar
2025-12-23 17:40:42 +01:00
parent fa8adbe7c8
commit eac01ac6c7
5 changed files with 9 additions and 29 deletions

View File

@@ -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 {

View File

@@ -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()

View File

@@ -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.

View File

@@ -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
}

View File

@@ -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() {