Stampede
Function and HTTP request coalescer
Install / Use
/learn @go-chi/StampedeREADME
Stampede
Prevents cache stampede https://en.wikipedia.org/wiki/Cache_stampede by only running a single data fetch operation per expired / missing key regardless of number of requests to that key.
Example: HTTP Middleware
import (
"log/slog"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/stampede"
memcache "github.com/goware/cachestore-mem"
)
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("index"))
})
cache, err := memcache.NewBackend(1000)
if err != nil {
panic(err)
}
cacheMiddleware := stampede.Handler(
slog.Default(), cache, 5*time.Second,
stampede.WithHTTPCacheKeyRequestHeaders([]string{"AuthorizatioN"}),
)
r.With(cacheMiddleware).Get("/cached", func(w http.ResponseWriter, r *http.Request) {
// processing..
time.Sleep(1 * time.Second)
w.WriteHeader(200)
w.Write([]byte("...hi"))
})
http.ListenAndServe(":3333", r)
}
Notes
- Requests passed through the stampede handler will be batched into a single request when there are parallel requests for the same endpoint/resource. This is also known as request coalescing.
- Parallel requests for the same endpoint / resource, will be just a single handler call and the remaining requests will receive the response of the first request handler.
- The response payload for the endpoint / resource will then be cached for up to
ttltime duration for subequence requests, which offers further caching. You may also use attlvalue of 0 if you want the response to be as fresh as possible, and still prevent a stampede scenario on your handler. - Security note: response headers will be the same for all requests, so make sure
to not include anything sensitive or user specific. In the case you require user-specific
stampede handlers, make sure you pass a custom
keyFuncto thestampede.Handlerand split the cache by an account's id. NOTE: we do avoid caching response headers for CORS, set-cookie and x-ratelimit.
See example for a variety of examples.
LICENSE
MIT
Related Skills
node-connect
343.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
90.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
