Hello!
On Fri, Oct 2, 2015 at 9:26 PM, Aapo Talvensaari wrote:
> I should know better, but it would help me a bit if someone could clarify
> the differences between these:
>
> - ok, err = ngx.timer.at(0, ...)
This creates a new light thread that is detached from the current
(creating) context (like the nginx request handler context or
init_worker_by_lua handler's). This is similar to creating a daemon
process that is detached from the controlling terminal in the OS
context.
> - co = ngx.thread.spawn(...)
This creates a new light thread which still attaches to the current
(request and etc) context.
> - co = coroutine.create(...)
>
This creates a the good old Lua coroutine and gives exactly the same
semantics (though with a different implementation), which is not a Lua
thread, so you schedule its running and yielding completely by
yourself. While light threads are scheduled automatically by ngx_lua's
I/O scheduler.
> Aka use cases / downsides / upsides of each of these.
>
They are for different use cases.
> One use case would be building an web chat or something similar that uses
> permanent web socket
> connection. Aka you read from websocket, and you write to the websocket. The
> writing usually gets
> the data from other socket (e.g. redis subscription). In that case my gut
> feeling says, ngx.thread.spawn
> is the way to go (I can confirm it works). But could this be made to work
> with coroutine.create (probably not?)
I don't think you want to use the standard coroutine API for this. As
mentioned above, plain Lua coroutines have no knowledge about I/O
scheduling and etc.
> or ngx.timer.at (maybe?) as well?
If websockets are directly involved, I don't think you want to detach
the work thread from the request context. When being detached, the
work thread can continue running regardless of the state (or even
existence) of the request creating it.
Regards,
-agentzh