Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add health check to high-level client interface
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
package client | ||
|
||
import ( | ||
"fmt" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
const ( | ||
HealthErrReasonUnrecoveredTransient = "UnrecoveredTransient" | ||
HealthErrReasonConnection = "ConnectionError" | ||
HealhtErrReasonUnknown = "Unknown" | ||
) | ||
|
||
// HealthError is used to represent error types for health checks | ||
type HealthError struct { | ||
ClientState string | ||
Reason string | ||
Message string | ||
} | ||
|
||
var _ error = HealthError{} | ||
|
||
// Error implements the Error interface. | ||
func (e HealthError) Error() string { | ||
return fmt.Sprintf("%s: %s", e.ClientState, e.Message) | ||
} | ||
|
||
// unrecoverableErrors are the set of errors that mean we can't recover an install strategy | ||
var unrecoverableErrors = map[string]struct{}{ | ||
HealthErrReasonUnrecoveredTransient: {}, | ||
} | ||
|
||
func NewHealthError(conn *grpc.ClientConn, reason string, msg string) HealthError { | ||
return HealthError{ | ||
ClientState: conn.GetState().String(), | ||
Reason: reason, | ||
Message: msg, | ||
} | ||
} | ||
|
||
// IsErrorUnrecoverable reports if a given strategy error is one of the predefined unrecoverable types | ||
func IsErrorUnrecoverable(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
_, ok := unrecoverableErrors[reasonForError(err)] | ||
return ok | ||
} | ||
|
||
func reasonForError(err error) string { | ||
switch t := err.(type) { | ||
case HealthError: | ||
return t.Reason | ||
case *HealthError: | ||
return t.Reason | ||
} | ||
return HealhtErrReasonUnknown | ||
} | ||
|