lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all,

inspired by the other thread with LuaJIT/ffi for numerical
calculations, I've experimented with it on sockets, with rather
positive results - the microbenchmarks on a degenerate web server are
better than those on lighttpd (obviously there is quite a different
amount of processing in lighttpd on the received data, so it is
probably slower in the end, but still quite fast).

Also a small trick of "setmetatable(_G, { __index = ffi.C } )" in the
beginning of the program allows for a nice to read code - avoiding the
need to use ffi.C. for native C function calls - so the resulting code
is less bumpy (and this appears to give also errors on undeclared
variables reads as a bonus)

Some longer ramblings here:
http://bnpcs.blogspot.com/2011/02/fun-with-luajit-and-ffi-library-httpd.html
if you want to toy with the test code, it's here on github:
https://github.com/ayourtch/luajit-fun

The "socket_set" abstraction that I came up with allows to express the
"useful" part of the application as this:

local HTTP_REPLY = [[HTTP/1.0 200 OK
Content-Type: text/plain

This is test
]]

local ss = socket_set(MAX_FD)

local my_accept_cb = function(fds, i)
  local cb = {}
  cb.read = function(fds, i, data, len)
    fds.send(i, HTTP_REPLY, #HTTP_REPLY)
    fds.close(i)
  end
  cb.close = function(fds, i)
    -- print("Closed socket")
  end
  return cb
end

while not ss.add_listener(12345, my_accept_cb) do
  sleep(1)
end

print("Added listener, please run the test")

while true do
  local n = ss.poll(1000)
end

---

I thought of naming it "node.lua" but figured it's a bit too early ;-)

cheers,
andrew