add: use SO_REUSEPORT on platform supporting it#4703
Conversation
c252511 to
27c16d7
Compare
|
This is failing all tests, I'd suggest to put these type of PRs as draft. |
Hmm... worked before latest push. Will switch to draft and fix. |
@steve-chavez - it looks like the issue is that on my machine PostgREST startup is fast enough so that it loads the schema cache before it accepts any requests. Here in CI the new instance fails with The question: is there any particular reason why we return |
We were aiming to have requests wait instead of 503, this waiting does happen during schema cache reload but not on startup; we discussed this on #4129. Would it be better to not listen on the socket? How would clients behave in this case? |
|
They would get a "connection refused" error, which means nobody there and is imo more confusing that any 5xx error. UX-wise I would prefer some waiting to a presumably hard fail any day. |
I am not convinced, see below. This is a complex topic so let's dig into it a little more. The startup sequence right now is:
The alternatives are:
b - listening on a socket only after schema cache loaded
So from the point of view of the clients (they don't know when Postgrest was started), we have 3 alternatives:
I am not sure what value clients get from the first two options comparing to the third one. Diagnostics and readiness checks should be done using In case of
So the first two options cause disruptions whereas the third one is fully zero-downtime and transparent to the clients. My take on it would be:
This would require splitting binding from listening on the main socket (ie. we need to bind without listening first so that we can pass the socket to the admin server). @steve-chavez @develop7 thoughts? |
|
Dependent on resolving (or having a workaround to) yesodweb/wai#853 |
64bb6ca to
2cb6503
Compare
Agree, sounds much better. |
7d7375a to
bd4c7ec
Compare
2b8643e to
6c64080
Compare
033bd60 to
127490e
Compare
|
Conflicted in the changelog. |
| mainSocketRef <- newIORef Nothing | ||
| adminSocket <- initAdminServerSocket conf | ||
|
|
||
| let closeSockets = do | ||
| whenJust adminSocket NS.close | ||
| NS.close mainSocket | ||
| readIORef mainSocketRef >>= foldMap NS.close | ||
| Unix.installSignalHandlers observer closeSockets (AppState.schemaCacheLoader appState) (AppState.readInDbConfig False appState) | ||
|
|
||
| Admin.runAdmin appState adminSocket (readIORef mainSocketRef) (serverSettings conf) | ||
|
|
||
| Listener.runListener appState | ||
|
|
||
| Admin.runAdmin appState adminSocket mainSocket (serverSettings conf) | ||
| -- Kick off and wait for the initial SchemaCache load before creating the | ||
| -- main API socket. | ||
| AppState.schemaCacheLoader appState | ||
| AppState.waitForSchemaCacheLoaded appState | ||
|
|
||
| mainSocket <- initServerSocket conf | ||
| atomicWriteIORef mainSocketRef $ Just mainSocket |
There was a problem hiding this comment.
@mkleczek Was the startup sequence change you mentioned on #4703 (comment), done here?
|
|
||
| Zero-Downtime Upgrades | ||
| ====================== | ||
|
|
There was a problem hiding this comment.
| :author: `mkleczek <https://github.com/mkleczek>`_ |
We've been doing this for almost all how-tos:
|
|
||
| The TCP port to bind the web server. Use ``0`` to automatically assign a port. | ||
|
|
||
| On operating systems that support ``SO_REUSEPORT``, you can start multiple |
There was a problem hiding this comment.
Let's put a heading and anchor here so we can link it from other places
| On operating systems that support ``SO_REUSEPORT``, you can start multiple | |
| .. _reuseport: | |
| SO_REUSEPORT | |
| ~~~~~~~~~~~~~ | |
| On operating systems that support ``SO_REUSEPORT``, you can start multiple |
0e7d4aa to
680c6e1
Compare
c12c6fd to
bec258c
Compare
5641f65 to
71ce2e3
Compare
This change ensures PostgREST starts listening on a server socket only after it loaded the schema cache and is ready to handle requests. It is no longer going to return 503 errors during startup until the schema cache is loaded.
DISCLAIMER:
This commit was authored entirely by a human without the assistance of LLMs.
Fixes #4694
Stacked on top of #4702 as it is not enough to start a new instance, it is also necessary not to fail in-flight requests on the old instance.