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

🐛 [Bug]: use sync.Map maybe error #2425

Closed
3 tasks done
zing-dev opened this issue Apr 21, 2023 · 4 comments
Closed
3 tasks done

🐛 [Bug]: use sync.Map maybe error #2425

zing-dev opened this issue Apr 21, 2023 · 4 comments

Comments

@zing-dev
Copy link

Bug Description

w.app.Use(favicon.New())
	a := sync.Map{}
	w.app.Get("/p/:p", func(ctx *fiber.Ctx) error {
		p := ctx.Params("p")
		a.Store(p, true)
		fmt.Println(p, p == "112233", ctx.Request().URI())
		return ctx.JSON(true)
	})
	w.app.Get("/g/:p", func(ctx *fiber.Ctx) error {
		p := ctx.Params("p")
		_, ok := a.Load(ctx.Params("p"))
		_, ok2 := a.Load(p)
		fmt.Println(ctx.Request().URI())
		a.Range(func(key, value any) bool {
			//  112233 vicon. false??
			fmt.Println("->", ctx.Params("p"), key.(string), p == key.(string))
			return true
		})
		return ctx.SendString(fmt.Sprintf("%v %v", ok, ok2))
	})

request /p/112233 response true
request /g/112233 response maybe true or false
so print a ,sometimes print 112233 vicon. false ?? understand
but /w.app.Use(favicon.New())/ is ok

How to Reproduce

Steps to reproduce the behavior:

  1. Go to '....'
  2. Click on '....'
  3. Do '....'
  4. See '....'

Expected Behavior

request /p/112233 response true
request /g/112233 response maybe true or false
so print a ,sometimes print 112233 vicon. false ?? understand
but /w.app.Use(favicon.New())/ is ok

Fiber Version

2.43.0

Code Snippet (optional)

package main

import (
	"fmt"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/favicon"
	"github.com/gofiber/fiber/v2/middleware/recover"
	"log-collector-server/helps"
	"log-collector-server/log"
	"net/http"
	"sync"
)

func init() {
	_ = helps.CreateDir(helps.DefaultLogPath)
}

type web struct {
	app *fiber.App
	//apiManager *api.Manager
}

func newWeb() *web {
	var w = &web{
		app: fiber.New(fiber.Config{
			AppName:   "日志数据收集服务器",
			BodyLimit: 100 * 1024 * 1024,
			ErrorHandler: func(c *fiber.Ctx, err error) error {
				log.L.Error(fmt.Sprintf("%s %s", c.IP(), err))
				_, _ = c.WriteString(fmt.Sprintf("oops: %s", err))
				c.Response().SetStatusCode(http.StatusBadRequest)
				return nil
			}}),
	}
	//w.apiManager = api.NewAPIManager(w.app)
	return w
}

func (w *web) run() {
	w.app.Use(recover.New())
	w.app.Use(favicon.New())
	a := sync.Map{}
	w.app.Get("/p/:p", func(ctx *fiber.Ctx) error {
		p := ctx.Params("p")
		a.Store(p, true)
		fmt.Println(p, p == "112233", ctx.Request().URI())
		return ctx.JSON(true)
	})
	w.app.Get("/g/:p", func(ctx *fiber.Ctx) error {
		p := ctx.Params("p")
		_, ok := a.Load(ctx.Params("p"))
		_, ok2 := a.Load(p)
		fmt.Println(ctx.Request().URI())
		a.Range(func(key, value any) bool {
			//  112233 vicon. false??
			fmt.Println("->", ctx.Params("p"), key.(string), p == key.(string))
			return true
		})
		return ctx.SendString(fmt.Sprintf("%v %v", ok, ok2))
	})
	//w.apiManager.RegisterRouter()
	log.L.Fatal(w.app.Listen(":7011"))
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.
@welcome
Copy link

welcome bot commented Apr 21, 2023

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@leonklingele
Copy link
Member

leonklingele commented Apr 21, 2023 via email

@zing-dev
Copy link
Author

thank you,I changed, it works,It seems like I need to read more documents now 😆

@ReneWerner87
Copy link
Member

you can also use our copy function
https://github.com/gofiber/fiber/blob/master/utils/convert.go#L40
then you don't have to set the whole application to immutable, because this creates allocations which are not always necessary and eat performance

default is that no allocations are created for them and references exist

this is enough in most cases, because you only use the parameters for searching or comparing and they are not needed after that

only if you want to use the data after sending the response, like you did in the example, it is necessary to make a copy of it

by the way this is valid for all data you can get via fiber and the internal core fasthttp

@ReneWerner87 ReneWerner87 mentioned this issue May 2, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants