From 2e2d9b33a4d0d866e37d7374a248edd726c0141e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 1 Feb 2021 17:30:43 +0100 Subject: [PATCH] contrib: Add a mitmproxy script which can be used to test out request retrying --- contrib/mitmproxy/failures.py | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 contrib/mitmproxy/failures.py diff --git a/contrib/mitmproxy/failures.py b/contrib/mitmproxy/failures.py new file mode 100644 index 000000000..10dc18989 --- /dev/null +++ b/contrib/mitmproxy/failures.py @@ -0,0 +1,47 @@ +""" +A mitmproxy script that introduces certain request failures in a deterministic +way. + +Used mainly for Matrix style requests. + +To run execute it with mitmproxy: + + >>> mitmproxy -s failures.py` + +""" +import time +import json + +from mitmproxy import http +from mitmproxy.script import concurrent + +REQUEST_COUNT = 0 + + +@concurrent +def request(flow): + global REQUEST_COUNT + + REQUEST_COUNT += 1 + + if REQUEST_COUNT % 2 == 0: + return + elif REQUEST_COUNT % 3 == 0: + flow.response = http.HTTPResponse.make( + 500, + b"Gateway error", + ) + elif REQUEST_COUNT % 7 == 0: + if "sync" in flow.request.pretty_url: + time.sleep(60) + else: + time.sleep(30) + else: + flow.response = http.HTTPResponse.make( + 429, + json.dumps({ + "errcode": "M_LIMIT_EXCEEDED", + "error": "Too many requests", + "retry_after_ms": 2000 + }) + )