Nginx custom module multi process help

Hi,

I’m new to nginx development and I have to work on a custom nginx
module.

The module is designed to provide a list of variables for the user to
use in
the nginx.conf file. Whenever those variables are used, the module makes
a
udp request to a helper server to get the correct value of that
variable.

Example (log_format directive)

log_format test_log ‘$closest_chinese_restaurant
$closest_ice_cream_shop’;

As it’s set up now (I wrote this a few months ago using Emiller’s guide
to
nginx module development and this module on github
https://github.com/leev/ngx_http_geoip2_module/blob/master/ngx_http_geoip2_module.c),

Whenever one of those variables ($closest_ice_cream_shop) is accessed,
the
module makes a call to the database and gets the value based on the
connections ip address and returns it for use in the conf file. The
problem
is that many of the variables in the module can be retrieved with a
single
query to the helper server. As it stands now, that log directive will
make
two calls to the helper server : one for $closest_chinese_restaurant and
one
for $closest_ice_cream_shop.

How can I make it so that I only have to do one query for multiple
variables? Can I get/set environment variables for use in the conf file
from
inside the module code? I considered making structs to hold the data so
that
any request will check if the ip address is the same as previously
queried -
and if it is, then retrieve the cached value from the structs. I’m just
worried that if the server gets busy enough, there will be multiple
instances of my module in different worker processes, which will lead to
multiple caching arrays/structs and trying to retrieve what might not
exist
for that particular instance.

Right now, I have 3 getters for all of my variables : one for ints, one
for
floats, one for strings. They make a query to the helper server each
time a
variable is accessed in the conf file. Each of those setters is similar
to
ngx_http_geoip2_variable() in the geoip2 module linked above (they get
the
data via a query and set the ngx_http_variable_value_t *v).

Please help. Thanks.

Posted at Nginx Forum:

Why not use Lua, collect the data from your variables, run the query
(and
optionally store them in a Lua cache) and process it all real-time,
none-blocking and without any (extra) module.

Examples:

Posted at Nginx Forum:

If the external server that I’m getting the data from doesn’t have a
relational database (and instead responds to udp requests), will I still
be
able to cache multiple fields from one udp request using Lua so that I
can
access them in the conf file later? Can you clarify what you mean by Lua
cache? Thank you for your help!

Posted at Nginx Forum:

Theoretically yes, it all depends how and by what means this data is
collected and if you need to wait for some parts, have a look at the
example
Urls I send before or join the openresty forum on googlegroups with some
example code/description what your after.

nb. a Lua cache is an in-memory cache of requests/answers that would be
the
same if run again, if so you serve them from cache (which is fast)
instead
of querying the database (which is ‘slow’) again.

Posted at Nginx Forum:

I will definitely join the google group and check out Lua. Thanks for
your
help!

Posted at Nginx Forum: