The LuaSocket library (which is used in the example code) does not support persistent connections over http (s). Therefore, the minimum pain can not do, and we must use some other library.
For example, you can take Lua-cURL :
local curl = require "cURL" local function main() local e = curl.easy() -- включает подробный вывод о ходе соединения e:setopt_verbose(true) e:setopt_url("https://httpbin.org/get") e:perform() -- выполняет загрузку и НЕ закрывает соединение -- отключает переиспользование соединений e:setopt_forbid_reuse(true) e:setopt_url("https://httpbin.org/ip") e:perform() -- выполняет загрузку и закрывает соединение e:close() end main()
In this library, on the contrary, connections by default are not closed and reused (even after calling e:close ) and here you must use the special option CURLOPT_FORBID_REUSE so that connections are broken (if necessary).
The whole process of opening / re-using / closing connections can be seen in the log:
* Trying 23.22.14.18... * Connected to httpbin.org (23.22.14.18) port 443 (#0) * ALPN, offering http/1.1 * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH * successfully set certificate verify locations: * CAfile: /etc/ssl/certs/ca-certificates.crt CApath: /etc/ssl/certs * NPN, negotiated HTTP1.1 * SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384 * ALPN, server did not agree to a protocol * Server certificate: * subject: OU=Domain Control Validated; OU=EssentialSSL Wildcard; CN=*.httpbin.org * start date: Jan 12 00:00:00 2016 GMT * expire date: Jan 19 23:59:59 2017 GMT * subjectAltName: httpbin.org matched * issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server CA * SSL certificate verify ok. > GET /get HTTP/1.1 Host: httpbin.org Accept: */* { "args": {}, "headers": { "Accept": "*/*", "Host": "httpbin.org" }, "origin": "xx.xx.xx.xx", "url": "https://httpbin.org/get" } < HTTP/1.1 200 OK < Server: nginx < Date: Tue, 04 Oct 2016 19:46:13 GMT < Content-Type: application/json < Content-Length: 154 < Connection: keep-alive < Access-Control-Allow-Origin: * < Access-Control-Allow-Credentials: true < * Connection #0 to host httpbin.org left intact * Found bundle for host httpbin.org: 0x95eac28 [can pipeline] * Re-using existing connection! (#0) with host httpbin.org * Connected to httpbin.org (23.22.14.18) port 443 (#0) > GET /ip HTTP/1.1 Host: httpbin.org Accept: */* { "origin": "xx.xx.xx.xx" } < HTTP/1.1 200 OK < Server: nginx < Date: Tue, 04 Oct 2016 19:46:13 GMT < Content-Type: application/json < Content-Length: 33 < Connection: keep-alive < Access-Control-Allow-Origin: * < Access-Control-Allow-Credentials: true < * Closing connection 0
After the first query is executed, curl reports that the connection has been left open:
Connection #0 to host httpbin.org left intact
Before executing the second query, it finds it and happily re-uses:
Found bundle for host httpbin.org: 0x95eac28 [can pipeline] Re-using existing connection! (#0) with host httpbin.org
But after the second request, due to the fact that in the example before the second request, the ban on reusing connections is indicated, it closes:
Closing connection 0
LuaSocketdoes not support persistent connections for http (s), so you need to either write on the sockets yourself, or look for another library. - zedLua-cURLshould be a good alternative: github.com/Lua-cURL/Lua-cURLv3 - zed