All checks were successful
CI / test (push) Successful in 31s
Six examples covering the full API surface: - basic-client: retry, timeout, logging, response size limit - form-request: form-encoded POST for OAuth/webhooks - load-balancing: weighted endpoints, circuit breaker, health checks - server-basic: routing, groups, JSON helpers, health, custom 404 - server-protected: CORS, rate limiting, body limits, timeouts - request-id-propagation: cross-service request ID forwarding Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
// Demonstrates load balancing across multiple backend endpoints with
|
|
// circuit breaking and health-checked failover.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"git.codelab.vc/pkg/httpx"
|
|
"git.codelab.vc/pkg/httpx/balancer"
|
|
"git.codelab.vc/pkg/httpx/circuitbreaker"
|
|
"git.codelab.vc/pkg/httpx/retry"
|
|
)
|
|
|
|
func main() {
|
|
client := httpx.New(
|
|
httpx.WithEndpoints(
|
|
balancer.Endpoint{URL: "http://localhost:8081", Weight: 3},
|
|
balancer.Endpoint{URL: "http://localhost:8082", Weight: 1},
|
|
),
|
|
httpx.WithBalancer(
|
|
balancer.WithStrategy(balancer.WeightedRandom()),
|
|
balancer.WithHealthCheck(
|
|
balancer.WithHealthInterval(5*time.Second),
|
|
balancer.WithHealthPath("/healthz"),
|
|
),
|
|
),
|
|
httpx.WithCircuitBreaker(
|
|
circuitbreaker.WithFailureThreshold(5),
|
|
circuitbreaker.WithOpenDuration(30*time.Second),
|
|
),
|
|
httpx.WithRetry(
|
|
retry.WithMaxAttempts(3),
|
|
),
|
|
httpx.WithLogger(slog.Default()),
|
|
)
|
|
defer client.Close()
|
|
|
|
ctx := context.Background()
|
|
|
|
for i := range 5 {
|
|
resp, err := client.Get(ctx, fmt.Sprintf("/api/item/%d", i))
|
|
if err != nil {
|
|
log.Printf("request %d failed: %v", i, err)
|
|
continue
|
|
}
|
|
|
|
body, _ := resp.String()
|
|
fmt.Printf("request %d → %d: %s\n", i, resp.StatusCode, body)
|
|
}
|
|
}
|