[Groonga-commit] groonga/groonga [master] Create grn_ctx only once

Back to archive index

null+****@clear***** null+****@clear*****
2012年 6月 4日 (月) 18:14:57 JST


Ryo Onodera	2012-06-04 18:14:57 +0900 (Mon, 04 Jun 2012)

  New Revision: 57629fa7fa55d6110f35e4aeb865fd5bce379ca9

  Log:
    Create grn_ctx only once

  Modified files:
    src/nginx-module/ngx_http_groonga_module.c

  Modified: src/nginx-module/ngx_http_groonga_module.c (+19 -15)
===================================================================
--- src/nginx-module/ngx_http_groonga_module.c    2012-06-04 18:00:32 +0900 (6f300f5)
+++ src/nginx-module/ngx_http_groonga_module.c    2012-06-04 18:14:57 +0900 (9479e9b)
@@ -25,6 +25,7 @@
 typedef struct {
   ngx_str_t database;
   char *database_cstr;
+  grn_ctx *global_context;
 } ngx_http_groonga_loc_conf_t;
 
 static char *ngx_http_groonga(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
@@ -112,22 +113,30 @@ ngx_http_groonga_handler(ngx_http_request_t *r)
 
   ngx_http_groonga_loc_conf_t *loc_conf;
   loc_conf = ngx_http_get_module_loc_conf(r, ngx_http_groonga_module);
-  grn_ctx_init(context, no_flags);
 
-  if (!loc_conf->database_cstr) {
-    loc_conf->database_cstr = ngx_str_null_terminate(&loc_conf->database);
+  if (!loc_conf->global_context) {
+    context = malloc(sizeof(grn_ctx));
+
+    grn_ctx_init(context, no_flags);
+
+    if (!loc_conf->database_cstr) {
+      loc_conf->database_cstr = ngx_str_null_terminate(&loc_conf->database);
+    }
+
+    grn_db_open(context, loc_conf->database_cstr);
+    rc = ngx_http_groonga_context_check(context);
+    if (rc != NGX_OK) {
+      return rc;
+    }
+    loc_conf->global_context = context;
   }
 
+  context = loc_conf->global_context;
+
   printf("database_path: %s\n", loc_conf->database_cstr);
   printf("version: %s\n", grn_get_version());
   printf("uri: %.*s\n", (int)r->unparsed_uri.len, r->unparsed_uri.data);
 
-  grn_db_open(context, loc_conf->database_cstr);
-  rc = ngx_http_groonga_context_check(context);
-  if (rc != NGX_OK) {
-    return rc;
-  }
-
   grn_ctx_send(context, (char *)r->unparsed_uri.data, r->unparsed_uri.len, no_flags);
   rc = ngx_http_groonga_context_check(context);
   if (rc != NGX_OK) {
@@ -146,12 +155,6 @@ ngx_http_groonga_handler(ngx_http_request_t *r)
   }
   ngx_memcpy(body_data, result, result_size);
 
-  grn_ctx_fin(context);
-  rc = ngx_http_groonga_context_check(context);
-  if (rc != NGX_OK) {
-    return rc;
-  }
-
   /* we response to 'GET' and 'HEAD' requests only */
   if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
     return NGX_HTTP_NOT_ALLOWED;
@@ -224,6 +227,7 @@ ngx_http_groonga_create_loc_conf(ngx_conf_t *cf)
   conf->database.data = NULL;
   conf->database.len = 0;
   conf->database_cstr = NULL;
+  conf->global_context = NULL;
 
   return conf;
 }




Groonga-commit メーリングリストの案内
Back to archive index