Search code examples
socketshttpnginxluaopenresty

Lua open socket error


I am using lua as module for nginx (openresty) to get files from remote host. My function:

function readfile(url)
  local http = require ("socket.http")
  if not http then
    error("Couldn't open socket.http")
  end
  http.TIMEOUT = 5
  local body, code = http.request(url)
  if not body then
    error("Couldn't read the remote file: " .. code)
  end
  return body
end

I have tested this code by using Siege. When I set the users more then 100 (for example), I catch this error:

2018/03/27 09:36:38 [info] 10#10: *91018 shutdown() failed (107: Socket not connected), client: 172.18.0.7, server: localhost

I have more errors when i set more users. What does it mean? Thank you for the help.


Solution

  • Don't use luasocket library with OpenResty. The resulting code would block on http.request().

    I suppose that all nginx worker just blocked and it is the reason of these errors.

    For you purpose you may use one of the libraries below:

    First is more fexible, allow to use secure transport. Second has simpler API.

    And both use internally nginx Lua cosocket API and are 100% nonblocking out of the box.