Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
miketang84 opened this issue May 11, 2013 · 4 comments
Closed

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

miketang84 opened this issue May 11, 2013 · 4 comments

Comments

@miketang84
Copy link

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
Copy link
Owner

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
Copy link
Author

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

@miketang84
Copy link
Author

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

@Neopallium
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants