update for tech summit workshop
This commit is contained in:
@@ -34,6 +34,17 @@ func (s *Server) setLimit(w http.ResponseWriter, r *http.Request) {
|
||||
s.RateLimiter = rl
|
||||
}
|
||||
|
||||
func (s *Server) handleClick(w http.ResponseWriter, r *http.Request) {
|
||||
// Sleep for a random amount of time up to 100 milliseconds.
|
||||
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
|
||||
t := r.Context().Value("rate-limit-token").(*Token)
|
||||
s.RateLimiter.Release(t)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Server) getData(w http.ResponseWriter, r *http.Request) {
|
||||
data := []struct {
|
||||
Item1 string
|
||||
@@ -42,7 +53,7 @@ func (s *Server) getData(w http.ResponseWriter, r *http.Request) {
|
||||
}{
|
||||
{
|
||||
Item1: "books",
|
||||
Item2: "hotdogs",
|
||||
Item2: "hot dogs",
|
||||
Item3: "trinkets",
|
||||
},
|
||||
}
|
||||
@@ -53,8 +64,8 @@ func (s *Server) getData(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Sleep for a random amount of time up to 5 seconds.
|
||||
time.Sleep(time.Duration(rand.Intn(500)) * time.Millisecond)
|
||||
// Sleep for a random amount of time up to 1 seconds.
|
||||
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
|
||||
|
||||
w.Write(bytes)
|
||||
|
||||
|
||||
@@ -20,5 +20,12 @@ func (s *Server) BuildRoutes() http.Handler {
|
||||
r.Get("/", s.getData)
|
||||
})
|
||||
|
||||
r.Route("/click", func(r chi.Router) {
|
||||
r.Use(s.rateLimiter)
|
||||
r.Use(s.faulty)
|
||||
|
||||
r.Post("/", s.handleClick)
|
||||
})
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user