mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-21 20:13:22 +00:00
🧹 Pre-initialize all compress handlers
This commit is contained in:
parent
d38419ebbc
commit
d18689558e
@ -27,6 +27,13 @@ var CompressConfigDefault = CompressConfig{
|
|||||||
Level: CompressLevelDefault,
|
Level: CompressLevelDefault,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var compressHandlers = map[int]fasthttp.RequestHandler{
|
||||||
|
CompressLevelDisabled: fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliNoCompression, fasthttp.CompressNoCompression),
|
||||||
|
CompressLevelDefault: fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliDefaultCompression, fasthttp.CompressDefaultCompression),
|
||||||
|
CompressLevelBestSpeed: fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliBestSpeed, fasthttp.CompressBestSpeed),
|
||||||
|
CompressLevelBestCompression: fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliBestCompression, fasthttp.CompressBestCompression),
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Compress allows the following config arguments in any order:
|
Compress allows the following config arguments in any order:
|
||||||
- Compress()
|
- Compress()
|
||||||
@ -58,17 +65,12 @@ func Compress(options ...interface{}) fiber.Handler {
|
|||||||
|
|
||||||
func compress(config CompressConfig) fiber.Handler {
|
func compress(config CompressConfig) fiber.Handler {
|
||||||
// Init middleware settings
|
// Init middleware settings
|
||||||
var compressHandler fasthttp.RequestHandler
|
compressHandler, ok := compressHandlers[config.Level]
|
||||||
switch config.Level {
|
if !ok {
|
||||||
case -1: // Disabled
|
// Use default level if provided level is invalid
|
||||||
compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliNoCompression, fasthttp.CompressNoCompression)
|
compressHandler = compressHandlers[CompressLevelDefault]
|
||||||
case 1: // Best speed
|
|
||||||
compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliBestSpeed, fasthttp.CompressBestSpeed)
|
|
||||||
case 2: // Best compression
|
|
||||||
compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliBestCompression, fasthttp.CompressBestCompression)
|
|
||||||
default: // Default
|
|
||||||
compressHandler = fasthttp.CompressHandlerBrotliLevel(func(c *fasthttp.RequestCtx) {}, fasthttp.CompressBrotliDefaultCompression, fasthttp.CompressDefaultCompression)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return handler
|
// Return handler
|
||||||
return func(c *fiber.Ctx) {
|
return func(c *fiber.Ctx) {
|
||||||
// Don't execute the middleware if Next returns false
|
// Don't execute the middleware if Next returns false
|
||||||
|
@ -144,7 +144,7 @@ func Test_Middleware_Compress_Panic(t *testing.T) {
|
|||||||
Compress("invalid")
|
Compress("invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
// go test -v -run=^$ -bench=Benchmark_Middleware_Compress -benchmem -count=4
|
// go test -v ./... -run=^$ -bench=Benchmark_Middleware_Compress -benchmem -count=4
|
||||||
func Benchmark_Middleware_Compress(b *testing.B) {
|
func Benchmark_Middleware_Compress(b *testing.B) {
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
app.Use(Compress())
|
app.Use(Compress())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user