A failed HTTPS check is retried (3 attempts, 2 s apart). If all attempts fail, the agent runs traceroute -4 -n to the target's IPv4 address in a separate process, polls it every 5 s (90 s limit) and posts the full output as a traceroute_result event for the address. The agent waits for traces before reporting completion, while the floating IP is attached. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package checkrunner
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// HTTPS performs a GET against target (expected to be a full URL, e.g.
|
|
// https://github.com) and reports success for any response with status
|
|
// < 400. Redirects are followed by the default http.Client policy.
|
|
// It makes a single attempt; use HTTPSWithRetry for retries.
|
|
func HTTPS(target string, timeout time.Duration) func(ctx context.Context) Result {
|
|
return run("https", target, func(ctx context.Context) error {
|
|
return httpsOnce(ctx, &http.Client{Timeout: timeout}, target, timeout)
|
|
})
|
|
}
|
|
|
|
func httpsOnce(ctx context.Context, client *http.Client, target string, timeout time.Duration) error {
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, target, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("build request: %w", err)
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode >= 400 {
|
|
return fmt.Errorf("unexpected status %d", resp.StatusCode)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// HTTPSWithRetry makes up to attempts tries, pausing delay between them, and
|
|
// stops at the first success. The result is a single Result: Detail carries
|
|
// the attempt number, LatencyMS is that of the last attempt.
|
|
func HTTPSWithRetry(target string, timeout time.Duration, attempts int, delay time.Duration) func(ctx context.Context) Result {
|
|
return httpsWithRetry(&http.Client{Timeout: timeout}, target, timeout, attempts, delay)
|
|
}
|
|
|
|
func httpsWithRetry(client *http.Client, target string, timeout time.Duration, attempts int, delay time.Duration) func(ctx context.Context) Result {
|
|
if attempts < 1 {
|
|
attempts = 1
|
|
}
|
|
return func(ctx context.Context) Result {
|
|
var res Result
|
|
for n := 1; n <= attempts; n++ {
|
|
res = run("https", target, func(ctx context.Context) error {
|
|
return httpsOnce(ctx, client, target, timeout)
|
|
})(ctx)
|
|
if res.Success {
|
|
res.Detail = fmt.Sprintf("ok (attempt %d/%d)", n, attempts)
|
|
return res
|
|
}
|
|
if n == attempts || ctx.Err() != nil {
|
|
break
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-time.After(delay):
|
|
}
|
|
if ctx.Err() != nil {
|
|
break
|
|
}
|
|
}
|
|
res.Detail = fmt.Sprintf("%s (%d/%d attempts failed)", res.Detail, attempts, attempts)
|
|
return res
|
|
}
|
|
}
|