Giter VIP home page Giter VIP logo

gin-rate-limit's Introduction

gin-limit

Stay under the limit with this handy Gin middleware.

Purpose

By default, http.ListenAndServe (which gin.Run wraps) will serve an unbounded number of requests. Limiting the number of simultaneous connections can sometimes greatly speed things up under load. Inspired by x/net/netutil.LimitListener.

Example

package main

import (
  "github.com/aviddiviner/gin-limit"
  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()
  r.Use(limit.MaxAllowed(20))
  // ...
  r.Run(":8080")
}

Lies, damned lies and statistics

Everyone loves synthetic benchmarks, so have some numbers from my 2015 Macbook (on a fast rendering page; single sqlite query, basic templates).

% wrk -t12 -c400 -d20s http://localhost:4560/
Running 20s test @ http://localhost:4560/
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   848.63ms  525.39ms   1.64s    62.81%
    Req/Sec    45.43     61.85   360.00     90.50%
  8908 requests in 20.10s, 21.91MB read
  Socket errors: connect 0, read 219, write 0, timeout 0
Requests/sec:    443.19
Transfer/sec:      1.09MB

Now 10x faster with limit.MaxAllowed(3) (although that would be higher in the real world). Hooray!

% wrk -t12 -c400 -d20s http://localhost:4560/
Running 20s test @ http://localhost:4560/
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    94.40ms   32.65ms 656.72ms   86.44%
    Req/Sec   351.61     84.32   666.00     79.32%
  84181 requests in 20.09s, 207.05MB read
  Socket errors: connect 0, read 165, write 0, timeout 0
Requests/sec:   4189.75
Transfer/sec:     10.30MB

gin-rate-limit's People

Contributors

aviddiviner avatar base698 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.