mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 01:38:50 +02:00
refactor(health): optimize health checking
This commit is contained in:
@@ -25,9 +25,10 @@ type AgentConfig struct {
|
||||
Version pkg.Version `json:"version"`
|
||||
Runtime ContainerRuntime `json:"runtime"`
|
||||
|
||||
httpClient *http.Client
|
||||
tlsConfig *tls.Config
|
||||
l zerolog.Logger
|
||||
httpClient *http.Client
|
||||
httpClientHealthCheck *http.Client
|
||||
tlsConfig *tls.Config
|
||||
l zerolog.Logger
|
||||
} // @name Agent
|
||||
|
||||
const (
|
||||
@@ -104,6 +105,8 @@ func (cfg *AgentConfig) StartWithCerts(ctx context.Context, ca, crt, key []byte)
|
||||
|
||||
// create transport and http client
|
||||
cfg.httpClient = cfg.NewHTTPClient()
|
||||
cfg.httpClientHealthCheck = cfg.NewHTTPClient()
|
||||
applyHealthCheckTransportConfig(cfg.httpClientHealthCheck.Transport.(*http.Transport))
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
@@ -207,3 +210,12 @@ func (cfg *AgentConfig) DialContext(ctx context.Context) (net.Conn, error) {
|
||||
func (cfg *AgentConfig) String() string {
|
||||
return cfg.Name + "@" + cfg.Addr
|
||||
}
|
||||
|
||||
func applyHealthCheckTransportConfig(transport *http.Transport) {
|
||||
transport.DisableKeepAlives = true
|
||||
transport.DisableCompression = true
|
||||
transport.MaxIdleConns = 1
|
||||
transport.MaxIdleConnsPerHost = 1
|
||||
transport.ReadBufferSize = 1024
|
||||
transport.WriteBufferSize = 1024
|
||||
}
|
||||
|
||||
@@ -30,6 +30,24 @@ func (cfg *AgentConfig) Forward(req *http.Request, endpoint string) (*http.Respo
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (cfg *AgentConfig) DoHealthCheck(ctx context.Context, endpoint string) ([]byte, int, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", APIBaseURL+endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
req.Header.Set("Accept-Encoding", "identity")
|
||||
req.Header.Set("Connection", "close")
|
||||
|
||||
resp, err := cfg.httpClientHealthCheck.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, _ := io.ReadAll(resp.Body)
|
||||
return data, resp.StatusCode, nil
|
||||
}
|
||||
|
||||
func (cfg *AgentConfig) Fetch(ctx context.Context, endpoint string) ([]byte, int, error) {
|
||||
resp, err := cfg.Do(ctx, "GET", endpoint, nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user