openresty / lua-resty-mysql

Nonblocking Lua MySQL driver library for ngx_lua or OpenResty
706 stars 236 forks source link

How to control the total number of mysql connections #16

Open ghost opened 10 years ago

ghost commented 10 years ago

The function connect will create a new mysql connection or select a free connection from pool! If there are too many connection using, I don't want it to return immediately until another connection is released! How to do it?

agentzh commented 10 years ago

@Samin-Du I've already been working on connection-pool-based concurrency level limiting of the underlying cosocket API in the pool-limiting branch of lua-nginx-module: https://github.com/openresty/lua-nginx-module/tree/pool-limiting

The first step is to return an error immediately when exceeding the pool size limit so that you can use ngx.sleep to poll connect() yourself. This part is already there. The second step is to wait via an internal queue in connect() itself, which won't introduce extra waiting latency as with ngx.sleep and is easier to use.

Because it works on the (lower) cosocket level, every higher level Lua library like lua-resty-mysql and lua-resty-redis will enjoy this feature out of the box.