lua-users home
lua-l archive

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


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.

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.