Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rate Limiting with HAproxy
[root@ip-172-31-20-63 centos]# cat /etc/haproxy/haproxy.cfg
defaults
option http-server-close
mode http
timeout http-request 5s
timeout connect 5s
timeout server 10s
timeout client 30s
#
# http(s)://api.example.com/***
#
frontend main *:80
#
# Start counting and track in intervals of 1 seconds
#
stick-table type ip size 5000k expire 30s store conn_cur,conn_rate(1s)
#
# Limit the number of connections per user:
#
tcp-request connection reject if { src_conn_cur ge 5 }
#
# Limit the number of requests per second:
#
tcp-request connection reject if { src_conn_rate ge 10 }
#
# Setup "tracking" and use "src_" as the variable:
#
tcp-request connection track-sc1 src
#
# https://api.example.com/https://api.gdax.com
#
acl gdax-in-path url_beg /https://api.gdax.com
use_backend proxy-nodes if gdax-in-path
#
# https://api.example.com/https://api.kraken.com/
#
acl kraken-in-path url_beg /https://api.kraken.com
use_backend proxy-nodes if kraken-in-path
##################################################################################################
#
# Declare servers to send load balanced requests to:
#
#################################################################################################
backend proxy-nodes
balance roundrobin
server server1 54.244.4.3:8080 check
server server2 34.219.141.218:8080 check
@mateothegreat

This comment has been minimized.

Copy link
Owner Author

commented May 21, 2018

[root@ip-172-31-20-63 haproxy]# ab -n 100 -c 2 -r  -H 'X-Requested-With: asdf' http://127.0.0.1/https://api.gdax.com/products/BTC-USD/bookddddd
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient).....done


Server Software:
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /https://api.gdax.com/products/BTC-USD/bookddddd
Document Length:        29 bytes

Concurrency Level:      2
Time taken for tests:   6.066 seconds
Complete requests:      100
Failed requests:        150
   (Connect: 0, Receive: 50, Length: 50, Exceptions: 50)
Write errors:           0
Non-2xx responses:      50
Total transferred:      56950 bytes
HTML transferred:       1450 bytes
Requests per second:    16.48 [#/sec] (mean)
Time per request:       121.326 [ms] (mean)
Time per request:       60.663 [ms] (mean, across all concurrent requests)
Transfer rate:          9.17 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0  116 145.6    121     466
Waiting:        0  116 145.6    121     466
Total:          0  116 145.6    121     466

Percentage of the requests served within a certain time (ms)
  50%    121
  66%    138
  75%    148
  80%    165
  90%    390
  95%    404
  98%    423
  99%    466
 100%    466 (longest request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.