Skip to content

Instantly share code, notes, and snippets.

@oadam
Last active November 5, 2015 12:48
Show Gist options
  • Save oadam/70b12005a8c65a15dbe3 to your computer and use it in GitHub Desktop.
Save oadam/70b12005a8c65a15dbe3 to your computer and use it in GitHub Desktop.
Delay proxy between services
#!/bin/bash
#clear previous value
tc qdisc del dev eth1 root
# set delay based on received argument
tc qdisc add dev eth1 root handle 1:0 netem delay $1

Objective

Use this if you want to simulate a delay between 2 services running on your local machine (for example between an application server and a database).

Description

We'll setup a Vagrant virtual machine running haproxy. An artifical delay can then be added using netem

Steps

  • Unzip this gist in a directory.
  • Edit haproxy.cfg to setup the port forwarding
  • Go to that directory and run vagrant up
  • Launch a shell in the vm with vagrant ssh
  • Start haproxy with /usr/sbin/haproxy -D -f /vagrant/haproxy.cfg
  • You can now access the proxy at 192.168.50.4

Adjust delay

From the vagrant machine, run sudo bash /vagrant/adjust-delay.sh 150ms to set the delay to 150ms. To disable it just set it to 0ms. To check that the delay is working, you can use this command (adjust the port number) from another linux machine sudo traceroute -T -p 1521 192.168.50.4

defaults
mode tcp
timeout connect 5000ms
timeout client 500000ms
timeout server 500000ms
#ports can be changed here. I was using oracle so I used 1521
listen http-in
bind *:1521
server server1 10.0.2.2:1521 maxconn 32
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "trusty"
config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
config.vm.network "private_network", ip: "192.168.50.4"
config.vm.provision "shell", inline: "sudo apt-get update && sudo apt-get install -y haproxy"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment