Skip to content

Commit c2940c1

Browse files
spacewanderthibaultcha
authored andcommitted
feature: added backlog queueing to cosocket connection pools.
Now we can pass 'pool_size' and 'backlog' options in connect method to specify the pool size and the queue size of the connection pool. When there is more connect operations than the pool_size, new connect operation will be queued until former connect operation fails or connection finalizes. When there is more queueing connect operations than the backlog, new connect operation will be dropped. This pr also migrated connection pool creation to connect method. Note that the meaning of `active_connections` is changed from the previous setkeepalive mechanism. The connection pool is destroyed only if all related connections closed now. Signed-off-by: Thibault Charbonnier <thibaultcha@me.com>
1 parent 2fb8f12 commit c2940c1

File tree

5 files changed

+2292
-328
lines changed

5 files changed

+2292
-328
lines changed

README.markdown

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7091,6 +7091,43 @@ An optional Lua table can be specified as the last argument to this method to sp
70917091
* `pool`
70927092
specify a custom name for the connection pool being used. If omitted, then the connection pool name will be generated from the string template `"<host>:<port>"` or `"<unix-socket-path>"`.
70937093

7094+
* `pool_size`
7095+
specify the size of the connection pool. If omitted and no
7096+
`backlog` option was provided, no pool will be created. If omitted
7097+
but `backlog` was provided, the pool will be created with a default
7098+
size equal to the value of the [lua_socket_pool_size](#lua_socket_pool_size)
7099+
directive.
7100+
The connection pool holds up to `pool_size` alive connections
7101+
ready to be reused by subsequent calls to [connect](#tcpsockconnect), but
7102+
note that there is no upper limit to the total number of opened connections
7103+
outside of the pool. If you need to restrict the total number of opened
7104+
connections, specify the `backlog` option.
7105+
When the connection pool would exceed its size limit, the least recently used
7106+
(kept-alive) connection already in the pool will be closed to make room for
7107+
the current connection.
7108+
Note that the cosocket connection pool is per Nginx worker process rather
7109+
than per Nginx server instance, so the size limit specified here also applies
7110+
to every single Nginx worker process. Also note that the size of the connection
7111+
pool cannot be changed once it has been created.
7112+
This option was first introduced in the `v0.10.14` release.
7113+
7114+
* `backlog`
7115+
if specified, this module will limit the total number of opened connections
7116+
for this pool. No more connections than `pool_size` can be opened
7117+
for this pool at any time. If the connection pool is full, subsequent
7118+
connect operations will be queued into a queue equal to this option's
7119+
value (the "backlog" queue).
7120+
If the number of queued connect operations is equal to `backlog`,
7121+
subsequent connect operations will fail and return `nil` plus the
7122+
error string `"too many waiting connect operations"`.
7123+
The queued connect operations will be resumed once the number of connections
7124+
in the pool is less than `pool_size`.
7125+
The queued connect operation will abort once they have been queued for more
7126+
than `connect_timeout`, controlled by
7127+
[settimeouts](#tcpsocksettimeouts), and will return `nil` plus
7128+
the error string `"timeout"`.
7129+
This option was first introduced in the `v0.10.14` release.
7130+
70947131
The support for the options table argument was first introduced in the `v0.5.7` release.
70957132

70967133
This method was first introduced in the `v0.5.0rc1` release.
@@ -7422,13 +7459,31 @@ Puts the current socket's connection immediately into the cosocket built-in conn
74227459

74237460
The first optional argument, `timeout`, can be used to specify the maximal idle timeout (in milliseconds) for the current connection. If omitted, the default setting in the [lua_socket_keepalive_timeout](#lua_socket_keepalive_timeout) config directive will be used. If the `0` value is given, then the timeout interval is unlimited.
74247461

7425-
The second optional argument, `size`, can be used to specify the maximal number of connections allowed in the connection pool for the current server (i.e., the current host-port pair or the unix domain socket file path). Note that the size of the connection pool cannot be changed once the pool is created. When this argument is omitted, the default setting in the [lua_socket_pool_size](#lua_socket_pool_size) config directive will be used.
7426-
7427-
When the connection pool exceeds the available size limit, the least recently used (idle) connection already in the pool will be closed to make room for the current connection.
7428-
7429-
Note that the cosocket connection pool is per Nginx worker process rather than per Nginx server instance, so the size limit specified here also applies to every single Nginx worker process.
7430-
7431-
Idle connections in the pool will be monitored for any exceptional events like connection abortion or unexpected incoming data on the line, in which cases the connection in question will be closed and removed from the pool.
7462+
The second optional argument `size` is considered deprecated since
7463+
the `v0.10.14` release of this module, in favor of the
7464+
`pool_size` option of the [connect](#tcpsockconnect) method.
7465+
Since the `v0.10.14` release, this option will only take effect if
7466+
the call to [connect](#tcpsockconnect) did not already create a connection
7467+
pool.
7468+
When this option takes effect (no connection pool was previously created by
7469+
[connect](#tcpsockconnect)), it will specify the size of the connection pool,
7470+
and create it.
7471+
If omitted (and no pool was previously created), the default size is the value
7472+
of the [lua_socket_pool_size](#lua_socket_pool_size) directive.
7473+
The connection pool holds up to `size` alive connections ready to be
7474+
reused by subsequent calls to [connect](#tcpsockconnect), but note that there
7475+
is no upper limit to the total number of opened connections outside of the
7476+
pool.
7477+
When the connection pool would exceed its size limit, the least recently used
7478+
(kept-alive) connection already in the pool will be closed to make room for
7479+
the current connection.
7480+
Note that the cosocket connection pool is per Nginx worker process rather
7481+
than per Nginx server instance, so the size limit specified here also applies
7482+
to every single Nginx worker process. Also note that the size of the connection
7483+
pool cannot be changed once it has been created.
7484+
If you need to restrict the total number of opened connections, specify both
7485+
the `pool_size` and `backlog` option in the call to
7486+
[connect](#tcpsockconnect).
74327487

74337488
In case of success, this method returns `1`; otherwise, it returns `nil` and a string describing the error.
74347489

doc/HttpLuaModule.wiki

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5979,6 +5979,43 @@ An optional Lua table can be specified as the last argument to this method to sp
59795979
* <code>pool</code>
59805980
: specify a custom name for the connection pool being used. If omitted, then the connection pool name will be generated from the string template <code>"<host>:<port>"</code> or <code>"<unix-socket-path>"</code>.
59815981
5982+
* <code>pool_size</code>
5983+
: specify the size of the connection pool. If omitted and no
5984+
: <code>backlog</code> option was provided, no pool will be created. If omitted
5985+
: but <code>backlog</code> was provided, the pool will be created with a default
5986+
: size equal to the value of the [[#lua_socket_pool_size|lua_socket_pool_size]]
5987+
: directive.
5988+
: The connection pool holds up to <code>pool_size</code> alive connections
5989+
: ready to be reused by subsequent calls to [[#tcpsock:connect|connect]], but
5990+
: note that there is no upper limit to the total number of opened connections
5991+
: outside of the pool. If you need to restrict the total number of opened
5992+
: connections, specify the <code>backlog</code> option.
5993+
: When the connection pool would exceed its size limit, the least recently used
5994+
: (kept-alive) connection already in the pool will be closed to make room for
5995+
: the current connection.
5996+
: Note that the cosocket connection pool is per Nginx worker process rather
5997+
: than per Nginx server instance, so the size limit specified here also applies
5998+
: to every single Nginx worker process. Also note that the size of the connection
5999+
: pool cannot be changed once it has been created.
6000+
: This option was first introduced in the <code>v0.10.14</code> release.
6001+
6002+
* <code>backlog</code>
6003+
: if specified, this module will limit the total number of opened connections
6004+
: for this pool. No more connections than <code>pool_size</code> can be opened
6005+
: for this pool at any time. If the connection pool is full, subsequent
6006+
: connect operations will be queued into a queue equal to this option's
6007+
: value (the "backlog" queue).
6008+
: If the number of queued connect operations is equal to <code>backlog</code>,
6009+
: subsequent connect operations will fail and return <code>nil</code> plus the
6010+
: error string <code>"too many waiting connect operations"</code>.
6011+
: The queued connect operations will be resumed once the number of connections
6012+
: in the pool is less than <code>pool_size</code>.
6013+
: The queued connect operation will abort once they have been queued for more
6014+
: than <code>connect_timeout</code>, controlled by
6015+
: [[#tcpsock:settimeouts|settimeouts]], and will return <code>nil</code> plus
6016+
: the error string <code>"timeout"</code>.
6017+
: This option was first introduced in the <code>v0.10.14</code> release.
6018+
59826019
The support for the options table argument was first introduced in the <code>v0.5.7</code> release.
59836020
59846021
This method was first introduced in the <code>v0.5.0rc1</code> release.
@@ -6273,13 +6310,31 @@ Puts the current socket's connection immediately into the cosocket built-in conn
62736310
62746311
The first optional argument, <code>timeout</code>, can be used to specify the maximal idle timeout (in milliseconds) for the current connection. If omitted, the default setting in the [[#lua_socket_keepalive_timeout|lua_socket_keepalive_timeout]] config directive will be used. If the <code>0</code> value is given, then the timeout interval is unlimited.
62756312
6276-
The second optional argument, <code>size</code>, can be used to specify the maximal number of connections allowed in the connection pool for the current server (i.e., the current host-port pair or the unix domain socket file path). Note that the size of the connection pool cannot be changed once the pool is created. When this argument is omitted, the default setting in the [[#lua_socket_pool_size|lua_socket_pool_size]] config directive will be used.
6277-
6278-
When the connection pool exceeds the available size limit, the least recently used (idle) connection already in the pool will be closed to make room for the current connection.
6279-
6280-
Note that the cosocket connection pool is per Nginx worker process rather than per Nginx server instance, so the size limit specified here also applies to every single Nginx worker process.
6281-
6282-
Idle connections in the pool will be monitored for any exceptional events like connection abortion or unexpected incoming data on the line, in which cases the connection in question will be closed and removed from the pool.
6313+
The second optional argument <code>size</code> is considered deprecated since
6314+
the <code>v0.10.14</code> release of this module, in favor of the
6315+
<code>pool_size</code> option of the [[#tcpsock:connect|connect]] method.
6316+
Since the <code>v0.10.14</code> release, this option will only take effect if
6317+
the call to [[#tcpsock:connect|connect]] did not already create a connection
6318+
pool.
6319+
When this option takes effect (no connection pool was previously created by
6320+
[[#tcpsock:connect|connect]]), it will specify the size of the connection pool,
6321+
and create it.
6322+
If omitted (and no pool was previously created), the default size is the value
6323+
of the [[#lua_socket_pool_size|lua_socket_pool_size]] directive.
6324+
The connection pool holds up to <code>size</code> alive connections ready to be
6325+
reused by subsequent calls to [[#tcpsock:connect|connect]], but note that there
6326+
is no upper limit to the total number of opened connections outside of the
6327+
pool.
6328+
When the connection pool would exceed its size limit, the least recently used
6329+
(kept-alive) connection already in the pool will be closed to make room for
6330+
the current connection.
6331+
Note that the cosocket connection pool is per Nginx worker process rather
6332+
than per Nginx server instance, so the size limit specified here also applies
6333+
to every single Nginx worker process. Also note that the size of the connection
6334+
pool cannot be changed once it has been created.
6335+
If you need to restrict the total number of opened connections, specify both
6336+
the <code>pool_size</code> and <code>backlog</code> option in the call to
6337+
[[#tcpsock:connect|connect]].
62836338
62846339
In case of success, this method returns <code>1</code>; otherwise, it returns <code>nil</code> and a string describing the error.
62856340

0 commit comments

Comments
 (0)