Skip to content

Commit 4d20e30

Browse files
authored
bugfix: support socket connection pool and fix repeated ssl_handshake().
1 parent eba3e97 commit 4d20e30

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
sudo: required
2-
dist: bionic
2+
dist: focal
33

44
os: linux
55

README.markdown

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,44 @@ An optional Lua table can be specified as the last argument to this method to sp
385385
* `pool`
386386

387387
Specifies 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>`.
388+
* `pool_size`
389+
390+
specify the size of the connection pool. If omitted and no
391+
`backlog` option was provided, no pool will be created. If omitted
392+
but `backlog` was provided, the pool will be created with a default
393+
size equal to the value of the [lua_socket_pool_size](https://github.com/openresty/lua-nginx-module/tree/master#lua_socket_pool_size)
394+
directive.
395+
The connection pool holds up to `pool_size` alive connections
396+
ready to be reused by subsequent calls to [connect](#client:connect), but
397+
note that there is no upper limit to the total number of opened connections
398+
outside of the pool. If you need to restrict the total number of opened
399+
connections, specify the `backlog` option.
400+
When the connection pool would exceed its size limit, the least recently used
401+
(kept-alive) connection already in the pool will be closed to make room for
402+
the current connection.
403+
Note that the cosocket connection pool is per Nginx worker process rather
404+
than per Nginx server instance, so the size limit specified here also applies
405+
to every single Nginx worker process. Also note that the size of the connection
406+
pool cannot be changed once it has been created.
407+
This option was first introduced in the `v0.10.14` release.
408+
409+
* `backlog`
410+
411+
if specified, this module will limit the total number of opened connections
412+
for this pool. No more connections than `pool_size` can be opened
413+
for this pool at any time. If the connection pool is full, subsequent
414+
connect operations will be queued into a queue equal to this option's
415+
value (the "backlog" queue).
416+
If the number of queued connect operations is equal to `backlog`,
417+
subsequent connect operations will fail and return `nil` plus the
418+
error string `"too many waiting connect operations"`.
419+
The queued connect operations will be resumed once the number of connections
420+
in the pool is less than `pool_size`.
421+
The queued connect operation will abort once they have been queued for more
422+
than `connect_timeout`, controlled by
423+
[settimeouts](#client:set_timeout), and will return `nil` plus
424+
the error string `"timeout"`.
425+
This option was first introduced in the `v0.10.14` release.
388426
* `ssl_verify`
389427

390428
Specifies whether to perform SSL certificate verification during the

lib/resty/websocket/client.lua

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
-- FIXME: this library is very rough and is currently just for testing
5-
-- the websocket server.
5+
-- the websocket client.
66

77

88
local wbproto = require "resty.websocket.protocol"
@@ -100,7 +100,7 @@ function _M.connect(self, uri, opts)
100100
end
101101

102102
local ssl_verify, server_name, headers, proto_header, origin_header
103-
local sock_opts = false
103+
local sock_opts = {}
104104
local client_cert, client_priv_key
105105

106106
if opts then
@@ -120,10 +120,17 @@ function _M.connect(self, uri, opts)
120120
origin_header = "\r\nOrigin: " .. origin
121121
end
122122

123-
local pool = opts.pool
124-
if pool then
125-
sock_opts = { pool = pool }
123+
if opts.pool then
124+
sock_opts.pool = opts.pool
126125
end
126+
--pool_size specify the size of the connection pool. If omitted and no backlog option was provided, no pool will be created.
127+
if opts.pool_size then
128+
sock_opts.pool_size = opts.pool_size
129+
end
130+
if opts.backlog then
131+
sock_opts.backlog = opts.backlog
132+
end
133+
127134

128135
client_cert = opts.client_cert
129136
client_priv_key = opts.client_priv_key
@@ -149,16 +156,22 @@ function _M.connect(self, uri, opts)
149156
end
150157
end
151158

152-
local ok, err
153-
if sock_opts then
154-
ok, err = sock:connect(host, port, sock_opts)
155-
else
156-
ok, err = sock:connect(host, port)
157-
end
159+
local ok, err = sock:connect(host, port, sock_opts)
158160
if not ok then
159161
return nil, "failed to connect: " .. err
160162
end
161163

164+
-- check for connections from pool:
165+
local reused_count, err = sock:getreusedtimes()
166+
if not reused_count then
167+
return nil, "failed to get reused times: " .. tostring(err)
168+
end
169+
170+
if reused_count > 0 then
171+
-- being a reused connection (must have done handshake)
172+
return 1
173+
end
174+
162175
if scheme == "wss" then
163176
if not ssl_support then
164177
return nil, "ngx_lua 0.9.11+ required for SSL sockets"
@@ -175,17 +188,6 @@ function _M.connect(self, uri, opts)
175188
end
176189
end
177190

178-
-- check for connections from pool:
179-
180-
local count, err = sock:getreusedtimes()
181-
if not count then
182-
return nil, "failed to get reused times: " .. err
183-
end
184-
if count > 0 then
185-
-- being a reused connection (must have done handshake)
186-
return 1
187-
end
188-
189191
local custom_headers
190192
if headers then
191193
custom_headers = concat(headers, "\r\n")

0 commit comments

Comments
 (0)