From afb2eab0faaf1971c6b15fe82bbacfd8f7e1c970 Mon Sep 17 00:00:00 2001 From: rosstimothy <39066650+rosstimothy@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:43:55 -0400 Subject: [PATCH] Allow breaker tripped error to be configurable (#32869) --- api/breaker/breaker.go | 11 +++++++++-- lib/auth/clt.go | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/api/breaker/breaker.go b/api/breaker/breaker.go index 6000d8025d8..0e95611b754 100644 --- a/api/breaker/breaker.go +++ b/api/breaker/breaker.go @@ -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() diff --git a/lib/auth/clt.go b/lib/auth/clt.go index e7aaa34f55e..9a4e0c8341a 100644 --- a/lib/auth/clt.go +++ b/lib/auth/clt.go @@ -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) }