Permalink
Browse files

refactoring

  • Loading branch information...
1 parent 2de73d7 commit 525685e50d4314d40de2eafaeabcaf2e71bd114e @alexly committed Apr 10, 2012
View
1 config
@@ -1,6 +1,7 @@
ngx_addon_name=ngx_http_r4x_module
HTTP_MODULES="$HTTP_MODULES ngx_http_r4x_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_r4x_redis_adapter.c
+ $ngx_addon_dir/ngx_http_r4x_redis_cluster.c
$ngx_addon_dir/ngx_http_r4x_json_adapter.c
$ngx_addon_dir/ngx_http_r4x_module.c
$ngx_addon_dir/ngx_http_r4x_utils.c
View
@@ -130,8 +130,7 @@ ngx_http_r4x_get_directive_argument_value(ngx_http_request_t *r,
}
*out = "nil";
- *len = 3;
-
+ *len = 3;
break;
case REDIS4NGINX_COMPILIED_ARG:
if (ngx_http_complex_value(r, arg->compilied, &value) != NGX_OK)
View
@@ -30,17 +30,17 @@ ngx_http_r4x_exec_return_callback(redisAsyncContext *c, void *repl, void *privda
ctx = ngx_http_get_module_ctx(r, ngx_http_r4x_module);
+ if(rr == NULL) // TODO: logging connection to redis db is lost
+ ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+
if(!ctx->completed)
{
ctx->completed = 1;
if(ctx->wait_read_body)
r->main->count--;
- if(rr == NULL) // TODO: logging connection to redis db is lost
- ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- else
- ngx_http_r4x_send_redis_reply(r, c, rr);
+ ngx_http_r4x_send_redis_reply(r, c, rr);
}
}
@@ -71,16 +71,17 @@ ngx_http_r4x_prepare_and_run_directive(ngx_http_request_t *r, ngx_http_r4x_direc
if(ngx_http_r4x_run_directive(r, directive, &json_fields_hash[i]) != NGX_OK)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
- if(!directive->require_json_field)
+ if(!directive->require_json_field)
break;
- }
- }
- else {
- if(ngx_http_r4x_run_directive(r, directive, NULL) != NGX_OK)
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ return NGX_OK;
}
}
+ if(ngx_http_r4x_run_directive(r, directive, NULL) != NGX_OK)
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+
return NGX_OK;
}
@@ -28,11 +28,6 @@
#include <stdlib.h>
#include <string.h>
-typedef struct {
- const char *value;
- size_t len;
-} ngx_http_r4x_json_field_t;
-
struct {
ngx_str_t last_key;
ngx_pool_t *pool;
View
@@ -31,9 +31,6 @@
extern ngx_module_t ngx_http_r4x_module;
-struct ngx_http_r4x_directive_t;
-typedef ngx_int_t (ngx_http_r4x_run_directive_t)(ngx_http_request_t *, struct ngx_http_r4x_directive_t *);
-
typedef struct {
ngx_uint_t type;
union {
@@ -42,7 +39,7 @@ typedef struct {
};
} ngx_http_r4x_directive_arg_t;
-typedef struct ngx_http_r4x_directive_t {
+typedef struct {
ngx_array_t arguments_metadata; // metadata for redis arguments
char **raw_redis_argvs;
size_t *raw_redis_argv_lens;
@@ -66,6 +63,15 @@ typedef struct {
unsigned wait_read_body:1;
} ngx_http_r4x_request_ctx;
+typedef struct {
+ redisAsyncContext *async;
+ ngx_connection_t *conn;
+ unsigned connected:1;
+ unsigned master_node:1;
+ char* host;
+ ngx_int_t port;
+} ngx_http_r4x_redis_node_t;
+
// Redis DB API:
ngx_int_t
ngx_http_r4x_init_connection(ngx_http_r4x_srv_conf_t *serv_conf);
@@ -21,17 +21,7 @@
#include "ddebug.h"
#include "ngx_http_r4x_module.h"
-// context struct in the request handling cycle, holding
-// the current states of the command evaluator
-typedef struct ngx_http_r4x_connection_ctx_t {
- redisAsyncContext *async;
- ngx_connection_t *conn;
- unsigned connected:1;
- char* host;
- ngx_int_t port;
-} ngx_http_r4x_connection_ctx_t;
-
-static ngx_http_r4x_connection_ctx_t* connection = NULL;
+static ngx_http_r4x_redis_node_t* sing_node = NULL;
static ngx_int_t ngx_http_r4x_add_event(ngx_connection_t *c, ngx_int_t event);
static ngx_int_t ngx_http_r4x_del_event(ngx_connection_t *c, ngx_int_t event);
@@ -52,52 +42,52 @@ ngx_int_t ngx_http_r4x_init_connection(ngx_http_r4x_srv_conf_t *serv_conf)
ngx_uint_t i;
ngx_str_t *script;
- if(connection == NULL)
+ if(sing_node == NULL)
{
if(serv_conf == NULL)
return NGX_ERROR;
- connection = ngx_palloc(ngx_cycle->pool, sizeof(ngx_http_r4x_connection_ctx_t));
- connection->host = ngx_http_r4x_string_to_c_string(&serv_conf->host, NULL);
- connection->port = serv_conf->port;
- connection->connected = 0;
+ sing_node = ngx_palloc(ngx_cycle->pool, sizeof(ngx_http_r4x_redis_node_t));
+ sing_node->host = ngx_http_r4x_string_to_c_string(&serv_conf->host, NULL);
+ sing_node->port = serv_conf->port;
+ sing_node->connected = 0;
}
- if(connection->connected == 0)
+ if(sing_node->connected == 0)
{
- if(connection->port >=0) {
- connection->async = redisAsyncConnect(
- (const char*)connection->host,
- connection->port);
+ if(sing_node->port >=0) {
+ sing_node->async = redisAsyncConnect(
+ (const char*)sing_node->host,
+ sing_node->port);
}
else {
- connection->async = redisAsyncConnectUnix((const char*)connection->host);
+ sing_node->async = redisAsyncConnectUnix((const char*)sing_node->host);
}
- if(connection->async->err) {
+ if(sing_node->async->err) {
return NGX_ERROR;
}
- redisAsyncSetConnectCallback(connection->async, ngx_http_r4x_connecte_event_handler);
- redisAsyncSetDisconnectCallback(connection->async, ngx_http_r4x_disconnecte_event_handler);
+ redisAsyncSetConnectCallback(sing_node->async, ngx_http_r4x_connecte_event_handler);
+ redisAsyncSetDisconnectCallback(sing_node->async, ngx_http_r4x_disconnecte_event_handler);
- if(ngx_http_r4x_add_connection(connection->async->c.fd, &connection->conn) != NGX_OK) {
+ if(ngx_http_r4x_add_connection(sing_node->async->c.fd, &sing_node->conn) != NGX_OK) {
return NGX_ERROR;
}
- connection->conn->read->handler = ngx_http_r4x_read_event_handler;
- connection->conn->write->handler = ngx_http_r4x_write_event_handler;
+ sing_node->conn->read->handler = ngx_http_r4x_read_event_handler;
+ sing_node->conn->write->handler = ngx_http_r4x_write_event_handler;
- connection->async->data = connection;
- connection->conn->data = connection;
+ sing_node->async->data = sing_node;
+ sing_node->conn->data = sing_node;
// Register functions to start/stop listening for events
- connection->async->ev.addRead = ngx_http_r4x_add_read;
- connection->async->ev.delRead = ngx_http_r4x_del_read;
- connection->async->ev.addWrite = ngx_http_r4x_add_write;
- connection->async->ev.delWrite = ngx_http_r4x_del_write;
- connection->async->ev.data = connection;
- connection->async->ev.cleanup = ngx_http_r4x_cleanup;
+ sing_node->async->ev.addRead = ngx_http_r4x_add_read;
+ sing_node->async->ev.delRead = ngx_http_r4x_del_read;
+ sing_node->async->ev.addWrite = ngx_http_r4x_add_write;
+ sing_node->async->ev.delWrite = ngx_http_r4x_del_write;
+ sing_node->async->ev.data = sing_node;
+ sing_node->async->ev.cleanup = ngx_http_r4x_cleanup;
if(serv_conf == NULL)
return NGX_ERROR;
@@ -124,7 +114,7 @@ ngx_int_t ngx_http_r4x_async_command(redisCallbackFn *fn, void *privdata, const
int status;
va_start(ap,format);
- status = redisvAsyncCommand(connection->async,fn, privdata, format, ap);
+ status = redisvAsyncCommand(sing_node->async,fn, privdata, format, ap);
va_end(ap);
return status == REDIS_OK ? NGX_OK : NGX_ERROR;
@@ -133,59 +123,59 @@ ngx_int_t ngx_http_r4x_async_command(redisCallbackFn *fn, void *privdata, const
ngx_int_t ngx_http_r4x_async_command_argv(redisCallbackFn *fn, void *privdata, int argc, char **argv, const size_t *argvlen)
{
int status;
- status = redisAsyncCommandArgv(connection->async, fn, privdata, argc, (const char**)argv, argvlen);
+ status = redisAsyncCommandArgv(sing_node->async, fn, privdata, argc, (const char**)argv, argvlen);
return status == REDIS_OK ? NGX_OK : NGX_ERROR;
}
static void ngx_http_r4x_connecte_event_handler(const redisAsyncContext *ctx)
{
- ngx_http_r4x_connection_ctx_t *context = ctx->data;
- context->connected = 1;
+ ngx_http_r4x_redis_node_t *node = ctx->data;
+ node->connected = 1;
}
static void ngx_http_r4x_disconnecte_event_handler(const redisAsyncContext *ctx, int status)
{
- ngx_http_r4x_connection_ctx_t *context = ctx->data;
- context->connected = 0;
+ ngx_http_r4x_redis_node_t *node = ctx->data;
+ node->connected = 0;
}
static void ngx_http_r4x_read_event_handler(ngx_event_t *handle)
{
ngx_connection_t *conn = handle->data;
- ngx_http_r4x_connection_ctx_t *context = conn->data;
- redisAsyncHandleRead(context->async);
+ ngx_http_r4x_redis_node_t *node = conn->data;
+ redisAsyncHandleRead(node->async);
}
static void ngx_http_r4x_write_event_handler(ngx_event_t *handle)
{
ngx_connection_t *conn = handle->data;
- ngx_http_r4x_connection_ctx_t *context = conn->data;
- redisAsyncHandleWrite(context->async);
+ ngx_http_r4x_redis_node_t *node = conn->data;
+ redisAsyncHandleWrite(node->async);
}
static void ngx_http_r4x_add_read(void *privdata)
{
- ngx_http_r4x_connection_ctx_t *conn = (ngx_http_r4x_connection_ctx_t*)privdata;
- ngx_http_r4x_add_event(conn->conn, NGX_READ_EVENT);
+ ngx_http_r4x_redis_node_t *node = privdata;
+ ngx_http_r4x_add_event(node->conn, NGX_READ_EVENT);
}
static void ngx_http_r4x_del_read(void *privdata)
{
- ngx_http_r4x_connection_ctx_t *conn = (ngx_http_r4x_connection_ctx_t*)privdata;
- ngx_http_r4x_del_event(conn->conn, NGX_READ_EVENT);
+ ngx_http_r4x_redis_node_t *node = privdata;
+ ngx_http_r4x_del_event(node->conn, NGX_READ_EVENT);
}
static void ngx_http_r4x_add_write(void *privdata)
{
- ngx_http_r4x_connection_ctx_t *conn = (ngx_http_r4x_connection_ctx_t*)privdata;
- ngx_http_r4x_add_event(conn->conn, NGX_WRITE_EVENT);
+ ngx_http_r4x_redis_node_t *node = privdata;
+ ngx_http_r4x_add_event(node->conn, NGX_WRITE_EVENT);
}
static void ngx_http_r4x_del_write(void *privdata)
{
- ngx_http_r4x_connection_ctx_t *conn = (ngx_http_r4x_connection_ctx_t*)privdata;
- ngx_http_r4x_del_event(conn->conn, NGX_WRITE_EVENT);
+ ngx_http_r4x_redis_node_t *node = privdata;
+ ngx_http_r4x_del_event(node->conn, NGX_WRITE_EVENT);
}
static void ngx_http_r4x_cleanup(void *privdata)
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2011-2012, Alexander Lyalin <alexandr.lyalin@gmail.com>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef DDEBUG
+#define DDEBUG 0
+#endif
+
+#include "ddebug.h"
+#include "ngx_http_r4x_module.h"
View
@@ -22,8 +22,8 @@
#include "ngx_http_r4x_module.h"
#include "sha1.h"
-static u_char null_value[] = "NULL";
-static u_char array_value[] = "REDIS_REPLY_ARRAY";
+static u_char null_value[] = "null";
+static u_char array_value[] = "{\"err\": \"redis arrays do not send by http\"}";
static
ngx_int_t ngx_http_r4x_prepare_reply(ngx_http_request_t *r, redisReply *reply, ngx_buf_t *buf)

0 comments on commit 525685e

Please sign in to comment.