mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-09 13:02:33 +02:00
fix check health for tcp/udp, refactor
This commit is contained in:
parent
1a93df5886
commit
fd50f8fcab
3 changed files with 14 additions and 5 deletions
|
@ -1,9 +1,11 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
apiUtils "github.com/yusing/go-proxy/internal/api/v1/utils"
|
apiUtils "github.com/yusing/go-proxy/internal/api/v1/utils"
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
"github.com/yusing/go-proxy/internal/net/types"
|
||||||
|
@ -51,6 +53,14 @@ func CheckHealth(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
hasPort := strings.Contains(host, ":")
|
||||||
|
port := query.Get("port")
|
||||||
|
if port != "" && !hasPort {
|
||||||
|
host = fmt.Sprintf("%s:%s", host, port)
|
||||||
|
} else {
|
||||||
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
result, err = monitor.NewRawHealthChecker(types.NewURL(&url.URL{
|
result, err = monitor.NewRawHealthChecker(types.NewURL(&url.URL{
|
||||||
Scheme: scheme,
|
Scheme: scheme,
|
||||||
Host: host,
|
Host: host,
|
||||||
|
|
|
@ -198,7 +198,7 @@ func TestCheckHealthTCPUDP(t *testing.T) {
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
query := url.Values{}
|
query := url.Values{}
|
||||||
query.Set("scheme", "tcp")
|
query.Set("scheme", tt.scheme)
|
||||||
query.Set("host", tt.host)
|
query.Set("host", tt.host)
|
||||||
query.Set("port", strconv.Itoa(tt.port))
|
query.Set("port", strconv.Itoa(tt.port))
|
||||||
|
|
||||||
|
|
|
@ -36,14 +36,13 @@ func (mon *RawHealthMonitor) CheckHealth() (result *health.HealthCheckResult, er
|
||||||
url := mon.url.Load()
|
url := mon.url.Load()
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
conn, dialErr := mon.dialer.DialContext(ctx, url.Scheme, url.Host)
|
conn, dialErr := mon.dialer.DialContext(ctx, url.Scheme, url.Host)
|
||||||
result = &health.HealthCheckResult{
|
result = new(health.HealthCheckResult)
|
||||||
Latency: time.Since(start),
|
|
||||||
}
|
|
||||||
if dialErr != nil {
|
if dialErr != nil {
|
||||||
result.Detail = dialErr.Error()
|
result.Detail = dialErr.Error()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.Close()
|
result.Latency = time.Since(start)
|
||||||
result.Healthy = true
|
result.Healthy = true
|
||||||
|
conn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue