Giter Club home page Giter Club logo

ratelimiter's Introduction

Rate Limiter

A customizable rate limiter for Go applications

Rate Limiter empowers you to control the rate of incoming requests in your Go applications, safeguarding against excessive traffic and potential overload. It offers a flexible configuration to tailor its behavior to your specific needs.

Key features

  • Customizable threshold: Define the maximum number of requests permitted within a specified duration.
  • Customizable duration: Set the time window within which the threshold is applied.
  • Reset: Clear the rate limiter's internal state, enabling a fresh start.

Installation

To integrate Rate Limiter into your project, execute the following command in your terminal:

go get github.com/bitsexplained/ratelimiter

Usage

  • Here's a practical example showcasing Rate Limiter's usage:
   package main

   import (
      "fmt"
      "github.com/bitsexplained/ratelimiter"
      "time"
   )

   func main() {
      // Create a new rate limiter with a threshold of 500 requests per second
      rl := ratelimiter.NewRateLimiter(
         ratelimiter.WithThreshold(500),
         ratelimiter.WithDuration(time.Second),
      )

      // Allow a request
      if rl.Allow() {
         // Process the request
         fmt.Println("Request allowed")
      } else {
         // Reject the request
         fmt.Println("Request rejected")
      }
   }

Public Interface

Rate Limiter exposes two essential methods for interaction:

  • go Allow() bool: Determines whether a request is permissible based on the configured threshold and duration.

  • Reset() : Clears the rate limiter's internal state, effectively starting anew.

Configuration

  • Customize Rate Limiter's behavior using these configuration options:

  • WithThreshold(threshold int): Sets the maximum allowable requests within the specified duration.

  • WithDuration(duration time.Duration): Establishes the time window for applying the threshold.

Initializing the Rate Limiter

Construct a Rate Limiter instance using the NewRateLimiter function, seamlessly chaining configuration options:

   rl := ratelimiter.NewRateLimiter(
  		ratelimiter.WithThreshold(500),
  		ratelimiter.WithDuration(time.Second),
	)

Resetting the Rate Limiter

To clear the rate limiter's internal state, employ the Reset() method:

   rl.Reset()

Contributing

Your contributions to Rate Limiter are warmly welcomed! If you encounter bugs, have feature requests, or wish to contribute code, please create an issue or submit a pull request to the GitHub repository.

License

rate limiter library is released under the MIT License. See LICENSE for details.

ratelimiter's People

Contributors

muathendirangu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.