go-chi / stampede Goto Github PK
View Code? Open in Web Editor NEWFunction and HTTP request coalescer
License: MIT License
Function and HTTP request coalescer
License: MIT License
Cannot view documentation on pkg.go.dev
: https://pkg.go.dev/github.com/go-chi/stampede
Maybe adding a license file will resolve this?
Panic occurs if an empty handler that does not write a status code is called more than once.
package stampede_test
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/go-chi/stampede"
)
func TestPanic(t *testing.T) {
mux := http.NewServeMux()
middleware := stampede.Handler(100, 1*time.Hour)
mux.Handle("/", middleware(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
t.Log(r.Method, r.URL)
})))
ts := httptest.NewServer(mux)
defer ts.Close()
{
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
t.Log(resp.StatusCode)
}
{
req, err := http.NewRequest(http.MethodGet, ts.URL, nil)
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
t.Log(resp.StatusCode)
}
}
2021/03/05 10:52:17 http: panic serving 127.0.0.1:59282: invalid WriteHeader code 0
goroutine 34 [running]:
net/http.(*conn).serve.func1(0xc000208000)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:1824 +0x153
panic(0x744ac0, 0xc000224100)
/home/heijo/ghq/go.googlesource.com/go/src/runtime/panic.go:980 +0x4d7
net/http.checkWriteHeaderCode(...)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:1112
net/http.(*response).WriteHeader(0xc0002380e0, 0x0)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:1146 +0x72b
github.com/go-chi/stampede.HandlerWithKey.func1.1(0x82d310, 0xc0002380e0, 0xc00022c100)
/home/heijo/ghq/github.com/go-chi/stampede/http.go:120 +0x4c3
net/http.HandlerFunc.ServeHTTP(0xc000222340, 0x82d310, 0xc0002380e0, 0xc00022c100)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:2069 +0x44
github.com/go-chi/stampede.Handler.func2.1(0x82d310, 0xc0002380e0, 0xc00022c100)
/home/heijo/ghq/github.com/go-chi/stampede/http.go:49 +0x16d
net/http.HandlerFunc.ServeHTTP(0xc00011da10, 0x82d310, 0xc0002380e0, 0xc00022c100)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:2069 +0x44
net/http.(*ServeMux).ServeHTTP(0xc000168fc0, 0x82d310, 0xc0002380e0, 0xc00022c100)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:2448 +0x1ad
net/http.serverHandler.ServeHTTP(0xc0001e2000, 0x82d310, 0xc0002380e0, 0xc00022c100)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:2887 +0xa3
net/http.(*conn).serve(0xc000208000, 0x82d980, 0xc000222000)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:1952 +0x8cd
created by net/http.(*Server).Serve
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:3013 +0x398
2021/03/05 10:52:17 http: panic serving 127.0.0.1:59284: invalid WriteHeader code 0
goroutine 38 [running]:
net/http.(*conn).serve.func1(0xc000208140)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:1824 +0x153
panic(0x744ac0, 0xc00028e020)
/home/heijo/ghq/go.googlesource.com/go/src/runtime/panic.go:980 +0x4d7
net/http.checkWriteHeaderCode(...)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:1112
net/http.(*response).WriteHeader(0xc000292000, 0x0)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:1146 +0x72b
github.com/go-chi/stampede.HandlerWithKey.func1.1(0x82d310, 0xc000292000, 0xc00022c300)
/home/heijo/ghq/github.com/go-chi/stampede/http.go:120 +0x4c3
net/http.HandlerFunc.ServeHTTP(0xc00028a080, 0x82d310, 0xc000292000, 0xc00022c300)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:2069 +0x44
github.com/go-chi/stampede.Handler.func2.1(0x82d310, 0xc000292000, 0xc00022c300)
/home/heijo/ghq/github.com/go-chi/stampede/http.go:49 +0x16d
net/http.HandlerFunc.ServeHTTP(0xc00011da10, 0x82d310, 0xc000292000, 0xc00022c300)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:2069 +0x44
net/http.(*ServeMux).ServeHTTP(0xc000168fc0, 0x82d310, 0xc000292000, 0xc00022c300)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:2448 +0x1ad
net/http.serverHandler.ServeHTTP(0xc0001e2000, 0x82d310, 0xc000292000, 0xc00022c300)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:2887 +0xa3
net/http.(*conn).serve(0xc000208140, 0x82d980, 0xc0002223c0)
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:1952 +0x8cd
created by net/http.(*Server).Serve
/home/heijo/ghq/go.googlesource.com/go/src/net/http/server.go:3013 +0x398
--- FAIL: TestPanic (0.00s)
http_test.go:16: GET /
http_test.go:32: 200
http_test.go:41: Get "http://127.0.0.1:43837": EOF
FAIL
FAIL github.com/go-chi/stampede 0.004s
FAIL
First of all, thanks for this library! The performance gains are very noticeable.
I found that when there is a panic during data fetch inside the handler, then the panic cannot be recovered even if there is a panic recovery middleware present in the middleware chain or if there is a recovery middleware wrapping the stampede cache handler itself.
I believe this is because the singleflight
package here creates a goroutine and if a panic occurs in that goroutine, then it can never be recovered in the main goroutine.
stampede/singleflight/singleflight.go
Lines 68 to 71 in 39949d2
The fix should be to recover the panic properly in the callback function within HandlerWithKey
and set the error properly.
Lines 82 to 95 in 39949d2
it appears the cache is recording cors headers, which can be problematic when responding to different origins
I'm trying to figure out how to use stampede on a single route level with compress middleware.
The issue is that compress middleware adds a Content-Encoding
header before stampede will cache it. So on the next request data with Content-Encoding
would be returned as is, because compress middleware would think that data has been compressed.
I'm willing to raise a PR fix, but not fully sure about direction. Is it an issue with compression logic or caching?
Related to #4, It looks like pkg.go.dev serves a cached version of the latest tagged release which in this case is v0.4.4 and does not include the LICENSE file.
So if you visit https://pkg.go.dev/github.com/go-chi/stampede it tries to serve the docs for v0.4.4. You can coerce it to fetch a new version from master which correctly indexes everything (https://pkg.go.dev/github.com/go-chi/stampede/@master), but this is not the default serving tag.
Releasing a v0.4.5 which includes the LICENSE file should fix it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.