Rate limit without ctx/context

Hi Guys,

I’m new to Go, only because of rate limit webhook. So I got these code,

func rateLimiter(next func(w http.ResponseWriter, r *http.Request, _ httprouter.Params)) httprouter.Handle {
	qps := 1
	burst := 1
	// ctx := context.TODO()
	limit := rate.Limit(qps)
	limiter := rate.NewLimiter(limit, burst)	
	return httprouter.Handle(func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		// for i := uint(1); i < 100; i++ {
			if !limiter.Allow() {
				if r.Method == "POST" {
					log.Println("Blocked by rate limit!")
				// _ = limiter.Wait(ctx)	
			} // else {
				// time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
			next(w, r, nil)
			// }
		// }

My supervisor check it with the use of vscode and bito (AI). The bito suggest to remove the ctx in the rateLimiter as shown above, and the for loop, etc, see above //.

My understanding is, the ctx is the token that differentiate other request, removing it will make the progam accept one and block others, so what if two request happened simulatenously, only one will be accepted right?

Thanks for your help,

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.