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
90 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 HealthErrorReason = "UnrecoveredTransient" | ||
HealthErrReasonConnection HealthErrorReason = "ConnectionError" | ||
HealthErrReasonUnknown HealthErrorReason = "Unknown" | ||
) | ||
|
||
type HealthErrorReason string | ||
|
||
// HealthError is used to represent error types for health checks | ||
type HealthError struct { | ||
ClientState string | ||
Reason HealthErrorReason | ||
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 the connection | ||
var unrecoverableErrors = map[HealthErrorReason]struct{}{ | ||
HealthErrReasonUnrecoveredTransient: {}, | ||
} | ||
|
||
func NewHealthError(conn *grpc.ClientConn, reason HealthErrorReason, msg string) HealthError { | ||
return HealthError{ | ||
ClientState: conn.GetState().String(), | ||
Reason: reason, | ||
Message: msg, | ||
} | ||
} | ||
|
||
// IsErrorUnrecoverable reports if a given 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) HealthErrorReason { | ||
switch t := err.(type) { | ||
case HealthError: | ||
return t.Reason | ||
case *HealthError: | ||
return t.Reason | ||
} | ||
return HealthErrReasonUnknown | ||
} |