mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
Package HTTPServer:
- Adjust delay to freeing port / checking alive with slow system
This commit is contained in:
@@ -30,16 +30,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
|
||||||
|
|
||||||
logent "github.com/nabbar/golib/logger/entry"
|
logent "github.com/nabbar/golib/logger/entry"
|
||||||
loglvl "github.com/nabbar/golib/logger/level"
|
loglvl "github.com/nabbar/golib/logger/level"
|
||||||
libmon "github.com/nabbar/golib/monitor"
|
libmon "github.com/nabbar/golib/monitor"
|
||||||
moninf "github.com/nabbar/golib/monitor/info"
|
moninf "github.com/nabbar/golib/monitor/info"
|
||||||
montps "github.com/nabbar/golib/monitor/types"
|
montps "github.com/nabbar/golib/monitor/types"
|
||||||
libptc "github.com/nabbar/golib/network/protocol"
|
|
||||||
libver "github.com/nabbar/golib/version"
|
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 {
|
} else if e := r.ErrorsLast(); e != nil {
|
||||||
fl(e)
|
fl(e)
|
||||||
return e
|
return e
|
||||||
} else if e = o.runAndHealthy(ctx); e != nil {
|
} else if e = PortNotUse(ctx, o.GetBindable()); e != nil {
|
||||||
fl(e)
|
fl(e)
|
||||||
return e
|
return e
|
||||||
} else {
|
} 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.
|
// MonitorName returns the unique monitoring identifier for this server instance.
|
||||||
// The identifier includes the server's bind address for uniqueness.
|
// The identifier includes the server's bind address for uniqueness.
|
||||||
func (o *srv) MonitorName() string {
|
func (o *srv) MonitorName() string {
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
liberr "github.com/nabbar/golib/errors"
|
liberr "github.com/nabbar/golib/errors"
|
||||||
srvtps "github.com/nabbar/golib/httpserver/types"
|
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], ":")
|
addr := strings.Join(part[:len(part)-1], ":")
|
||||||
|
|
||||||
if strings.HasPrefix(addr, "0") || strings.HasPrefix(addr, "::") {
|
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
|
var cnl context.CancelFunc
|
||||||
ctx, cnl = context.WithTimeout(ctx, srvtps.TimeoutWaitingPortFreeing)
|
ctx, cnl = context.WithTimeout(ctx, srvtps.TimeoutWaitingPortFreeing)
|
||||||
defer cnl()
|
defer cnl()
|
||||||
@@ -103,11 +104,11 @@ func PortInUse(ctx context.Context, listen string) liberr.Error {
|
|||||||
addr := strings.Join(part[:len(part)-1], ":")
|
addr := strings.Join(part[:len(part)-1], ":")
|
||||||
|
|
||||||
if strings.HasPrefix(addr, "0") || strings.HasPrefix(addr, "::") {
|
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
|
var cnl context.CancelFunc
|
||||||
ctx, cnl = context.WithTimeout(ctx, srvtps.TimeoutWaitingPortFreeing)
|
ctx, cnl = context.WithTimeout(ctx, srvtps.TimeoutWaitingPortFreeing)
|
||||||
defer cnl()
|
defer cnl()
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import "time"
|
|||||||
const (
|
const (
|
||||||
// TimeoutWaitingPortFreeing is the timeout duration for checking if a port becomes available.
|
// TimeoutWaitingPortFreeing is the timeout duration for checking if a port becomes available.
|
||||||
// Used when verifying port availability before binding.
|
// Used when verifying port availability before binding.
|
||||||
TimeoutWaitingPortFreeing = 250 * time.Microsecond
|
TimeoutWaitingPortFreeing = 3 * time.Second
|
||||||
|
|
||||||
// TimeoutWaitingStop is the default timeout for graceful server shutdown.
|
// TimeoutWaitingStop is the default timeout for graceful server shutdown.
|
||||||
// Servers have 5 seconds to complete ongoing requests before forced termination.
|
// Servers have 5 seconds to complete ongoing requests before forced termination.
|
||||||
|
|||||||
@@ -191,7 +191,7 @@ func Example_timeoutConstants() {
|
|||||||
fmt.Printf("Port freeing timeout: %v\n", portTimeout)
|
fmt.Printf("Port freeing timeout: %v\n", portTimeout)
|
||||||
fmt.Printf("Stop timeout: %v\n", stopTimeout)
|
fmt.Printf("Stop timeout: %v\n", stopTimeout)
|
||||||
// Output:
|
// Output:
|
||||||
// Port freeing timeout: 250µs
|
// Port freeing timeout: 3s
|
||||||
// Stop timeout: 5s
|
// Stop timeout: 5s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ var _ = Describe("[TC-FT] Field Types and Constants", func() {
|
|||||||
|
|
||||||
Describe("Timeout Constants", func() {
|
Describe("Timeout Constants", func() {
|
||||||
It("[TC-FT-008] should define TimeoutWaitingPortFreeing", 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() {
|
It("[TC-FT-007] should define TimeoutWaitingStop", func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user