How To Simulate A Delay With HAProxy

Introduction

Recently, I’ve been working on an angular app. One issue I had was I couldn’t see the loading bar on prod. I didn’t detect this problem in development because on local, the services loaded so fast.

So, since I was using HAProxy, I thought there should be some config to add some delays to the response. Turned out there is.

Add delays to HAProxy

The trick is to add the following line to your frontend (in haproxy.cfg)

frontend sso
# Other configs
  tcp-request inspect-delay 1000ms
  tcp-request content accept if WAIT_END

 # additional config

In the example above, I added a 1-second delay to all requests going through that frontend.

Here is the complete config of my frontend section:

frontend sso
  bind :80
  mode tcp
  acl is_options method OPTIONS
  tcp-request inspect-delay 1000ms
  tcp-request content accept if WAIT_END
  use_backend cors_backend if is_options
  default_backend main_backend

Now I can detect the bug locally and fix the issue as soon as I see it.

Conclusion

That’s all for the post. It’s a neat trick to add delays to your APIs. If you need a more sophisticated setup (delay for certain route/path), you may want to tinker with other options of tcp-request

Leave a Comment