Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 chore: Improve Performance of Fiber Router #3261

Merged
merged 14 commits into from
Dec 29, 2024
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
uses: golangci/golangci-lint-action@v6
with:
# NOTE: Keep this in sync with the version from .golangci.yml
version: v1.62.0
version: v1.62.2
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ markdown:
## lint: 🚨 Run lint checks
.PHONY: lint
lint:
go run github.com/golangci/golangci-lint/cmd/[email protected].0 run ./...
go run github.com/golangci/golangci-lint/cmd/[email protected].2 run ./...

## test: 🚦 Execute all tests
.PHONY: test
Expand Down
16 changes: 14 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@
// Note: It doesn't allow adding new methods, only customizing exist methods.
func (app *App) NewCtxFunc(function func(app *App) CustomCtx) {
app.newCtxFunc = function

if app.server != nil {
app.server.Handler = app.customRequestHandler
}
}

// RegisterCustomConstraint allows to register custom constraint.
Expand Down Expand Up @@ -868,7 +872,11 @@
func (app *App) Handler() fasthttp.RequestHandler { //revive:disable-line:confusing-naming // Having both a Handler() (uppercase) and a handler() (lowercase) is fine. TODO: Use nolint:revive directive instead. See https://github.com/golangci/golangci-lint/issues/3476
// prepare the server for the start
app.startupProcess()
return app.requestHandler

if app.newCtxFunc != nil {
return app.customRequestHandler
}

Check warning on line 878 in app.go

View check run for this annotation

Codecov / codecov/patch

app.go#L877-L878

Added lines #L877 - L878 were not covered by tests
return app.defaultRequestHandler
}

// Stack returns the raw router stack.
Expand Down Expand Up @@ -1057,7 +1065,11 @@
}

// fasthttp server settings
app.server.Handler = app.requestHandler
if app.newCtxFunc != nil {
app.server.Handler = app.customRequestHandler

Check warning on line 1069 in app.go

View check run for this annotation

Codecov / codecov/patch

app.go#L1069

Added line #L1069 was not covered by tests
} else {
app.server.Handler = app.defaultRequestHandler
}
app.server.Name = app.config.ServerHeader
app.server.Concurrency = app.config.Concurrency
app.server.NoDefaultDate = app.config.DisableDefaultDate
Expand Down
33 changes: 33 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,40 @@ func Test_App_Add_Method_Test(t *testing.T) {
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusNotImplemented, resp.StatusCode, "Status code")

// Add a new method
app.Add([]string{"JANE"}, "/doe", testEmptyHandler)

resp, err = app.Test(httptest.NewRequest("JANE", "/doe", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}

func Test_App_All_Method_Test(t *testing.T) {
t.Parallel()
defer func() {
if err := recover(); err != nil {
require.Equal(t, "add: invalid http method JANE\n", fmt.Sprintf("%v", err))
}
}()

methods := append(DefaultMethods, "JOHN") //nolint:gocritic // We want a new slice here
app := New(Config{
RequestMethods: methods,
})

// Add a new method with All
app.All("/doe", testEmptyHandler)

resp, err := app.Test(httptest.NewRequest("JOHN", "/doe", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")

// Add a new method
app.Add([]string{"JANE"}, "/doe", testEmptyHandler)

resp, err = app.Test(httptest.NewRequest("JANE", "/doe", nil))
require.NoError(t, err, "app.Test(req)")
require.Equal(t, StatusOK, resp.StatusCode, "Status code")
}

// go test -run Test_App_GETOnly
Expand Down
4 changes: 1 addition & 3 deletions binder/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ func parseToStruct(aliasTag string, out any, data map[string][]string) error {
func parseToMap(ptr any, data map[string][]string) error {
elem := reflect.TypeOf(ptr).Elem()

//nolint:exhaustive // it's not necessary to check all types
switch elem.Kind() {
switch elem.Kind() { //nolint:exhaustive // it's not necessary to check all types
case reflect.Slice:
newMap, ok := ptr.(map[string][]string)
if !ok {
Expand All @@ -129,7 +128,6 @@ func parseToMap(ptr any, data map[string][]string) error {
newMap[k] = ""
continue
}

newMap[k] = v[len(v)-1]
}
}
Expand Down
3 changes: 3 additions & 0 deletions ctx_interface_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,16 @@ func GetTrimmedParam(param string) string {

// RemoveEscapeChar remove escape characters
func RemoveEscapeChar(word string) string {
if strings.IndexByte(word, escapeChar) != -1 {
gaby marked this conversation as resolved.
Show resolved Hide resolved
return strings.ReplaceAll(word, string(escapeChar), "")
b := []byte(word)
dst := 0
for src := 0; src < len(b); src++ {
if b[src] == '\\' {
continue
}
b[dst] = b[src]
dst++
}
return word
return string(b[:dst])
}

func getParamConstraintType(constraintPart string) TypeConstraint {
Expand Down
Loading
Loading