data-dog / fastroute Goto Github PK
View Code? Open in Web Editor NEWSimple, idiomatic and fast 161 loc http router for golang
License: Other
Simple, idiomatic and fast 161 loc http router for golang
License: Other
It seems that the call to ps.reset(req) in line 324 of route.go is done with ps.ReadCloser unasigned, that means that req.Body receives a nil in the reset() function. This panics later when calling req.ParseForm()
Hi,
How do you envision the native http request Context would work with fastroute?
Consider a case where a middleware needs to set a value into the request context. At the moment this can be done if invoked handler.ServerHTTP( req.WithContext). This seems to defeat the point of returning a handler that fastroute can invoke.
Maybe I am missing something?
func aMiddleware(routes ...fastroute.Router) fastroute.Router {
return fastroute.RouterFunc(func(req *http.Request) http.Handler {
for _, r := range routes {
if h := r.Route(req); h != nil {
req.Header.Set("<set header or better modify context>", "aValue")
return h
}
}
})
}
The codecov report is wrong (it treats benchmarks as tests).
zapf(~/src/github.com/DATA-DOG/fastroute) % cover
PASS
coverage: 98.0% of statements
ok github.com/DATA-DOG/fastroute 0.015s
github.com/DATA-DOG/fastroute/router.go:97: Parameters 100.0%
github.com/DATA-DOG/fastroute/router.go:110: Pattern 100.0%
github.com/DATA-DOG/fastroute/router.go:132: Recycle 0.0%
github.com/DATA-DOG/fastroute/router.go:147: ByName 100.0%
github.com/DATA-DOG/fastroute/router.go:157: push 100.0%
github.com/DATA-DOG/fastroute/router.go:194: Route 100.0%
github.com/DATA-DOG/fastroute/router.go:204: ServeHTTP 100.0%
github.com/DATA-DOG/fastroute/router.go:219: Chain 100.0%
github.com/DATA-DOG/fastroute/router.go:247: New 100.0%
github.com/DATA-DOG/fastroute/router.go:321: match 100.0%
github.com/DATA-DOG/fastroute/router.go:353: wrap 100.0%
github.com/DATA-DOG/fastroute/router.go:358: reset 100.0%
github.com/DATA-DOG/fastroute/router.go:366: parameterized 100.0%
total: (statements) 98.0%
run code with -race flag from README, save as t.go :
package main
import (
"fmt"
"net/http"
fr "github.com/DATA-DOG/fastroute"
)
var routes = map[string]fr.Router{
"GET": fr.Chain(
fr.New("/", handler),
fr.New("/hello/:name/:surname", handler),
fr.New("/hello/:name", handler),
),
"POST": fr.Chain(
fr.New("/users", handler),
fr.New("/users/:id", handler),
),
}
var router = fr.RouterFunc(func(req *http.Request) http.Handler {
return routes[req.Method] // fastroute.Router is also http.Handler
})
func main() {
http.ListenAndServe(":8080", router)
}
func handler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, fmt.Sprintf(
`%s "%s", pattern: "%s", parameters: "%v"`,
req.Method,
req.URL.Path,
fr.Pattern(req),
fr.Parameters(req),
))
}
go run -race t.go
==================
WARNING: DATA RACE
Write at 0x00c420078340 by goroutine 7:
github.com/DATA-DOG/fastroute.New.func1()
/home/yj/gopath/src/github.com/DATA-DOG/fastroute/router.go:279 +0x105
github.com/DATA-DOG/fastroute.RouterFunc.Route()
/home/yj/gopath/src/github.com/DATA-DOG/fastroute/router.go:207 +0x41
github.com/DATA-DOG/fastroute.Chain.func1()
/home/yj/gopath/src/github.com/DATA-DOG/fastroute/router.go:234 +0x94
github.com/DATA-DOG/fastroute.RouterFunc.ServeHTTP()
/home/yj/gopath/src/github.com/DATA-DOG/fastroute/router.go:217 +0x41
github.com/DATA-DOG/fastroute.RouterFunc.ServeHTTP()
/home/yj/gopath/src/github.com/DATA-DOG/fastroute/router.go:218 +0x78
net/http.serverHandler.ServeHTTP()
/usr/local/go/src/net/http/server.go:2568 +0xbc
net/http.(*conn).serve()
/usr/local/go/src/net/http/server.go:1825 +0x71a
Previous write at 0x00c420078340 by goroutine 6:
github.com/DATA-DOG/fastroute.New.func1()
/home/yj/gopath/src/github.com/DATA-DOG/fastroute/router.go:279 +0x105
github.com/DATA-DOG/fastroute.RouterFunc.Route()
/home/yj/gopath/src/github.com/DATA-DOG/fastroute/router.go:207 +0x41
github.com/DATA-DOG/fastroute.Chain.func1()
/home/yj/gopath/src/github.com/DATA-DOG/fastroute/router.go:234 +0x94
github.com/DATA-DOG/fastroute.RouterFunc.ServeHTTP()
/home/yj/gopath/src/github.com/DATA-DOG/fastroute/router.go:217 +0x41
github.com/DATA-DOG/fastroute.RouterFunc.ServeHTTP()
/home/yj/gopath/src/github.com/DATA-DOG/fastroute/router.go:218 +0x78
net/http.serverHandler.ServeHTTP()
/usr/local/go/src/net/http/server.go:2568 +0xbc
net/http.(*conn).serve()
/usr/local/go/src/net/http/server.go:1825 +0x71a
Goroutine 7 (running) created at:
net/http.(*Server).Serve()
/usr/local/go/src/net/http/server.go:2668 +0x35f
net/http.(*Server).ListenAndServe()
/usr/local/go/src/net/http/server.go:2585 +0xe0
net/http.ListenAndServe()
/usr/local/go/src/net/http/server.go:2787 +0xee
main.main()
/tmp/t.go:27 +0x6b
Goroutine 6 (running) created at:
net/http.(*Server).Serve()
/usr/local/go/src/net/http/server.go:2668 +0x35f
net/http.(*Server).ListenAndServe()
/usr/local/go/src/net/http/server.go:2585 +0xe0
net/http.ListenAndServe()
/usr/local/go/src/net/http/server.go:2787 +0xee
main.main()
/tmp/t.go:27 +0x6b
==================
test script with t.sh:
#!/bin/sh
for i in `seq 1 10`;do
(curl -d msg=hello 'localhost:8080/users')&
done
wait
reason : return RouterFunc will be concurrently called by mutiple goroutine, share one parameters instance.
https://github.com/DATA-DOG/fastroute/blob/master/router.go#L263
uses IndexAny(...) == -1, which can be replaced with ContainsAny(...) => bool. However during testing I accidentally used Contains, not ContainsAny and all tests pass.
This codepath may be under tested.
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.