mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-06 20:10:11 +02:00
d586793169
- Fix DNS blocklists slow downloads, fix #3102 - Leave 35s timeout for updaters - Set timeouts to 1s for local calls - Set timeouts to 5s for LAN VPN calls and small external calls - Set timeouts to 10s external VPN API calls
47 lines
892 B
Go
47 lines
892 B
Go
package healthcheck
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
var ErrHTTPStatusNotOK = errors.New("HTTP response status is not OK")
|
|
|
|
type Client struct {
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func NewClient(httpClient *http.Client) *Client {
|
|
return &Client{
|
|
httpClient: httpClient,
|
|
}
|
|
}
|
|
|
|
func (c *Client) Check(ctx context.Context, url string) error {
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
defer cancel()
|
|
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
response, err := c.httpClient.Do(request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode == http.StatusOK {
|
|
return nil
|
|
}
|
|
b, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return fmt.Errorf("%w: %d %s: %s", ErrHTTPStatusNotOK,
|
|
response.StatusCode, response.Status, string(b))
|
|
}
|