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

Fix cache mutex lock #1764

Merged
merged 1 commit into from Feb 11, 2022
Merged

Conversation

apoq
Copy link
Contributor

@apoq apoq commented Feb 11, 2022

Hello. There's a problem with mutex lock in fiber cache middleware. See the example below:

package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cache"
	_ "net/http/pprof"
	"time"
)

func main() {
	app := fiber.New()

	app.Use(cache.New(cache.Config{
		Next: func(c *fiber.Ctx) bool {
			if c.Route().Path == "/health" {
				return false
			}

			return true
		},
	}))

	app.Get("/health", func(c *fiber.Ctx) error {
		return c.SendString("ok")
	})

	app.Get("/test", func(c *fiber.Ctx) error {
		time.Sleep(5 * time.Second)
		return c.SendString("sleep-test")
	})

	if err := app.Listen(":9001"); err != nil {
		panic(err)
	}
}

Try accessing /test and /health endpoints. Health endpoint will respond only when Test endpoint has finished. Thus, the slowest endpoint becomes the bottleneck for all GET requests (as other methods are being skipped in cache middleware)

This PR makes sure that we don't have lock enabled when running c.Next() (down the stack), and only being used when dealing with cache entry.

@welcome
Copy link

welcome bot commented Feb 11, 2022

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@apoq apoq force-pushed the fix/cache-bottleneck branch from eb0ae8b to 70d13ef Compare Feb 11, 2022
@ReneWerner87 ReneWerner87 merged commit b9efc76 into gofiber:master Feb 11, 2022
14 checks passed
@welcome
Copy link

welcome bot commented Feb 11, 2022

Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants