geektutu/7days-golang · error

HTTPPool serving unexpected path: " + r.URL.Path

Error message

HTTPPool serving unexpected path: " + r.URL.Path

What it means

Same as errors 91/93/96, in the day6-single-flight version: HTTPPool.ServeHTTP panics when r.URL.Path does not start with p.basePath ("/_geecache/"). Only requests matching /<basePath>/<group>/<key> are valid peer-protocol calls; all other paths are treated as routing bugs and panic.

Source

Thrown at gee-cache/day6-single-flight/geecache/http.go:45

}

// NewHTTPPool initializes an HTTP pool of peers.
func NewHTTPPool(self string) *HTTPPool {
	return &HTTPPool{
		self:     self,
		basePath: defaultBasePath,
	}
}

// Log info with server name
func (p *HTTPPool) Log(format string, v ...interface{}) {
	log.Printf("[Server %s] %s", p.self, fmt.Sprintf(format, v...))
}

// ServeHTTP handle all http requests
func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if !strings.HasPrefix(r.URL.Path, p.basePath) {
		panic("HTTPPool serving unexpected path: " + r.URL.Path)
	}
	p.Log("%s %s", r.Method, r.URL.Path)
	// /<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 on GitHub (pinned to cf36443821)

Solutions

  1. Mount the pool only at its base path: http.Handle("/_geecache/", pool)
  2. Serve health checks from a separate handler
  3. Configure proxies to preserve the /_geecache/ prefix
  4. Ensure all peers use the same base path

Example fix

// before
http.Handle("/", pool) // any non-cache path panics
// after
http.Handle("/_geecache/", pool)
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })
Defensive patterns

Strategy: validation

Validate before calling

if !strings.HasPrefix(r.URL.Path, "/_geecache/") {
    http.NotFound(w, r)
    return
}
pool.ServeHTTP(w, r)

Try / catch

defer func() {
    if rec := recover(); rec != nil {
        if s, ok := rec.(string); ok && strings.HasPrefix(s, "HTTPPool serving unexpected path") {
            http.NotFound(w, r)
            return
        }
        panic(rec)
    }
}()

Prevention

When it happens

Trigger: The pool's handler receives "/" or app paths — mux registration at root, health probes, reverse proxy stripping the prefix, or clients with a mismatched base path.

Common situations: Single binary serving UI + cache API on one mux; Docker/K8s health checks on "/"; load balancer rewrites removing /_geecache/.

Related errors


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