julienschmidt / httprouter

A high performance HTTP request router that scales well
https://pkg.go.dev/github.com/julienschmidt/httprouter
BSD 3-Clause "New" or "Revised" License
16.64k stars 1.47k forks source link

Do we need public API for Node? #235

Open iahmedov opened 6 years ago

iahmedov commented 6 years ago

Hi @julienschmidt,

While working on Telegram/Slack/Messenger Bot, I was in need something similar to HTTP Router but for routing messages in order to eliminate creating huge state machine. httprouter was perfect match for this use case and changed some parts of it. This could be used not only for bots but for other things where routing is needed.

package main

import (
    "fmt"
    "github.com/iahmedov/httprouter"
)

type ConsoleHandle = func(httprouter.Params)

func ConsoleIndex(_ httprouter.Params) {
    fmt.Printf("Welcome!\n")
}

func ConsoleHello(ps httprouter.Params) {
    fmt.Printf("hello, %s!\n", ps.ByName("name"))
}

func main() {
    tree := httprouter.NewTree()
    tree.AddRoute("/", ConsoleIndex)
    tree.AddRoute("/hello/:name", ConsoleHello)

    route1 := "/"
    handle1, params1, _ := tree.GetValue(route1)
    if handle1 != nil {
        handle1.(ConsoleHandle)(params1)
    }

    route2 := "/hello/router"
    handle2, params2, _ := tree.GetValue(route2)
    if handle2 != nil {
        handle2.(ConsoleHandle)(params2)
    }
}

Benchmark comparison shows slightly degraded performance in ns/operations. But allocations and memory usage hasn't changed

                                     old           new
BenchmarkHttpRouter_Param            107           96.7          -9.63%
BenchmarkHttpRouter_Param            105           97.2          -7.43%
BenchmarkHttpRouter_Param            100           98.4          -1.60%
BenchmarkHttpRouter_Param            103           96.1          -6.70%
BenchmarkHttpRouter_Param            100           99.4          -0.60%
BenchmarkHttpRouter_Param5           238           230           -3.36%
BenchmarkHttpRouter_Param5           236           231           -2.12%
BenchmarkHttpRouter_Param5           233           231           -0.86%
BenchmarkHttpRouter_Param5           254           230           -9.45%
BenchmarkHttpRouter_Param5           235           232           -1.28%
BenchmarkHttpRouter_Param20          652           625           -4.14%
BenchmarkHttpRouter_Param20          625           630           +0.80%
BenchmarkHttpRouter_Param20          628           629           +0.16%
BenchmarkHttpRouter_Param20          645           637           -1.24%
BenchmarkHttpRouter_Param20          635           631           -0.63%
BenchmarkHttpRouter_ParamWrite       152           153           +0.66%
BenchmarkHttpRouter_ParamWrite       153           147           -3.92%
BenchmarkHttpRouter_ParamWrite       150           149           -0.67%
BenchmarkHttpRouter_ParamWrite       153           150           -1.96%
BenchmarkHttpRouter_ParamWrite       153           152           -0.65%
BenchmarkHttpRouter_GithubStatic     64.7          62.8          -2.94%
BenchmarkHttpRouter_GithubStatic     65.7          62.7          -4.57%
BenchmarkHttpRouter_GithubStatic     63.2          63.0          -0.32%
BenchmarkHttpRouter_GithubStatic     63.0          63.1          +0.16%
BenchmarkHttpRouter_GithubStatic     63.0          63.0          +0.00%
BenchmarkHttpRouter_GithubParam      237           245           +3.38%
BenchmarkHttpRouter_GithubParam      237           247           +4.22%
BenchmarkHttpRouter_GithubParam      238           246           +3.36%
BenchmarkHttpRouter_GithubParam      238           245           +2.94%
BenchmarkHttpRouter_GithubParam      246           246           +0.00%
BenchmarkHttpRouter_GithubAll        41155         42797         +3.99%
BenchmarkHttpRouter_GithubAll        43126         42860         -0.62%
BenchmarkHttpRouter_GithubAll        41170         43934         +6.71%
BenchmarkHttpRouter_GithubAll        41021         42293         +3.10%
BenchmarkHttpRouter_GithubAll        43568         42762         -1.85%
BenchmarkHttpRouter_GPlusStatic      37.5          38.6          +2.93%
BenchmarkHttpRouter_GPlusStatic      38.5          39.0          +1.30%
BenchmarkHttpRouter_GPlusStatic      37.3          39.7          +6.43%
BenchmarkHttpRouter_GPlusStatic      37.4          39.3          +5.08%
BenchmarkHttpRouter_GPlusStatic      38.2          39.0          +2.09%
BenchmarkHttpRouter_GPlusParam       166           170           +2.41%
BenchmarkHttpRouter_GPlusParam       165           170           +3.03%
BenchmarkHttpRouter_GPlusParam       171           170           -0.58%
BenchmarkHttpRouter_GPlusParam       168           170           +1.19%
BenchmarkHttpRouter_GPlusParam       169           169           +0.00%
BenchmarkHttpRouter_GPlus2Params     195           204           +4.62%
BenchmarkHttpRouter_GPlus2Params     202           204           +0.99%
BenchmarkHttpRouter_GPlus2Params     198           207           +4.55%
BenchmarkHttpRouter_GPlus2Params     194           204           +5.15%
BenchmarkHttpRouter_GPlus2Params     207           206           -0.48%
BenchmarkHttpRouter_GPlusAll         2059          2102          +2.09%
BenchmarkHttpRouter_GPlusAll         1991          1994          +0.15%
BenchmarkHttpRouter_GPlusAll         2033          2021          -0.59%
BenchmarkHttpRouter_GPlusAll         2000          2032          +1.60%
BenchmarkHttpRouter_GPlusAll         1998          2017          +0.95%
BenchmarkHttpRouter_ParseStatic      39.6          38.5          -2.78%
BenchmarkHttpRouter_ParseStatic      38.4          38.9          +1.30%
BenchmarkHttpRouter_ParseStatic      39.7          38.7          -2.52%
BenchmarkHttpRouter_ParseStatic      38.2          38.9          +1.83%
BenchmarkHttpRouter_ParseStatic      38.0          37.8          -0.53%
BenchmarkHttpRouter_ParseParam       146           140           -4.11%
BenchmarkHttpRouter_ParseParam       142           140           -1.41%
BenchmarkHttpRouter_ParseParam       144           140           -2.78%
BenchmarkHttpRouter_ParseParam       142           141           -0.70%
BenchmarkHttpRouter_ParseParam       143           140           -2.10%
BenchmarkHttpRouter_Parse2Params     164           162           -1.22%
BenchmarkHttpRouter_Parse2Params     164           162           -1.22%
BenchmarkHttpRouter_Parse2Params     162           163           +0.62%
BenchmarkHttpRouter_Parse2Params     161           162           +0.62%
BenchmarkHttpRouter_Parse2Params     162           163           +0.62%
BenchmarkHttpRouter_ParseAll         2702          2705          +0.11%
BenchmarkHttpRouter_ParseAll         2706          2704          -0.07%
BenchmarkHttpRouter_ParseAll         2714          2873          +5.86%
BenchmarkHttpRouter_ParseAll         2722          2719          -0.11%
BenchmarkHttpRouter_ParseAll         2721          2710          -0.40%
BenchmarkHttpRouter_StaticAll        13872         13862         -0.07%
BenchmarkHttpRouter_StaticAll        14346         13858         -3.40%
BenchmarkHttpRouter_StaticAll        13843         13862         +0.14%
BenchmarkHttpRouter_StaticAll        14045         13821         -1.59%
BenchmarkHttpRouter_StaticAll        13985         13822         -1.17%

Here is the diff: https://github.com/julienschmidt/httprouter/compare/master...iahmedov:make_tree_public?diff=unified&name=master

Do we need this functionality in this main repo? If yes, I can open pull request.

EmpireJones commented 6 years ago

@julienschmidt, what are your thoughts on this? I think I'll be using @iahmedov's branch too, for custom routing scenarios. It'd be cool if this got into master.