Git Product home page Git Product logo

stampede's People

Contributors

c2h5oh avatar pkieltyka avatar senpos avatar vojtechvitek avatar xiam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

stampede's Issues

panic with empty handler func

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

Panic during data fetch is not recovered properly even with panic recovery middleware present

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.

go func() {
c.val, c.err = fn(ctx)
close(c.done)
}()

The fix should be to recover the panic properly in the callback function within HandlerWithKey and set the error properly.

stampede/http.go

Lines 82 to 95 in 39949d2

val, err := cache.GetFresh(r.Context(), key, func(ctx context.Context) (interface{}, error) {
first = true
buf := bytes.NewBuffer(nil)
ww := &responseWriter{ResponseWriter: w, tee: buf}
next.ServeHTTP(ww, r)
val := responseValue{
headers: ww.Header(),
status: ww.Status(),
body: buf.Bytes(),
}
return val, nil
})

cors bug

it appears the cache is recording cors headers, which can be problematic when responding to different origins

Using stampede with compress middleware.

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?

Please cut a new release to pick up LICENSE file

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.