Neopallium / nixio

System, Networking and I/O library for Lua. This is an unoffical fork of NIXO from: http://luci.subsignal.org/
http://neopallium.github.io/nixio/
Apache License 2.0
58 stars 23 forks source link

hi, can nixio socket object been passed to another thread? #7

Closed miketang84 closed 1 month ago

miketang84 commented 11 years ago

hi neo,

I am using lua-handlers to write a http server. Now I want to process static files in other threads, so after parse the http request, I need to pass the file uri and nixio socket object to other threads (I use lua-llthreads), other threads use nixion.sendfile to server files, so they must get the socket object to response correctly. How should I do to pass socket object?

Expect your reply. Thank you.

Neopallium commented 11 years ago

Simple answer: There is no support for this.

Longer answer: Nixio doesn't have support for creating a Socket from an FD number. Even if it did both threads would have an object that would try to close the FD when the object is GCed by Lua. So you would have to have a way to reference count the object or delegate which thread owns the FD and only have that thread close it on GC.

If you still want to try using sendfile from a thread then you could create a very simple C module that only wraps the sendfile() function. That function would take the in/out FD as numbers instead of objects. This would be a quick way to test if this design will work for you.

There may also be some issues with lua-handlers when trying to write to the socket from another thread. Write timeout, flushing the internal connection write buffer.

I think the best way to scale would be to use nodejs's cluster design where the server forks multiple child worker processes that have shared access to the listening sockets.

I am working on a new design for lua-handlers that supports different backends for IO/events: https://github.com/Neopallium/lua-handlers/tree/poller

I am trying to replace nixio with a new module lua-llnet: https://github.com/Neopallium/lua-llnet

lua-llnet has much better performance under LuaJIT since it uses the FFI interface. I am planning on adding support for sending an FD over a Unix Domain socket (this is needed to support nodejs's clustering design), this feature could be used for passing a socket to another thread.

There are still a few missing features (timers/signal fd support with the epoll backend, and TLS sockets) before I release the new lua-handlers design.

miketang84 commented 11 years ago

ok, thank you, I'll follow your progress. :)

miketang84 commented 11 years ago

can wrap this nixio socket object to a light userdata, and pass to another thread, like your lua-zmq-threads?

Neopallium commented 11 years ago

no. The zeromq context is designed to be shared between threads. The nixio socket objects are just wrappers for the socket FD. You will run into problems with the GC like I outlined in my previous comment.