Geoip cache options patch

Akins, Brian Brian.Akins at turner.com
Mon Dec 20 21:36:47 MSK 2010


Corrected patch below...


On 12/20/10 1:29 PM, "Brian Akins" <Brian.Akins at turner.com> wrote:

> The attached patch allows you to change the geoip cache option in the
> config.  For example:
> 
> geoip_country /usr/share/GeoIP/GeoIP.dat mmap;
> 
> Will use GEOIP_MMAP_CACHE.  The default is memory ( GEOIP_MEMORY_CACHE ),
> the same as today.  The mmap option can make a large difference in memory
> usage when using the large city data files.
> 
> 
> --
> Brian Akins
> 
--- nginx-0.8.54/src/http/modules/ngx_http_geoip_module.c       2010-08-03
14:38:08.000000000 -0400
+++ nginx-0.8.54.geoip/src/http/modules/ngx_http_geoip_module.c 2010-12-20
13:17:10.000000000 -0500
@@ -50,14 +50,14 @@
 static ngx_command_t  ngx_http_geoip_commands[] = {
 
     { ngx_string("geoip_country"),
-      NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
+      NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
       ngx_http_geoip_country,
       NGX_HTTP_MAIN_CONF_OFFSET,
       0,
       NULL },
 
     { ngx_string("geoip_city"),
-      NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
+      NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE12,
       ngx_http_geoip_city,
       NGX_HTTP_MAIN_CONF_OFFSET,
       0,
@@ -423,11 +423,29 @@
     return conf;
 }
 
+static ngx_int_t ngx_http_geoip_cache_type(ngx_str_t  *value)
+{
+    ngx_int_t cache = GEOIP_MEMORY_CACHE;
+
+    if(!ngx_strncasecmp(value->data, (u_char *)"MEMORY", value->len)) {
+        cache = GEOIP_MEMORY_CACHE;
+    } else if(!ngx_strncasecmp(value->data, (u_char *)"CHECK", value->len))
{
+        cache = GEOIP_CHECK_CACHE;
+    } else if(!ngx_strncasecmp(value->data, (u_char *)"INDEX", value->len))
{
+        cache = GEOIP_INDEX_CACHE;
+    } else if(!ngx_strncasecmp(value->data, (u_char *)"MMAP", value->len))
{
+        cache = GEOIP_MMAP_CACHE;
+    } else {
+        return NGX_CONF_ERROR;
+    }
+    return cache;
+}
 
 static char *
 ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_geoip_conf_t  *gcf = conf;
+    ngx_int_t cache = GEOIP_MEMORY_CACHE;
 
     ngx_str_t  *value;
 
@@ -437,7 +455,15 @@
 
     value = cf->args->elts;
 
-    gcf->country = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
+    if(cf->args->nelts == 3) {
+        if((cache = ngx_http_geoip_cache_type(&value[2])) ==
NGX_CONF_ERROR) {
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                               "Unknown GeoIP cache type: \"%V\"",
&value[2]);
+            return NGX_CONF_ERROR;
+        }
+    }
+
+    gcf->country = GeoIP_open((char *) value[1].data, cache);
 
     if (gcf->country == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
@@ -467,6 +493,7 @@
 ngx_http_geoip_city(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_geoip_conf_t  *gcf = conf;
+    ngx_int_t cache = GEOIP_MEMORY_CACHE;
 
     ngx_str_t  *value;
 
@@ -476,7 +503,15 @@
 
     value = cf->args->elts;
 
-    gcf->city = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);
+    if(cf->args->nelts == 3) {
+        if((cache = ngx_http_geoip_cache_type(&value[2])) ==
NGX_CONF_ERROR) {
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                               "Unknown GeoIP cache type: \"%V\"",
&value[2]);
+            return NGX_CONF_ERROR;
+        }
+    }
+
+    gcf->city = GeoIP_open((char *) value[1].data, cache);
 
     if (gcf->city == NULL) {
         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,




More information about the nginx-devel mailing list