ostinelli/gin — pull request #16 by ppolv (2014-10-30 00:02:42+00:00)
Title: Fix race condition when using the code cache

require()s are evaluated only once.
Each time we require a user-generated controller module,
in production mode (with code cache enabled) it returns the *same*
table.
http://wiki.nginx.org/HttpLuaModule#Data_Sharing_within_an_Nginx_Worker

If we use setmetatable() to "inject" the request data to use in the controller,
and the coroutine yields (blocked in IO for example), and another
request come in for that controller in the same worker process, then it
will *override* the request data of the previous one.

Simple scenario:

function MyController:MyMethod()
local myUriParam = self.params[1]

sleep() / block on IO

local body = self.request.body
-- Problem! it could be the body of another request!
-- myUriParam can be different than self.params[1] now
)

The solution was to chain the metatables in the other direction:
set the user controller as the metatable of the request instance.
The user controller itself has a metatable the Gin' controller
(for now I'm setting it every time, but pretty sure that could be done
only once at setup time when generating the routes)