lua-users home
lua-l archive

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



On Mar 26, 2008, at 2:39 AM, Graham Wakefield wrote:

2. I need a method of keeping some kind of "state" or "session" on each connection. This "session" is opaque from c++ point of view. Must be opaque because the user of the server must write his own Lua scripts and keep his own state particular for each application. For example, I have app1 and app2. App1 must keep an username and a passowrd in his "session" and app2 must keep a counter of some kind.

How about a coroutine per session: lua_newthread() & lua_resume()? Session storage would come for free in the form of any local variables.
One little issue... each lua_newthread() pushes a lua_State on the stack of the parent context. Right? And they need to stay there, otherwise will be garbage collected. On the other hand, stack space is precious and limited. And I will surly have hundreds of connections... So, I need to pop the lua_State from the stack and keep it somewhere and make Lua interpreter understand that is not allowed to garbage collect it.

Am I right? Any idea how can I keep this lua_State in the parent interpreter?

Thank you very much for your help!



Can this generate memory issues on a large number of connections due to large number of interpreter spawn?

lua_States can be recycled, providing they exit without error, though it might be safer to create a new one for each session anyway.

Another method is to spawn an interpreter for each application and find a way to distinguish between connection 1's session and connection 2's session. Anyway, applications are few in number and i can afford to open a Lua interpreter
for each app.

You could run each app in a different OS thread?

If each per-session lua_State is created with lua_newthread(), you can also store state global to the entire application by sharing a data structure from the parent lua_State. Also, giving each app a isolated parent lua_State stops one app from stomping on another.