leakybucket icon indicating copy to clipboard operation
leakybucket copied to clipboard

Missing documentation for real-world use

Open dpup opened this issue 4 years ago • 1 comments

This library looks exactly what we need. It would be helpful to have guidance on expected usage. For example, if you were to implement IP rate limits or per user limits. It's not clear whether it is ok for the bucket to be request scoped.

Is this correct?

func handleRequest(req app.Request) (app.Response, error) {
  bucket, err := storage.Create(req.UserID, 100, time.Minute)
  if err != nil {
      return nil, InternalError
  }
  if err := bucket.Add(1); err == ErrorFull {
    return nil, ResourceExhausted
  } else if err != nil {
    return nil, InternalError
  }

  // handle request

  return resp
}

Thanks in advance.

dpup avatar Jul 30 '20 23:07 dpup

Also, from the implementation it looks like this isn't intended to be used in a distributed environment where multiple instances may be writing to the same bucket.

dpup avatar Jul 31 '20 17:07 dpup