- Added videoWorkflowRepository with methods to manage video and user interactions. - Introduced catalog_mapper for converting database models to protobuf representations. - Created domain_helpers for normalizing domain and ad format values. - Defined service interfaces for payment, account, notification, domain, ad template, player config, video, and user management. - Implemented OAuth helpers for generating state and caching keys. - Developed payment_proto_helpers for mapping payment-related models to protobuf. - Added service policy helpers to enforce plan requirements and user permissions. - Created user_mapper for converting user payloads to protobuf format. - Implemented value_helpers for handling various value conversions and nil checks. - Developed video_helpers for normalizing video statuses and managing storage types. - Created video_mapper for mapping video models to protobuf format. - Implemented render workflow for managing video creation and job processing.
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package circuitbreaker
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/sony/gobreaker"
|
|
)
|
|
|
|
var (
|
|
ErrCircuitOpen = errors.New("circuit breaker is open")
|
|
)
|
|
|
|
type CircuitBreaker struct {
|
|
cb *gobreaker.CircuitBreaker
|
|
}
|
|
|
|
// New creates a new circuit breaker with default settings
|
|
func New(name string) *CircuitBreaker {
|
|
settings := gobreaker.Settings{
|
|
Name: name,
|
|
MaxRequests: 3,
|
|
Interval: time.Second * 60,
|
|
Timeout: time.Second * 30,
|
|
ReadyToTrip: func(counts gobreaker.Counts) bool {
|
|
failureRatio := float64(counts.TotalFailures) / float64(counts.Requests)
|
|
return counts.Requests >= 3 && failureRatio >= 0.6
|
|
},
|
|
OnStateChange: func(name string, from gobreaker.State, to gobreaker.State) {
|
|
// Log state changes
|
|
// logger.Info("Circuit breaker state changed", "name", name, "from", from, "to", to)
|
|
},
|
|
}
|
|
|
|
return &CircuitBreaker{
|
|
cb: gobreaker.NewCircuitBreaker(settings),
|
|
}
|
|
}
|
|
|
|
// Execute runs the function with circuit breaker protection
|
|
func (cb *CircuitBreaker) Execute(ctx context.Context, fn func() error) error {
|
|
_, err := cb.cb.Execute(func() (interface{}, error) {
|
|
return nil, fn()
|
|
})
|
|
|
|
if err == gobreaker.ErrOpenState {
|
|
return ErrCircuitOpen
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// ExecuteWithFallback runs the function with circuit breaker and fallback
|
|
func (cb *CircuitBreaker) ExecuteWithFallback(ctx context.Context, fn func() error, fallback func() error) error {
|
|
err := cb.Execute(ctx, fn)
|
|
if err == ErrCircuitOpen && fallback != nil {
|
|
return fallback()
|
|
}
|
|
return err
|
|
}
|
|
|
|
// State returns the current state of the circuit breaker
|
|
func (cb *CircuitBreaker) State() gobreaker.State {
|
|
return cb.cb.State()
|
|
}
|