mirror of
https://github.com/hanzo-ml/kuberay.git
synced 2026-10-30 19:48:47 +00:00
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
const healthCheckPath = "/-/healthz"
|
|
|
|
type RayHttpProxyClientInterface interface {
|
|
InitClient()
|
|
CheckHealth() error
|
|
SetHostIp(hostIp string, port int)
|
|
}
|
|
|
|
// GetRayHttpProxyClientFunc Used for unit tests.
|
|
var GetRayHttpProxyClientFunc = GetRayHttpProxyClient
|
|
|
|
func GetRayHttpProxyClient() RayHttpProxyClientInterface {
|
|
return &RayHttpProxyClient{}
|
|
}
|
|
|
|
type RayHttpProxyClient struct {
|
|
client http.Client
|
|
httpProxyURL string
|
|
}
|
|
|
|
func (r *RayHttpProxyClient) InitClient() {
|
|
r.client = http.Client{
|
|
Timeout: 20 * time.Millisecond,
|
|
}
|
|
}
|
|
|
|
func (r *RayHttpProxyClient) SetHostIp(hostIp string, port int) {
|
|
r.httpProxyURL = fmt.Sprintf("http://%s:%d", hostIp, port)
|
|
}
|
|
|
|
func (r *RayHttpProxyClient) CheckHealth() error {
|
|
req, err := http.NewRequest("GET", r.httpProxyURL+healthCheckPath, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := r.client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
return fmt.Errorf("RayHttpProxyClient CheckHealth fail: %s %s", resp.Status, string(body))
|
|
}
|
|
|
|
return nil
|
|
}
|