Skip to content

Option for turning off HTTPRequestRetry #2770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chinefed opened this issue Jan 4, 2023 · 4 comments
Closed

Option for turning off HTTPRequestRetry #2770

chinefed opened this issue Jan 4, 2023 · 4 comments

Comments

@chinefed
Copy link

chinefed commented Jan 4, 2023

I would introduce an optional boolean parameter to turn off the HTTPRequestRetry middleware when creating a HTTPProvider. This would provide users with the possibility of implementing a custom retrial logic in their code.

@kclowes
Copy link
Collaborator

kclowes commented Jan 4, 2023

You can just remove it and add your own. See the docs here

@kclowes kclowes closed this as completed Jan 4, 2023
@chinefed
Copy link
Author

chinefed commented Jan 4, 2023

I've tried to do this, but it doesn't work. Even w3.middleware_onion.clear() seems not to turn off HTTPRequestRetry. My code continues to run even if Alchemy has issued some maximum throughput error responses (I can see them from Alchemy's portal). If HTTPRequestRetry was off, the code should rise an exception and stop when receiving an error response.

Code:

w3 = Web3(Web3.HTTPProvider(ENDPOINT_URL))
if w3.isConnected()==False:
    print('Connection error')
w3.middleware_onion.clear()

Even if there are error responses...

Screenshot 2023-01-04 at 10 03 32 PM

... the code continues to run:

Screenshot 2023-01-04 at 10 05 05 PM

Furthermore, if I try:

from web3 import Web3
from web3.middleware import http_retry_request_middleware

# Connect to endpoint
w3 = Web3(Web3.HTTPProvider(ENDPOINT_URL))
if w3.isConnected()==False:
    print('Connection error')
w3.middleware_onion.remove(http_retry_request_middleware)

I get this error:
Value Error:You can only remove something that has been added

Indeed, HTTPRequestRetry is not listed if I try w3.middleware_onion.middlewares.

@kclowes kclowes reopened this Jan 4, 2023
@EyalMuga
Copy link

This could be due to the fact that the HTTPRequestRetry middleware is added by default to the Web3 instance when it's created, and it is not possible to remove it by using w3.middleware_onion.remove(http_retry_request_middleware).
try to create a new Web3 instance without the HTTPRequestRetry middleware, by creating your own provider class that doesn't include it.

@kclowes
Copy link
Collaborator

kclowes commented Jun 8, 2023

@EyalMuga you're right. The http_retry_request_middleware is actually on the provider, not the middleware_onion. So @chinefed you should be able to remove it with w3.provider.middleware = []. I'm going to close this, but we're going to add better UX in v7 (probably a flag on the provider).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants