# Throttling feature

**URL:** <https://discourse.mitmproxy.org/t/throttling-feature/453>\
**Category:** help\
**Created:** [April 24, 2017, 3:42am UTC](https://discourse.mitmproxy.org/t/throttling-feature/453 "2017-04-24T03:42:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![internety](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/internety/32/27_2.png) [@internety](https://discourse.mitmproxy.org/u/internety)\
**Post date:** [April 24, 2017, 3:42am UTC](https://discourse.mitmproxy.org/t/throttling-feature/453/1 "2017-04-24T03:42:42Z")

</div>

I would appreciate any hints on how to implement connection throttling. I have to cap speed available to some particular connections. There is no need to cap any other connections within same cli-srv session, so it would be best if I could define caps on per-connection basis. …

---

<div class="post-metadata">

**Author:** ![mhils](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mhils/32/7_2.png) [@mhils](https://discourse.mitmproxy.org/u/mhils)\
**Post date:** [April 24, 2017, 2:40pm UTC](https://discourse.mitmproxy.org/t/throttling-feature/453/2 "2017-04-24T14:40:50Z")

</div>

Generally speaking, that could be tricky. Your best bet is looking at mitmproxy.net.tcp’s Reader.

---

<div class="post-metadata">

**Author:** ![jokester](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/jokester/32/371_2.png) [@jokester](https://discourse.mitmproxy.org/u/jokester)\
**Post date:** [November 7, 2018, 6:12am UTC](https://discourse.mitmproxy.org/t/throttling-feature/453/3 "2018-11-07T06:12:13Z")

</div>

Sorry for bumping but I may found something close to OP asked.

This is a script I used to test browser behavior on particularly slow css / js / etc. It adds a delay before streaming response body (it’s per-request, not connection).

Maybe one can base the condition of delay on `flow.client_conn`, and do a real throttle.

```auto
# usage: mitmproxy -s THIS_FILE.py
# add a delay before starting streaming response body to client 

from time import sleep
from urllib.parse import urlparse

def delay_before_streaming_response(flow):
    url = urlparse(flow.request.url)
    if url.path.lower().endswith('.css'):
        return 2
    return 0

def responseheaders(flow):
    def modify(chunks):
        sleep(delay_before_streaming_response(flow))
        # continue to stream original response
        yield from chunks
    flow.response.stream = modify

```
