diff --git a/sd/eureka/registrar.go b/sd/eureka/registrar.go index 99fef7c..7af7836 100644 --- a/sd/eureka/registrar.go +++ b/sd/eureka/registrar.go @@ -28,6 +28,10 @@ type fargoUnsuccessfulHTTPResponse struct { statusCode int messagePrefix string +} + +func (u *fargoUnsuccessfulHTTPResponse) Error() string { + return fmt.Sprintf("err=%s code=%d", u.messagePrefix, u.statusCode) } // Registrar maintains service instance liveness information in Eureka. @@ -110,18 +114,30 @@ } } +func httpResponseStatusCode(err error) (code int, present bool) { + if code, ok := fargo.HTTPResponseStatusCode(err); ok { + return code, true + } + // Allow injection of errors for testing. + if u, ok := err.(*fargoUnsuccessfulHTTPResponse); ok { + return u.statusCode, true + } + return 0, false +} + +func isNotFound(err error) bool { + code, ok := httpResponseStatusCode(err) + return ok && code == http.StatusNotFound +} + func (r *Registrar) heartbeat() error { err := r.conn.HeartBeatInstance(r.instance) - if err != nil { - if u, ok := err.(*fargoUnsuccessfulHTTPResponse); ok && u.statusCode == http.StatusNotFound { - // Instance expired (e.g. network partition). Re-register. - r.logger.Log("during", "heartbeat", err.Error()) - return r.conn.ReregisterInstance(r.instance) - } + if err == nil { + return nil + } + if isNotFound(err) { + // Instance expired (e.g. network partition). Re-register. + return r.conn.ReregisterInstance(r.instance) } return err } - -func (u *fargoUnsuccessfulHTTPResponse) Error() string { - return fmt.Sprintf("err=%s code=%d", u.messagePrefix, u.statusCode) -}