Codebase list golang-github-go-kit-kit / a843a9e
Detect when a fargo error indicates not found The fargo library already provides the HTTPResponseStatusCode function to extract an HTTP status code from a returned error, but here we want to be able to inject similar errors for tests, so look for both a real fargo error indicating that a heartbeat attempt failed to find its target instance and a synthetic error returned by a fake connection during a test. Steven E. Harris 6 years ago
1 changed file(s) with 26 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
2727 type fargoUnsuccessfulHTTPResponse struct {
2828 statusCode int
2929 messagePrefix string
30 }
31
32 func (u *fargoUnsuccessfulHTTPResponse) Error() string {
33 return fmt.Sprintf("err=%s code=%d", u.messagePrefix, u.statusCode)
3034 }
3135
3236 // Registrar maintains service instance liveness information in Eureka.
109113 }
110114 }
111115
116 func httpResponseStatusCode(err error) (code int, present bool) {
117 if code, ok := fargo.HTTPResponseStatusCode(err); ok {
118 return code, true
119 }
120 // Allow injection of errors for testing.
121 if u, ok := err.(*fargoUnsuccessfulHTTPResponse); ok {
122 return u.statusCode, true
123 }
124 return 0, false
125 }
126
127 func isNotFound(err error) bool {
128 code, ok := httpResponseStatusCode(err)
129 return ok && code == http.StatusNotFound
130 }
131
112132 func (r *Registrar) heartbeat() error {
113133 err := r.conn.HeartBeatInstance(r.instance)
114 if err != nil {
115 if u, ok := err.(*fargoUnsuccessfulHTTPResponse); ok && u.statusCode == http.StatusNotFound {
116 // Instance expired (e.g. network partition). Re-register.
117 r.logger.Log("during", "heartbeat", err.Error())
118 return r.conn.ReregisterInstance(r.instance)
119 }
134 if err == nil {
135 return nil
136 }
137 if isNotFound(err) {
138 // Instance expired (e.g. network partition). Re-register.
139 return r.conn.ReregisterInstance(r.instance)
120140 }
121141 return err
122142 }
123
124 func (u *fargoUnsuccessfulHTTPResponse) Error() string {
125 return fmt.Sprintf("err=%s code=%d", u.messagePrefix, u.statusCode)
126 }