buger / goreplay

GoReplay is an open-source tool for capturing and replaying live HTTP traffic into a test environment in order to continuously test your system with real data. It can be used to increase confidence in code deployments, configuration changes and infrastructure changes.
https://goreplay.org
Other
18.59k stars 17 forks source link

Middleware Replay Events Not Triggering Correctly with Rate Limiting in Gor #1253

Open ohs30359-nobuhara opened 3 months ago

ohs30359-nobuhara commented 3 months ago

Issue Description: I am currently using a simple server and middleware for testing with Gor. The server code is as follows:

func handler(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    body := r.URL.RawQuery
    fmt.Println(body)
    w.Write([]byte(body))
}

func main() {
    http.HandleFunc("/", handler)

    port := "8080"
    if len(os.Args) > 1 {
        port = os.Args[1]
    }
    if err := http.ListenAndServe(":"+port, nil); err != nil {
        panic(err)
    }
}

The middleware is based on a sample from the Gor documentation:

gor.on("request", function(req) {
    gor.on("response", req.ID, function(resp) {
        gor.on("replay", req.ID, function(repl) {
            console.error("replay", req.ID)
            return repl;
        })
        return resp;
    })
    return req;
})

I run Gor with the following command:

sudo gor --input-raw :3000 --output-http "http://localhost:8000|10%" --middleware ./middleware/test.js --output-http-track-response --input-raw-track-response --http-allow-method GET --input-raw-buffer-size 1048576 --input-raw-override-snaplen

When I send 100 requests per second using Apache Bench:

ab -n 100 -c 100 "http://localhost:3000/?sample=aaa"

I can confirm that the target server receives exactly 10 requests, as expected due to the 10% rate limit:

sample=aaaaaa
sample=aaaaaa
sample=aaaaaa
sample=aaaaaa
sample=aaaaaa
sample=aaaaaa
sample=aaaaaa
sample=aaaaaa
sample=aaaaaa
sample=aaaaaa

However, in the middleware, only 2 replay events are triggered:

replay f0e40bb87f000001fd11521f
replay f0ea0bb87f0000014d46b09c

This issue does not occur if I remove the 10% rate limit.

Question:

Could you help me understand why only 2 replay events are being triggered in the middleware when the rate limit is applied? How can this be resolved to ensure all replay events are triggered correctly?