Coder Social home page Coder Social logo

leaky_bucket_redis's Introduction

Leaky Bucket Redis (Distributed)

Overview

The leaky_bucket_redis.go is a leaky bucket implementation in Go that utilizes Redis's Lua script. It is designed to work well with distributed systems, managing rate limiting and throttling.

Installation

To use the leaky_bucket_redis.go in your Go project, you need to have Redis installed and running. You can install Redis by following the instructions provided in the official Redis documentation.

Once Redis is installed, you can add the leaky_bucket_redis.go file to your project's source code.

Usage

To use the leaky bucket implementation, follow these steps:

  1. Import the necessary packages in your Go file:
    import (
        "github.com/redis/go-redis/v9"
        "github.com/alibazlamit/leaky_bucket_redis"
    )

LeakyBucketRedis Allow Method

The Allow method in the LeakyBucketRedis struct implements the Leaky Bucket algorithm. It takes a context.Context as input and returns A time.Duration representing the wait time if the request is not allowed.

The method works as follows:

  • It obtains the current time and defines a Lua script for the Leaky Bucket algorithm.
  • It assigns the Redis key and the rate of token consumption from the bucket.
  • It executes the Lua script on the Redis server using the Eval method of the Redis client.
  • It parses the result of the Lua script to a float64 representing the wait time in seconds.
  • If the wait time is less than or equal to 0, the method returns wait time of 0, indicating that the request is allowed.
  • If the wait time is greater than 0, the method returns the calculated wait time, indicating that the request is not allowed.

This implementation allows you to control the rate of requests in your application using the Leaky Bucket algorithm.

If you have any suggestions or find this implementation useful, feel free to drop me a star!

leaky_bucket_redis's People

Contributors

alibazlamit avatar

Stargazers

 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.