-
Notifications
You must be signed in to change notification settings - Fork 2k
Introduction
Lua-nginx-module integrated Lua into NginX, and providing high-concurrent & non-blocking request processing ability without forcing developers explicitly dividing business logics into state machine. Developers can write their programs in plain-old sequential ways, and lua-nginx-module will automatically interrupt the program logic at blockable I/O operations, save the context and delegate these I/O ops to NginX’s event mechanism. When I/O ops are done, lua-nginx-module will restore the context and restore the normal running of the program logic. User program itself will feel everything as usual as never being interrupted.
Lua-nginx-module use one-coroutine-per-request request handling model, i.e. for each incoming request, lua-nginx-module spawns a coroutine to run user code to process the request, and the coroutine is destroyed when request handling process is done. Every coroutine has its own independent global environment, which inherits shared read-only common data. So any globals injected by user code won’t affect other request’s processing, and will be freed when request handling is done. You can imagine that user code is running in a ‘sandbox’, which shares the same lifecycle with request itself. By this way, lua-nginx-module can prevent memory-leak caused by the abusing of globals in user code, and improved service robustness.
Benefit from Lua’s excellant coroutine support, lua-nginx-module can handle tens of thousands of concurrent requests with very low memory overhead. According to our tests, the memory overhead for each request in lua-nginx-module is only 2 KB or even half smaller if LuaJIT is used. So obviously lua-nginx-module is a good candidate for implementing extensive concurrent services.