mirror of
https://github.com/gin-gonic/gin.git
synced 2025-12-13 13:12:17 +08:00
Merge a41520ecbf63e739ed16a2c78b6ec44b96548f89 into 19b877fa50cbbb9282763099fb177a1e5cc5c850
This commit is contained in:
commit
12f803d34e
22
engine_test.go
Normal file
22
engine_test.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRoutesConcurrent(t *testing.T) {
|
||||||
|
r := New()
|
||||||
|
|
||||||
|
done := make(chan bool)
|
||||||
|
|
||||||
|
// Concurrently read routes
|
||||||
|
go func() {
|
||||||
|
_ = r.Routes()
|
||||||
|
done <- true
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Register a route at the same time
|
||||||
|
r.GET("/", func(c *Context) { c.String(200, "OK") })
|
||||||
|
|
||||||
|
<-done
|
||||||
|
}
|
||||||
5
gin.go
5
gin.go
@ -182,6 +182,7 @@ type Engine struct {
|
|||||||
noMethod HandlersChain
|
noMethod HandlersChain
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
trees methodTrees
|
trees methodTrees
|
||||||
|
treesMu sync.RWMutex
|
||||||
maxParams uint16
|
maxParams uint16
|
||||||
maxSections uint16
|
maxSections uint16
|
||||||
trustedProxies []string
|
trustedProxies []string
|
||||||
@ -362,6 +363,8 @@ func (engine *Engine) rebuild405Handlers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
||||||
|
engine.treesMu.Lock()
|
||||||
|
defer engine.treesMu.Unlock()
|
||||||
assert1(path[0] == '/', "path must begin with '/'")
|
assert1(path[0] == '/', "path must begin with '/'")
|
||||||
assert1(method != "", "HTTP method can not be empty")
|
assert1(method != "", "HTTP method can not be empty")
|
||||||
assert1(len(handlers) > 0, "there must be at least one handler")
|
assert1(len(handlers) > 0, "there must be at least one handler")
|
||||||
@ -388,6 +391,8 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
|
|||||||
// Routes returns a slice of registered routes, including some useful information, such as:
|
// Routes returns a slice of registered routes, including some useful information, such as:
|
||||||
// the http method, path, and the handler name.
|
// the http method, path, and the handler name.
|
||||||
func (engine *Engine) Routes() (routes RoutesInfo) {
|
func (engine *Engine) Routes() (routes RoutesInfo) {
|
||||||
|
engine.treesMu.RLock()
|
||||||
|
defer engine.treesMu.RUnlock()
|
||||||
for _, tree := range engine.trees {
|
for _, tree := range engine.trees {
|
||||||
routes = iterate("", tree.method, routes, tree.root)
|
routes = iterate("", tree.method, routes, tree.root)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user