redis / redis-py

Redis Python client
MIT License
12.4k stars 2.48k forks source link

The timeout settings are not working #3291

Open Bor-is-luv opened 1 week ago

Bor-is-luv commented 1 week ago

Hello! I am using Python 3.11.6, redis-py version 4.6.0 and hiredis 2.0.0. My application I run in Docker Python:3.11.6-slim-bookworm. I set up the timeout settings as follows: socket_connect_timeout=0.008 and socket_timeout=0.01. In addition to this, I set the following parameters: socket_keepalive=True, decode_responses=False. After which I began to measure the operation time of MGET. According to the measurement results, I clearly see that the response time sometimes exceeds 0.1 seconds. See screenshot below.

Снимок экрана 2024-06-25 в 11 37 05

My question is why the timeout settings did not work and the MGET operation took 0.1 seconds instead of 0.01 seconds. To solve this problem, I tried: 1) Disabling hiredis. This didn't help. The response time also sometimes exceeds 0.01 seconds. 2) Passing the setting 'socket_keepalive_options': {socket.TCP_USER_TIMEOUT, 3}. This also didn't help. 3) Studying the original client code. I discovered that in the Connection class there is a method called can_read, which has an argument for timeout, which by default is set to 0, meaning that the timeout is absent and the completion of the operation will be awaited infinitely. I used monkey patching and redefined this method to my own, in which case when the timeout is equal to 0, the socket_timeout setting was used. But this also didn't help.

What else can I do? Can this be a bug in the original client code?