Files
GopherGate/internal/providers/circuit_breaker.go
T
hobokenchicken 1f574d8134
CI / Lint (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Build (push) Has been cancelled
feat: Phase 2 - reliability & observability
- Circuit breaker: proper thresholds (3 failures, 30s timeout)
- HTTP timeouts: 30s on all providers (was no timeout)
- Structured logging: slog replaces fmt.Printf throughout
- Stream errors: propagated as SSE error events to client
- Registry fetch: retry with backoff (3 attempts)
- Registry reads in dashboard protected by RWMutex
2026-04-26 14:48:56 -04:00

57 lines
1.5 KiB
Go

package providers
import (
"context"
"time"
"github.com/sony/gobreaker"
"gophergate/internal/models"
)
type CircuitBreakerProvider struct {
provider Provider
cb *gobreaker.CircuitBreaker
}
func NewCircuitBreakerProvider(p Provider) Provider {
name := p.Name()
var maxRequests uint32 = 5
var interval = 60 * time.Second
var timeout = 30 * time.Second
settings := gobreaker.Settings{
Name: name,
MaxRequests: maxRequests,
Interval: interval,
Timeout: timeout,
ReadyToTrip: func(counts gobreaker.Counts) bool {
// Trip after 3 consecutive failures
return counts.ConsecutiveFailures > 3
},
}
return &CircuitBreakerProvider{
provider: p,
cb: gobreaker.NewCircuitBreaker(settings),
}
}
func (cbp *CircuitBreakerProvider) Name() string {
return cbp.provider.Name()
}
func (cbp *CircuitBreakerProvider) ChatCompletion(ctx context.Context, req *models.UnifiedRequest) (*models.ChatCompletionResponse, error) {
result, err := cbp.cb.Execute(func() (interface{}, error) {
return cbp.provider.ChatCompletion(ctx, req)
})
if err != nil {
return nil, err
}
return result.(*models.ChatCompletionResponse), nil
}
func (cbp *CircuitBreakerProvider) ChatCompletionStream(ctx context.Context, req *models.UnifiedRequest) (<-chan *models.ChatCompletionStreamResponse, error) {
// Circuit breaker for streaming is tricky. We'll just call the provider directly.
// Future: Implement a way to track stream failures in the circuit breaker.
return cbp.provider.ChatCompletionStream(ctx, req)
}