feat: v0.4.0 simplify
This commit is contained in:
28
checks.go
28
checks.go
@@ -35,32 +35,32 @@ func GetHTTP(addr string, timeout time.Duration) (CheckFunc, error) {
|
||||
timeout = DefaultTimeout
|
||||
}
|
||||
|
||||
return func(ctx context.Context) (Status, error) {
|
||||
return func(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
||||
if err != nil {
|
||||
return StatusUnknown, fmt.Errorf("failed to create http request: %w", err)
|
||||
return fmt.Errorf("failed to create http request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return StatusDown, fmt.Errorf("do request failed: %w", err)
|
||||
return fmt.Errorf("do request failed: %w", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return StatusDown, fmt.Errorf("err reading response body: %w", err)
|
||||
return fmt.Errorf("err reading response body: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return StatusDown, fmt.Errorf("got HTTP response code %d, body: %s", resp.StatusCode, string(body))
|
||||
return fmt.Errorf("got HTTP response code %d, body: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
return StatusOK, nil
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -83,27 +83,27 @@ func HeadHTTP(addr string, timeout time.Duration) (CheckFunc, error) {
|
||||
timeout = DefaultTimeout
|
||||
}
|
||||
|
||||
return func(ctx context.Context) (Status, error) {
|
||||
return func(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, u.String(), nil)
|
||||
if err != nil {
|
||||
return StatusUnknown, fmt.Errorf("failed to create http request: %w", err)
|
||||
return fmt.Errorf("failed to create http request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return StatusDown, fmt.Errorf("do request failed: %w", err)
|
||||
return fmt.Errorf("do request failed: %w", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return StatusDown, fmt.Errorf("got HTTP response code %d", resp.StatusCode)
|
||||
return fmt.Errorf("got HTTP response code %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return StatusOK, nil
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ func DialTCP(addr string, timeout time.Duration) (CheckFunc, error) {
|
||||
timeout = DefaultTimeout
|
||||
}
|
||||
|
||||
return func(ctx context.Context) (Status, error) {
|
||||
return func(ctx context.Context) error {
|
||||
deadline := time.Now().Add(timeout)
|
||||
if t, ok := ctx.Deadline(); ok && t.Before(deadline) {
|
||||
deadline = t
|
||||
@@ -129,11 +129,11 @@ func DialTCP(addr string, timeout time.Duration) (CheckFunc, error) {
|
||||
|
||||
conn, err := net.DialTimeout("tcp", addr, time.Until(deadline))
|
||||
if err != nil {
|
||||
return StatusDown, fmt.Errorf("error dialing: %w", err)
|
||||
return fmt.Errorf("error dialing: %w", err)
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
return StatusOK, nil
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user