Correct way to retry response on 503 in script?
See original GitHub issueEnv: mitmdump 0.18.2 (trying to migrate a script from 0.16)
Hello!
I’m trying to understand how I might use replay_request
or something else to add proxy-level retries on certain errors, such as when a flaky backend throws a 503 error.
I’ve tried this:
$ mitmdump -p 8080 --no-http2 -s myscript.py
myscript.py:
from mitmproxy import ctx
def response(flow):
# Retry once if the backend responded with 503:
if flow.response.status_code == 503:
f = ctx.master.duplicate_flow(flow)
ctx.master.replay_request(f, block=True)
…but this seems to deadlock, I think because I don’t understand the way the main thread processes flows (or how exactly to replace the current flow.response).
Can you point me in the right direction here?
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Status 503: is "Retry-After" necessary? - Stack Overflow
The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay...
Read more >How can I return a 503 status in apache without invoking ...
Hm. I think invoking a whole interpreted scripting language to set a single line of the server response does qualify as hack. Setting...
Read more >Retry-After - HTTP - MDN Web Docs
Retry -After · When sent with a 503 (Service Unavailable) response, this indicates how long the service is expected to be unavailable. ·...
Read more >How to Fix the HTTP Error 503 Service Unavailable - Kinsta
The server MAY send a Retry-After header field to suggest an appropriate amount of time for the client to wait before retrying the...
Read more >503 Service Unavailable Error: What It Is and How to Fix It
Application Code or Script Bugs ... If all else fails, check your code. A bug could be causing the 503 Service Unavailable Error....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s what ended up working, feel free to let me know if something looks off here.
Notably: it seems that the
response
hook fires for every replayed request (which is the opposite of what I read you talk about on the forums), so I had to add a_num_retries
tracker to prevent infinite recursion.I also had to do
flow.response = new_flow.response
, which I’m not sure if that’s correct way to override the top-level response.Thanks for the help!
How to make script like this on 5.x version?