gofiber / fiber

⚡️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.9k stars 1.67k forks source link

🐛 [Bug]: Memory data disorder #2612

Closed qiannianshaonian closed 1 year ago

qiannianshaonian commented 1 year ago

Bug Description

my request method is "GET" or "POST", but when i write route info in my sqlDB, i found the method is changed "POS"

1693462097196

How to Reproduce

Steps to reproduce the behavior:

  1. start httpServer by debug
  2. start httpClient
  3. you can find this bug

Expected Behavior

Reproduction steps:

  1. start httpServer by debug
  2. start httpClient
  3. you can find this bug

1693462097196 //code in attachs code.zip

Fiber Version

github.com/gofiber/fiber/v2 v2.49.0

Code Snippet (optional)

package main

import (
    "fmt"
    "log"
    "strings"

    "github.com/gofiber/fiber/v2"
)

func main() {
    app := fiber.New(
        fiber.Config{
            RequestMethods: []string{"POST", "GET", "PUT", "HEAD"},
            // 进行各个参数设置
            ServerHeader: "test",
            // 设置请求body大小限制
            BodyLimit: 10 * 1024 * 1024 * 1024,
            // 设置请求并发限制
            Concurrency:          1024 * 1024 * 1024,
            ReadBufferSize:       128 * 1024,
            WriteBufferSize:      128 * 1024,
            CompressedFileSuffix: ".gz",
            ReduceMemoryUsage:    false,
            DisableKeepalive:     true,
            // 禁用启动时的信息展示
            DisableStartupMessage: true,
        },
    )
    app.All("*", microservice)
    log.Fatal(app.Listen(":8000"))
}

func microservice(c *fiber.Ctx) error {
    method := strings.ToUpper(c.Method())
    path := c.Path()
    doTracing(path, method)
    msg := fmt.Sprintf("1111 %s", c.Params("*"))
    return c.SendString(msg)
}

func doTracing(path, method string) {
    t := GatewayRouterForm{
        Method: method,
        ReqURL: path + method,
    }
    if t.Method == "POS" {
        fmt.Println(".....")
    }
    fmt.Printf("....1111   %p %s %v \n", &t, t.ReqURL, t)
    go next(t)
}
func next(t GatewayRouterForm) {
    fmt.Printf("....222222   %p %s %v \n", &t, t.ReqURL, t)
    if t.Method == "POS" {
        fmt.Printf("....3333   %p %s %v \n", &t, t.ReqURL, t)
    }
}

type GatewayRouterForm struct {
    // 请求Method
    Method string
    // 请求的全url
    ReqURL string
}

Checklist:

welcome[bot] commented 1 year ago

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

ReneWerner87 commented 1 year ago

this is why fiber and fasthttp is so fast

we reuse the requests and responses for subsequent requests

read this https://docs.gofiber.io/#zero-allocation

the solution is to simply make a copy of the data for everything that breaks out of this context, such as a go routine that runs alongside it or other processes that want to get the data afterwards, so that it is not overwritten by the replacement in the next request response process.

image

qiannianshaonian commented 1 year ago

thank you This is a great technique, and I will learn to master it