Skip to content

Commit

Permalink
Use atomic instead of mutex when counting the route handler. (#1314)
Browse files Browse the repository at this point in the history
* use atomic instead of mutex when counting

* 📒 router: fix typo error
  • Loading branch information
bestgopher committed May 5, 2021
1 parent a28afaa commit 056c680
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
11 changes: 5 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/gofiber/fiber/v2/internal/colorable"
Expand Down Expand Up @@ -95,9 +96,9 @@ type App struct {
// contains the information if the route stack has been changed to build the optimized tree
routesRefreshed bool
// Amount of registered routes
routesCount int
routesCount uint32
// Amount of registered handlers
handlerCount int
handlerCount uint32
// Ctx pool
pool sync.Pool
// Fasthttp server
Expand Down Expand Up @@ -424,9 +425,7 @@ func (app *App) Mount(prefix string, fiber *App) Router {
}
}

app.mutex.Lock()
app.handlerCount += fiber.handlerCount
app.mutex.Unlock()
atomic.AddUint32(&app.handlerCount, fiber.handlerCount)

return app
}
Expand Down Expand Up @@ -918,7 +917,7 @@ func (app *App) startupMessage(addr string, tls bool, pids string) {
" │ Prefork .%s PID ....%s │\n"+
" └───────────────────────────────────────────────────┘"+
cReset,
value(strconv.Itoa(app.handlerCount), 14), value(procs, 12),
value(strconv.Itoa(int(app.handlerCount)), 14), value(procs, 12),
value(isPrefork, 14), value(strconv.Itoa(os.Getpid()), 14),
)

Expand Down
4 changes: 2 additions & 2 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func Test_App_Mount(t *testing.T) {
resp, err := app.Test(httptest.NewRequest(MethodGet, "/john/doe", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, 2, app.handlerCount)
utils.AssertEqual(t, uint32(2), app.handlerCount)
}

func Test_App_Use_Params(t *testing.T) {
Expand Down Expand Up @@ -845,7 +845,7 @@ func Test_App_Group_Mount(t *testing.T) {
resp, err := app.Test(httptest.NewRequest(MethodGet, "/v1/john/doe", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, 2, app.handlerCount)
utils.AssertEqual(t, uint32(2), app.handlerCount)
}

func Test_App_Group(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package fiber
import (
"fmt"
"reflect"
"sync/atomic"
)

// Group struct
Expand All @@ -27,9 +28,7 @@ func (grp *Group) Mount(prefix string, fiber *App) Router {
}
}

grp.app.mutex.Lock()
grp.app.handlerCount += fiber.handlerCount
grp.app.mutex.Unlock()
atomic.AddUint32(&grp.app.handlerCount, fiber.handlerCount)

return grp
}
Expand Down
18 changes: 6 additions & 12 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sort"
"strconv"
"strings"
"sync/atomic"
"time"

"github.com/gofiber/fiber/v2/utils"
Expand Down Expand Up @@ -41,7 +42,7 @@ type Router interface {
// Route is a struct that holds all metadata for each registered handler
type Route struct {
// Data for routing
pos int // Position in stack -> important for the sort of the matched routes
pos uint32 // Position in stack -> important for the sort of the matched routes
use bool // USE matches path prefixes
star bool // Path equals '*'
root bool // Path equals '/'
Expand Down Expand Up @@ -262,9 +263,7 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) Router {
Handlers: handlers,
}
// Increment global handler count
app.mutex.Lock()
app.handlerCount += len(handlers)
app.mutex.Unlock()
atomic.AddUint32(&app.handlerCount, uint32(len(handlers)))

// Middleware route matches all HTTP methods
if isUse {
Expand Down Expand Up @@ -393,9 +392,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {
Handlers: []Handler{handler},
}
// Increment global handler count
app.mutex.Lock()
app.handlerCount++
app.mutex.Unlock()
atomic.AddUint32(&app.handlerCount, 1)
// Add route to stack
app.addRoute(MethodGet, &route)
// Add HEAD route
Expand All @@ -404,7 +401,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {
}

func (app *App) addRoute(method string, route *Route) {
// Get unique HTTP method indentifier
// Get unique HTTP method identifier
m := methodInt(method)

// prevent identically route registration
Expand All @@ -414,10 +411,7 @@ func (app *App) addRoute(method string, route *Route) {
preRoute.Handlers = append(preRoute.Handlers, route.Handlers...)
} else {
// Increment global route position
app.mutex.Lock()
app.routesCount++
app.mutex.Unlock()
route.pos = app.routesCount
route.pos = atomic.AddUint32(&app.routesCount, 1)
route.Method = method
// Add route to the stack
app.stack[m] = append(app.stack[m], route)
Expand Down

0 comments on commit 056c680

Please sign in to comment.