supabase / supabase-py

Python Client for Supabase. Query Postgres from Flask, Django, FastAPI. Python user authentication, security policies, edge functions, file storage, and realtime data streaming. Good first issue.
https://supabase.com/docs/reference/python
MIT License
1.65k stars 191 forks source link

Connection Keep Alive #783

Open StanGirard opened 4 months ago

StanGirard commented 4 months ago

Hey !

It seems that the TLS Handshake happens every query.

image

Implementing a Keep-alive and keeping the connection alive would save around 60ms per query.

juancarlospaco commented 4 months ago

is it really required? 🤔

Also, it should be ON by default whatsoever, because it is hardcoded:

It is interesting nevertheless, will keep investigating...

StanGirard commented 4 months ago

Hey @juancarlospaco,

I've been battling with it for almost a week.

Going with direct connection to the database with RAW SQL queries makes it way faster. I looked into Kong, it should be around 8ms of latency not 40ms.

There might be something funny happening.

image

Here you can see the time when I do queries directly to database and when using the python sdk.

Would love to help you investigate.

Both my backend and my supabase are in Ireland on AWS.

juancarlospaco commented 4 months ago

I can confirm it is not the "keep-alive" by itself IMHO, something funny is happening somehow, will keep investigating...

StanGirard commented 4 months ago

@juancarlospaco Thanks Juan! Let me know if i can help anyhow

StanGirard commented 3 months ago

Hello @juancarlospaco, any update of this issue ?

juancarlospaco commented 3 weeks ago

As explained at https://github.com/supabase/supabase-py/issues/783#issuecomment-2087544554 it is enabled and hardcoded from httpx, and basically nowadays it is mostly ignored whatsoever.

Make sure you are not running too old libs, or running in an env in debug mode (sometimes things are slower or different in debuggers or with debug mode active etc).

In the latest version of the clients, it has HTTP2 enabled which ignores all of this and uses persistent connections by default.