Skip to content

Commit

Permalink
✨ enhancement: use msgp for flash message encoding/decoding (#3099)
Browse files Browse the repository at this point in the history
* enhancement: use msgp for flash message encoding/decoding

* add msgp tests

* improve test coverage

* improve test coverage

* fix linter

* update makefile

* extend go generation process

---------

Co-authored-by: Juan Calderon-Perez <[email protected]>
Co-authored-by: René <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2024
1 parent 0fbb447 commit 08d9fda
Show file tree
Hide file tree
Showing 12 changed files with 953 additions and 263 deletions.
14 changes: 6 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ tidy:
betteralign:
go run github.com/dkorunic/betteralign/cmd/betteralign@latest -test_files -generated_files -apply ./...

## tidy: ⚡️ Generate msgp
.PHONY: msgp
msgp:
go run github.com/tinylib/msgp@latest -file="middleware/cache/manager.go" -o="middleware/cache/manager_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/session/data.go" -o="middleware/session/data_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/csrf/storage_manager.go" -o="middleware/csrf/storage_manager_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/limiter/manager.go" -o="middleware/limiter/manager_msgp.go" -tests=true -unexported
go run github.com/tinylib/msgp@latest -file="middleware/idempotency/response.go" -o="middleware/idempotency/response_msgp.go" -tests=true -unexported
## tidy: ⚡️ Generate msgp && interface implementations
.PHONY: generate
generate:
go install github.com/tinylib/msgp@latest
go install github.com/vburenin/ifacemaker@975a95966976eeb2d4365a7fb236e274c54da64c
go generate ./...
4 changes: 2 additions & 2 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type DefaultCtx struct {
pathOriginal string // Original HTTP path
pathBuffer []byte // HTTP path buffer
detectionPathBuffer []byte // HTTP detectionPath buffer
redirectionMessages []string // Messages of the previous redirect
flashMessages redirectionMsgs // Flash messages
indexRoute int // Index of the current route
indexHandler int // Index of the current handler
methodINT int // HTTP method INT equivalent
Expand Down Expand Up @@ -1896,7 +1896,7 @@ func (c *DefaultCtx) release() {
c.route = nil
c.fasthttp = nil
c.bind = nil
c.redirectionMessages = c.redirectionMessages[:0]
c.flashMessages = c.flashMessages[:0]
c.viewBindMap = sync.Map{}
if c.redirect != nil {
ReleaseRedirect(c.redirect)
Expand Down
1 change: 1 addition & 0 deletions ctx_interface_gen.go

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

2 changes: 1 addition & 1 deletion middleware/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// msgp -file="manager.go" -o="manager_msgp.go" -tests=true -unexported
//
//go:generate msgp
//go:generate msgp -o=manager_msgp.go -tests=true -unexported
type item struct {
headers map[string][]byte
body []byte
Expand Down
2 changes: 1 addition & 1 deletion middleware/csrf/storage_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// msgp -file="storage_manager.go" -o="storage_manager_msgp.go" -tests=true -unexported
//
//go:generate msgp
//go:generate msgp -o=storage_manager_msgp.go -tests=true -unexported
type item struct{}

//msgp:ignore manager
Expand Down
2 changes: 1 addition & 1 deletion middleware/idempotency/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package idempotency
// response is a struct that represents the response of a request.
// generation tool `go install github.com/tinylib/msgp@latest`
//
//go:generate msgp -o=response_msgp.go -io=false -tests=true -unexported
//go:generate msgp -o=response_msgp.go -tests=true -unexported
type response struct {
Headers map[string][]string `msg:"hs"`

Expand Down
2 changes: 1 addition & 1 deletion middleware/limiter/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// msgp -file="manager.go" -o="manager_msgp.go" -tests=false -unexported
//
//go:generate msgp
//go:generate msgp -o=manager_msgp.go -tests=false -unexported
type item struct {
currHits int
prevHits int
Expand Down
2 changes: 1 addition & 1 deletion middleware/session/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// msgp -file="data.go" -o="data_msgp.go" -tests=true -unexported
//
//go:generate msgp
//go:generate msgp -o=data_msgp.go -tests=true -unexported
type data struct {
Data map[string]any
sync.RWMutex `msg:"-"`
Expand Down
Loading

1 comment on commit 08d9fda

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 08d9fda Previous: 87bb93e Ratio
Benchmark_Ctx_Fresh_WithNoCache 156.3 ns/op 0 B/op 0 allocs/op 92.92 ns/op 0 B/op 0 allocs/op 1.68
Benchmark_Ctx_Fresh_WithNoCache - ns/op 156.3 ns/op 92.92 ns/op 1.68
Benchmark_Ctx_IP_With_ProxyHeader 56.55 ns/op 0 B/op 0 allocs/op 32.39 ns/op 0 B/op 0 allocs/op 1.75
Benchmark_Ctx_IP_With_ProxyHeader - ns/op 56.55 ns/op 32.39 ns/op 1.75
Benchmark_Ctx_IP_With_ProxyHeader_and_IP_Validation 93.07 ns/op 0 B/op 0 allocs/op 60.52 ns/op 0 B/op 0 allocs/op 1.54
Benchmark_Ctx_IP_With_ProxyHeader_and_IP_Validation - ns/op 93.07 ns/op 60.52 ns/op 1.54
Benchmark_Ctx_Send 7.435 ns/op 0 B/op 0 allocs/op 4.661 ns/op 0 B/op 0 allocs/op 1.60
Benchmark_Ctx_Send - ns/op 7.435 ns/op 4.661 ns/op 1.60
Benchmark_Ctx_XHR 65.04 ns/op 0 B/op 0 allocs/op 39.65 ns/op 0 B/op 0 allocs/op 1.64
Benchmark_Ctx_XHR - ns/op 65.04 ns/op 39.65 ns/op 1.64
Benchmark_Utils_GetOffer/1_parameter 214.4 ns/op 0 B/op 0 allocs/op 136.1 ns/op 0 B/op 0 allocs/op 1.58
Benchmark_Utils_GetOffer/1_parameter - ns/op 214.4 ns/op 136.1 ns/op 1.58
Benchmark_Middleware_BasicAuth - B/op 80 B/op 48 B/op 1.67
Benchmark_Middleware_BasicAuth - allocs/op 5 allocs/op 3 allocs/op 1.67
Benchmark_Middleware_BasicAuth_Upper - B/op 80 B/op 48 B/op 1.67
Benchmark_Middleware_BasicAuth_Upper - allocs/op 5 allocs/op 3 allocs/op 1.67
Benchmark_CORS_NewHandler - B/op 16 B/op 0 B/op +∞
Benchmark_CORS_NewHandler - allocs/op 1 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerSingleOrigin - B/op 16 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerSingleOrigin - allocs/op 1 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflight - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflight - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflightSingleOrigin - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflightSingleOrigin - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflightWildcard - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflightWildcard - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_Middleware_CSRF_GenerateToken - B/op 521 B/op 326 B/op 1.60
Benchmark_Middleware_CSRF_GenerateToken - allocs/op 10 allocs/op 6 allocs/op 1.67

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.