How to change buffer size for lua socket?

72 views Asked by At

I have the following lua function that creates, connects, and sends information on a udp socket.

local udp = ngx.socket.udp
local function write_to_socket(conf, bytes)
  local sock = udp()
  sock:settimeout(conf.timeout)
  sock:setpeername(conf.socket_host, conf.socket_port)
  sock:send(bytes)
  sock:close()
end

(I've omitted error handling)

I would like to increase the writing buffer size with an option similar to so_sndbuf, but as far as I can tell both ngx socket and lua socket don't offer this ability. Is there a way to change writing buffer size in lua?

I did look into writing C functions to do the socket logic but this is a small part of an application written in lua so I can't change my entry point to run C with lua_register.

1

There are 1 answers

2
shingo On

For luasocket, since version 3.0.0, you can change the size with setoption.

local sock = socket.udp()
sock:setoption('send-buffer-size', bytes)

By default sending is not buffered, but you can actually achieve buffering by concatenating strings in Lua on your own.