Giter Club home page Giter Club logo

fastroute's People

Contributors

davecheney avatar l3pp4rd avatar unixisevil 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  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  avatar  avatar

fastroute's Issues

Native request context

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 
			}
		}
	})
}

Recycle has no unit tests

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%

when match static route, find race condition

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.

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.