mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-09 04:52:35 +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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
apiUtils "github.com/yusing/go-proxy/internal/api/v1/utils"
|
||||
"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)
|
||||
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{
|
||||
Scheme: scheme,
|
||||
Host: host,
|
||||
|
|
|
@ -198,7 +198,7 @@ func TestCheckHealthTCPUDP(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
query := url.Values{}
|
||||
query.Set("scheme", "tcp")
|
||||
query.Set("scheme", tt.scheme)
|
||||
query.Set("host", tt.host)
|
||||
query.Set("port", strconv.Itoa(tt.port))
|
||||
|
||||
|
|
|
@ -36,14 +36,13 @@ func (mon *RawHealthMonitor) CheckHealth() (result *health.HealthCheckResult, er
|
|||
url := mon.url.Load()
|
||||
start := time.Now()
|
||||
conn, dialErr := mon.dialer.DialContext(ctx, url.Scheme, url.Host)
|
||||
result = &health.HealthCheckResult{
|
||||
Latency: time.Since(start),
|
||||
}
|
||||
result = new(health.HealthCheckResult)
|
||||
if dialErr != nil {
|
||||
result.Detail = dialErr.Error()
|
||||
return
|
||||
}
|
||||
conn.Close()
|
||||
result.Latency = time.Since(start)
|
||||
result.Healthy = true
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue