geektutu/7days-golang · error
key is required
Error message
key is required
What it means
Same empty-key guard in the day7-proto-buf build: Group.Get rejects an empty key up front. In day7 the peer protocol uses protobuf responses, but validation still happens before the local lookup, peer fetch, or loader invocation.
Source
Thrown at gee-cache/day7-proto-buf/geecache/geecache.go:69
loader: &singleflight.Group{},
}
groups[name] = g
return g
}
// GetGroup returns the named group previously created with NewGroup, or
// nil if there's no such group.
func GetGroup(name string) *Group {
mu.RLock()
g := groups[name]
mu.RUnlock()
return g
}
// Get value for a key from cache
func (g *Group) Get(key string) (ByteView, error) {
if key == "" {
return ByteView{}, fmt.Errorf("key is required")
}
if v, ok := g.mainCache.get(key); ok {
log.Println("[GeeCache] hit")
return v, nil
}
return g.load(key)
}
// RegisterPeers registers a PeerPicker for choosing remote peer
func (g *Group) RegisterPeers(peers PeerPicker) {
if g.peers != nil {
panic("RegisterPeerPicker called more than once")
}
g.peers = peers
}
View on GitHub (pinned to cf36443821)
Solutions
- Validate the key (non-empty after trim) before calling group.Get
- Fix the upstream key construction (path parsing, request params)
- Reject empty-key requests with 400 at the HTTP boundary
Example fix
// before
key := r.URL.Path[len(basePath)+len(groupName)+1:]
v, err := g.Get(key)
// after
if key == "" {
http.Error(w, "key is required", http.StatusBadRequest)
return
}
v, err := g.Get(key) Defensive patterns
Strategy: validation
Validate before calling
if k := strings.TrimSpace(key); k == "" {
http.Error(w, "key is required", http.StatusBadRequest)
return
}
v, err := group.Get(strings.TrimSpace(key)) Type guard
func hasKey(r *http.Request) bool {
_, key := splitGroupKey(r.URL.Path)
return strings.TrimSpace(key) != ""
} Prevention
- Validate the key path segment at the top of the peer HTTP handler
- Never pass raw request input into Get without trimming
- Cover empty-key requests in handler tests
When it happens
Trigger: group.Get(""); empty key segment parsed from the /_geecache/<group>/<key> URL path in the day7 server; unvalidated handler parameters.
Common situations: Client requests with a trailing slash and no key; keys sourced from empty request fields or config; tests hitting Get with empty strings.
Related errors
AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03).
Data as JSON: /api/errors/90557e97d08f0d8f.
Report an issue: GitHub.