Allow breaker tripped error to be configurable (#32869)

This commit is contained in:
rosstimothy 2023-10-05 12:43:55 -04:00 committed by GitHub
parent 99b4cb4d3d
commit afb2eab0fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View file

@ -103,7 +103,7 @@ func (s State) String() string {
// ErrStateTripped will be returned from executions performed while the CircuitBreaker
// is in StateTripped
var ErrStateTripped = trace.ConnectionProblem(nil, "breaker is tripped")
var ErrStateTripped = &trace.ConnectionProblemError{Message: "breaker is tripped"}
// Config contains configuration of the CircuitBreaker
type Config struct {
@ -134,6 +134,9 @@ type Config struct {
IsSuccessful func(v interface{}, err error) bool
// Logger is the logger
Logger logrus.FieldLogger
// TrippedErrorMessage is an optional message to use as the error message when the CircuitBreaker
// is tripped. Defaults to ErrStateTripped if not provided.
TrippedErrorMessage string
}
// TripFn determines if the CircuitBreaker should be tripped based
@ -331,7 +334,11 @@ func (c *CircuitBreaker) beforeExecution() (uint64, error) {
switch {
case state == StateTripped:
return generation, ErrStateTripped
if c.cfg.TrippedErrorMessage != "" {
return generation, trace.ConnectionProblem(nil, c.cfg.TrippedErrorMessage)
}
return generation, trace.Wrap(ErrStateTripped)
}
c.metrics.execute()

View file

@ -86,6 +86,8 @@ var _ ClientI = &Client{}
func NewClient(cfg client.Config, params ...roundtrip.ClientParam) (*Client, error) {
cfg.DialInBackground = true
cfg.CircuitBreakerConfig.TrippedErrorMessage = "Unable to communicate with the Teleport Auth Service"
if err := cfg.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}