tomusdrw / rust-web3

Ethereum JSON-RPC multi-transport client. Rust implementation of web3 library. ENS address: rust-web3.eth
MIT License
1.45k stars 466 forks source link

Detecting rate limiting and transport error codes #565

Open Mirko-von-Leipzig opened 2 years ago

Mirko-von-Leipzig commented 2 years ago

I am currently using eth_getLogs and will occasionally get rate limited. This would be indicated by an error code 429.

Judging by this code, this currently gets "exposed" as an error string

Err(Error::Transport(format!(
            "response status code is not success: {}",
            status
        )

Would it be possible to expose the plain code? This would make it far easier to match on.

--edit-- In addition, Infura adds additional information into the body, which would be useful to have. i.e. the number of seconds to backoff.

tomusdrw commented 2 years ago

I think ideally this kind of throttling should be handled by the transport layer itself. Obviously I'm also fine with exposing this as a typed error. I think Error::Transport would could hold an enum instead of a String?

Mirko-von-Leipzig commented 2 years ago

I think ideally this kind of throttling should be handled by the transport layer itself. I think it can so long as the retry-amount is configurable? I would likely want to log that it happened though.

In general I think exposing the error code and contents in an enum would be fine.

tomusdrw commented 2 years ago

Related: https://github.com/tomusdrw/rust-web3/pull/585

Mirko-von-Leipzig commented 2 years ago

I see 0.18.0 now returns this situation as a TransportError::Code(429) which is great.

One thing that is still missing (I think) is getting the accompanying payload message. Some servers will set the back off time or other useful information.

For example, Infura returns 429 plus a json RPC payload which contains the back off time and current rate-limiting.

Infura rate limit docs: https://docs.infura.io/infura/networks/ethereum/how-to/secure-a-project/set-rate-limits#how-do-i-know-if-im-being-rate-limited