Giter Club home page Giter Club logo

bucketgo's Introduction

Token Bucket List

This page explains how the code works, if you wanna know what is the pourpose of a rate limiter and its detail you should checkout my text on medium or dev.to

Setup

To execute the bucket list script all you need to have is Golang.

I am using go1.18.3 darwin/arm64 not sure how it will perform on other Go versions and OS but I think it is not going to be a problem. Let me know if works on differents system configuration.

To execute the whole environment you'll need:

  • Docker
  • Docker Compose
  • K6 CLI
  • Go Compiler

How to

First run docker-compose upto run the environment. You should have running 3 services:

  • Token Bucket Service
  • Prometheus
  • Grafana

Make requests on http://localhost:8080 (it can be from your very browser) you should recieve a 200 and if you spam requests should start receiving some 429. This is great!

http://localhost:9090 should render the prometheus page, in this page you can query the Metrics you created on prometheus

http://localhost:3000 goes to the graphana webpage, for loggin in, use username admin and password admin. Add prometheus as data source and create a dashboard to see your metrics :)

To stress test the application simply run k6 run stressTest.js รŸ

Code Analysis

Dependencies

The only dependency is the client of prometheus for the sake of developing metrics, the prometheus client comes with other dependencies that can be analyized on the go.mod file

Prometheus Metrics

There are only 2 metrics that the program generates:

  • http_request_conumed_tokens: Indicates how much tokens are consumed therefore indicating a successful request on the rate limiter

  • http_request_denied_requests: Indicates requests that are denied due to lack of tokens.

Details

Basically the code runs a goroutine that keeps adding tokens, if the tokens added are above the maxToken then it overflow the tokens to be added. It exposes an endpoint that is only responsible to consume a token if possible and return Http Status Code 200 otherwise, return a 429 Too Many Requests

bucketgo's People

Stargazers

Pedro Vallese avatar Homayoon Alimohammadi avatar

Watchers

Pedro Vallese avatar

bucketgo's Issues

TakeTokens is not thread safe

Since for each request to the root url the TakeTokens function is going to be called, the b.Tokens value is going to decrement but not in a concurrency safe way. Try bombarding the server with concurrent requests and you might get lucky and make additional requests. Implementing a lock should solve the issue.

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.