-
Notifications
You must be signed in to change notification settings - Fork 316
/
httpproxy_httpclient.go
53 lines (43 loc) · 1.02 KB
/
httpproxy_httpclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package utils
import (
"fmt"
"io"
"net/http"
"time"
)
type RayHttpProxyClientInterface interface {
InitClient()
CheckHealth() error
SetHostIp(hostIp string, port int)
}
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+RayServeProxyHealthPath, 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
}