geektutu/7days-golang · error

err.Error()

Error message

err.Error()

What it means

day5 ServeHTTP: group found, but group.Get(key) errored and the message is forwarded as HTTP 500. In the multi-node setup the usual sources are a failing GetterFunc or a failed remote fetch from a peer node selected via consistent hashing / Peek.

Source

Thrown at gee-cache/day5-multi-nodes/geecache/http.go:66

	// /<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())
}

// Set updates the pool's list of peers.
func (p *HTTPPool) Set(peers ...string) {
	p.mu.Lock()
	defer p.mu.Unlock()
	p.peers = consistenthash.New(defaultReplicas, nil)
	p.peers.Add(peers...)
	p.httpGetters = make(map[string]*httpGetter, len(peers))
	for _, peer := range peers {
		p.httpGetters[peer] = &httpGetter{baseURL: peer + p.basePath}
	}
}

View on GitHub (pinned to cf36443821)

Solutions

  1. Inspect the 500 body for the underlying err.Error() text.
  2. Check peer health and peer base-URL registration; remove dead peers from the ring.
  3. Harden the GetterFunc (timeouts, retries, clean 'not found' handling).
  4. Retry the request client-side if a peer was momentarily unavailable.

Example fix

// before: peer URL typo
p.SetPeers(peers...)
// peers contains "http://127.0.0.1:7002" but node listens on 8002
// after: generate peer URLs from the actual listen addresses
addrs := []string{":8001", ":8002", ":8003"}
var peers []string
for _, a := range addrs {
    peers = append(peers, "http://127.0.0.1"+a)
}
Defensive patterns

Strategy: retry

Validate before calling

// day5: verify peer connectivity before reads
func checkPeer(addr string) error {
    conn, err := net.DialTimeout("tcp", strings.TrimPrefix(addr, "http://"), time.Second)
    if err != nil { return err }
    return conn.Close()
}

Try / catch

// Go: read the 500 body (server's err.Error()) and retry once
resp, err := http.Get(url)
if err != nil { return err }
if resp.StatusCode == http.StatusInternalServerError {
    body, _ := io.ReadAll(resp.Body)
    time.Sleep(100 * time.Millisecond)
    return fmt.Errorf("cache get failed: %s (will retry)", body)
}

Prevention

When it happens

Trigger: GetterFunc returns an error on miss; peer-to-peer HTTP fetch (proto exchange) fails; backend datastore error during load; dead or misconfigured peer base URLs.

Common situations: A cache peer crashed or was redeployed; wrong peer address in the pool; DB outage; network partition between nodes.

Related errors


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