geektutu/7days-golang · error

err.Error()

Error message

err.Error()

What it means

day4 ServeHTTP: the group exists but group.Get(key) returned an error, propagated to the client as HTTP 500 with err.Error(). Typical origin: the GetterFunc failing, or (with consistent hashing in day4) the selected peer fetch failing.

Source

Thrown at gee-cache/day4-consistent-hash/geecache/http.go:56

	// /<basepath>/<groupname>/<key> required
	parts := strings.SplitN(r.URL.Path[len(p.basePath):], "/", 2)
	if len(parts) != 2 {
		http.Error(w, "bad request", http.StatusBadRequest)
		return
	}

	groupName := parts[0]
	key := parts[1]

	group := GetGroup(groupName)
	if group == nil {
		http.Error(w, "no such group: "+groupName, http.StatusNotFound)
		return
	}

	view, err := group.Get(key)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/octet-stream")
	w.Write(view.ByteSlice())
}

View on GitHub (pinned to cf36443821)

Solutions

  1. Read the 500 body for the underlying err.Error() message.
  2. Verify peer addresses/health — remove or replace dead nodes in the consistent-hash ring.
  3. Fix the GetterFunc to handle 'not found' and transient backend errors gracefully.
  4. Add client-side retry for transient 500s.

Example fix

// before: dead peer in ring
peers := []string{"http://10.0.0.1:8001", "http://10.0.0.2:8001"} // .2 is down
// after: health-check peers and only register live ones
peers := filterHealthy([]string{"http://10.0.0.1:8001", "http://10.0.0.2:8001"})
Defensive patterns

Strategy: retry

Validate before calling

// pre-flight peer health before reads
func peerAlive(addr string) bool {
    resp, err := http.Get(addr + "/healthz")
    return err == nil && resp.StatusCode == http.StatusOK
}

Try / catch

// Go: retry transient 500s from the cache endpoint
var last error
for i := 0; i < 3; i++ {
    resp, err := http.Get(url)
    if err == nil && resp.StatusCode == http.StatusOK {
        return io.ReadAll(resp.Body)
    }
    if resp != nil && resp.StatusCode == http.StatusInternalServerError {
        last = fmt.Errorf("peer error (attempt %d)", i+1)
        time.Sleep(50 * time.Millisecond)
        continue
    }
    break
}
return last

Prevention

When it happens

Trigger: GetterFunc returns an error on cache miss; remote peer HTTP call fails or times out; backend datastore unavailable during a miss.

Common situations: One node of the hash ring is down; DB outage; serialization/nil handling bug in the getter; wrong peer address registered in the pool.

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/fc5f1120d82e31f5. Report an issue: GitHub.