Repository navigation
Return redis connections to the worker pool - #59
Conversation
1bd8293 to
2357317
Compare
| }) | ||
| if ok then | ||
| red:select(option.database) | ||
| if red:get_reused_times() == 0 then |
There was a problem hiding this comment.
Isn't select still supposed to happen? The reuse thing is just about the TCP connection afaik.
There was a problem hiding this comment.
https://github.com/ledgetech/lua-resty-redis-connector/issues/41#issuecomment-863246307 says that too:
You cannot share connections across requests in a safe way.
There was a problem hiding this comment.
Oh right, the Claude commit message was rather wordy (I'd prefer if you wrote your own really) but I think it's saying it's safe in the very specific way it's used here? But if so it should say so in a (non-Claude verbose style) comment.
|
Yes, that is it. The connector thread is about holding a connection object across requests. I will add the comment and rewrite those commit messages. The skip is also droppable. Running |
|
I think the select should be fairly negligible compared to the overhead of starting up a TCP connection, but measuring is always better. |
pool = 5 was already set in DbSettings and unused. set_keepalive puts the socket back for the next request instead of dropping it, and SELECT then only runs on a new one. One pool per database keeps dev, test and prod apart. Release happens around the action because gin turns raise_error into a response through pcall, and nginx closes cosockets before log_by_lua.
2357317 to
a03e923
Compare
DbSettingsdeclarespool = 5for every environment and nothing reads it. Connections are dropped at the end of each request, so every request pays a TCP handshake and aSELECT.set_keepalivereturns the socket to OpenResty's per-worker pool, sized from that setting and held for 60 seconds.config/redis.confsetstimeout 0, so redis never closes one first.The pool is named
host:port/database, so a connection taken from it has already selected the database its name states, andSELECTruns only whenget_reused_times()is zero. Development, test and production are databases 3, 2 and 1 on one server and cannot share a pool.Release happens in the content phase, wrapped around each action rather than at each return: gin turns
raise_errorinto a response through its ownpcall, so an action has no single return point, and nginx finalizes cosockets beforelog_by_luaruns, whereset_keepalivefails with "closed".Measured over 121 requests:
SELECTfalls to one per nginx worker. Throughput 1,565 to 3,583 requests per second serially and 8,417 to 20,965 at eight connections, p50 630 to 279 microseconds, five interleaved repetitions paired against master.28 tests pass. The busted harness starts and stops nginx around every request, so no two requests share a worker and it cannot exercise pooling; the command counts above come from a running server instead.
Builds on #58, and is the smaller half of the gain without it.
This change is