Prevent same link got expanded at the same (will wait cache now)

This commit is contained in:
LittleSheep 2024-08-19 15:00:05 +08:00
parent a590bca9a4
commit 3d5ba6ef4c

View File

@ -4,11 +4,25 @@ import (
"encoding/base64" "encoding/base64"
"git.solsynth.dev/hydrogen/dealer/pkg/internal/services" "git.solsynth.dev/hydrogen/dealer/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"sync"
) )
var inProgress sync.Map
func getLinkMeta(c *fiber.Ctx) error { func getLinkMeta(c *fiber.Ctx) error {
targetEncoded := c.Params("target") targetEncoded := c.Params("target")
targetRaw, _ := base64.StdEncoding.DecodeString(targetEncoded) targetRaw, _ := base64.URLEncoding.DecodeString(targetEncoded)
if ch, loaded := inProgress.LoadOrStore(targetEncoded, make(chan struct{})); loaded {
// If the request is already in progress, wait for it to complete
<-ch.(chan struct{})
} else {
// If this is the first request, process it and signal others
defer func() {
close(ch.(chan struct{}))
inProgress.Delete(targetEncoded)
}()
}
if meta, err := services.LinkExpand(string(targetRaw)); err != nil { if meta, err := services.LinkExpand(string(targetRaw)); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error()) return fiber.NewError(fiber.StatusBadRequest, err.Error())