title: Rate limit with golang.org/x/time/rate api request
body: <p>I already created a function for limiting to 50 requests for API logins in one day.</p>
  <p>var limit = 50</p>
  <pre><code>package middleware
  
  import (
      &quot;log&quot;
      &quot;net&quot;
      &quot;net/http&quot;
      &quot;sync&quot;
      &quot;time&quot;
  
      &quot;golang.org/x/time/rate&quot;
  )
  
  // Create a custom request struct which holds the rate limiter for each
  // visitor and the last time that the request was seen.
  type request struct {
      limiter  *rate.Limiter
      lastSeen time.Time
  }
  
  // Change the the map to hold values of the type request.
  // defaultTime using 3 minutes
  var requests = make(map[string]*request)
  var mu sync.Mutex
  
  func getRequest(ip string, limit int) *rate.Limiter {
      mu.Lock()
      defer mu.Unlock()
  
      v, exists := requests[ip]
      if !exists {
          limiter := rate.NewLimiter(1, limit)
          requests[ip] = &amp;request{limiter, time.Now()}
          return limiter
      }
      // Update the last seen time for the visitor.
      v.lastSeen = time.Now()
      return v.limiter
  }
  
  func throttle(next http.Handler, limit int) http.Handler {
      return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
          ip, _, err := net.SplitHostPort(r.RemoteAddr)
          if err != nil {
              log.Println(err.Error())
              http.Error(w, &quot;Internal Server Error&quot;, http.StatusInternalServerError)
              return
          }
          limiter := getRequest(ip, limit)
          fmt.Println(limiter.Allow())
          if limiter.Allow() == false {
              http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
              return
          }
          next.ServeHTTP(w, r)
      })
  }
  </code></pre>
  <p>Is it correct?</p>
  <p>Because when I try it, it still passes. The function limit is not working.</p>
  <p>I doubt with NewLimiter()</p>
  <pre><code> limiter := rate.NewLimiter(1, limit)
  </code></pre>
  <p>Does it mean one user only can request login 50 requests per day? (I already read the docs, but I do not understand.)</p>