Compare commits

...

3 Commits

Author SHA1 Message Date
S-Sarim
9d014de7e9
Merge a41520ecbf63e739ed16a2c78b6ec44b96548f89 into 2a794cd0b0faa7d829291375b27a3467ea972b0d 2025-12-03 19:53:47 -08:00
OHZEKI Naoki
2a794cd0b0
fix(debug): version mismatch (#4403)
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
2025-12-04 10:49:37 +08:00
Syed Sarim Bin Ahmer
a41520ecbf Fix: make Engine.Routes() concurrency-safe (#4457) 2025-12-02 12:47:27 +05:00
3 changed files with 28 additions and 1 deletions

View File

@ -13,7 +13,7 @@ import (
"sync/atomic"
)
const ginSupportMinGoVer = 23
const ginSupportMinGoVer = 24
// IsDebugging returns true if the framework is running in debug mode.
// Use SetMode(gin.ReleaseMode) to disable debug mode.

22
engine_test.go Normal file
View 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
View File

@ -182,6 +182,7 @@ type Engine struct {
noMethod HandlersChain
pool sync.Pool
trees methodTrees
treesMu sync.RWMutex
maxParams uint16
maxSections uint16
trustedProxies []string
@ -362,6 +363,8 @@ func (engine *Engine) rebuild405Handlers() {
}
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
engine.treesMu.Lock()
defer engine.treesMu.Unlock()
assert1(path[0] == '/', "path must begin with '/'")
assert1(method != "", "HTTP method can not be empty")
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:
// the http method, path, and the handler name.
func (engine *Engine) Routes() (routes RoutesInfo) {
engine.treesMu.RLock()
defer engine.treesMu.RUnlock()
for _, tree := range engine.trees {
routes = iterate("", tree.method, routes, tree.root)
}