// 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) } }