nginx custom module multi process help

de_nginx_noob nginx-forum at forum.nginx.org
Wed Jun 15 21:10:11 UTC 2016


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: https://forum.nginx.org/read.php?2,267611,267611#msg-267611



More information about the nginx mailing list