sanic icon indicating copy to clipboard operation
sanic copied to clipboard

[Feature Request] KEEP_ALIVE_CONNECTIONS

Open halaei opened this issue 6 years ago • 3 comments

It will be nice if a keepalive config similar to what nginx upstream has gets added to sanic. Basically, it limits the number of idle connections, and closes the least recently used one when the limit is exceeded. I am not entirely sure, but I think it will be more useful than the current KEEP_ALIVE_TIMEOUT config.

reference: http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive

halaei avatar Jul 24 '18 07:07 halaei

Hi! Is there anyone working on that? I'd like to try to implement that, but not sure if that's desired by core developers.

Also, extra thoughts on realisation appreciated.

LittleLampLight avatar Dec 18 '18 06:12 LittleLampLight

@LittleLampLight It does sound like something that should not be too difficult to add.

Take a look at https://github.com/huge-success/sanic/blob/master/sanic/server.py, and you will see that connections currently are handled as a set(). I guess we would need to store them in some sort of an LRU cache instead. Using the standard library module would likely not be enough because we need to remove them ONLY if they are idle, and it probably would not be efficient to maintain both idle and active connections in a separate structure. But, we can discuss implementation details separately.

I am also curious to see if this is something that any of the ASGI frameworks have already implemented. We plan to make a push towards becoming ASGI compliant after the 1st of the year.

So, as for whether it is or is not a worthwhile use of your time, the answer sort of depends to what degree we adopt and push ASGI. However, it is my personal opinion that even if we jumped fully into that bucket, we will still be supporting and there will still be a need to handle this inside Sanic itself for some time. And if so, as long as there were no performance penalties, I see this as a worthwhile enhancement.

ahopkins avatar Dec 25 '18 09:12 ahopkins

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.

stale[bot] avatar May 14 '19 23:05 stale[bot]