Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:agentzh/redis2-nginx-module

  • Loading branch information...
commit a4416093c575a6c7d1e47a221fa550ad8a60163b 2 parents ec6193d + 0a8a6b9
@agentzh agentzh authored
View
2  .gitignore
@@ -58,4 +58,4 @@ src/reply.rl
all
go
buildroot/
-build10
+build1[01]
View
645 README
@@ -1,258 +1,286 @@
-NAME
-
+Name
ngx_redis2 - Nginx upstream module for the Redis 2.0 protocol
*This module is not distributed with the Nginx source.* See the
- installation instructions.
-
-STATUS
- This module is still under development and is considered experimental.
-
-SYNOPSIS
-
- location /foo {
- set $value 'first';
- redis2_query set one $value;
- redis2_pass 127.0.0.1:6379;
- }
-
- # GET /get?key=some_key
- location /get {
- set_unescape_uri $key $arg_key; # this requires ngx_set_misc
- redis2_query get $key;
- redis2_pass foo.com:6379;
- }
-
- # GET /set?key=one&val=first%20value
- location /set {
- set_unescape_uri $key $arg_key; # this requires ngx_set_misc
- set_unescape_uri $val $arg_val; # this requires ngx_set_misc
- redis2_query set $key $val;
- redis2_pass foo.com:6379;
- }
-
- # multiple pipelined queries
- location /foo {
- set $value 'first';
- redis2_query set one $value;
- redis2_query get one;
- redis2_query set one two;
- redis2_query get one;
- redis2_pass 127.0.0.1:6379;
- }
-
- location /bar {
- # $ is not special here...
- redis2_literal_raw_query '*1\r\n$4\r\nping\r\n';
- redis2_pass 127.0.0.1:6379;
- }
-
- location /bar {
- # variables can be used below and $ is special
- redis2_raw_query 'get one\r\n';
- redis2_pass 127.0.0.1:6379;
- }
-
- # GET /baz?get%20foo%0d%0a
- location /baz {
- set_unescape_uri $query $query_string; # this requires the ngx_set_misc module
- redis2_raw_query $query;
- redis2_pass 127.0.0.1:6379;
- }
+ installation instructions.
-Description
- This is an nginx upstream module that makes nginx talk to a redis 2.x
- server in a non-blocking way. The full Redis 2.0 unified protocol has
- been implemented including the redis pipelining support.
+Status
+ This module is already production ready.
- This module returns the raw TCP response from the redis server.
- It's recommended to use my lua-redis-parser module (written in
- pure C) to parse these responses into lua data structure when combined
- with the ngx_lua module:
+Version
+ This document describes ngx_redis2 v0.08rc2
+ (<https://github.com/agentzh/redis2-nginx-module/tags>) released on 24
+ November 2011.
- https://github.com/agentzh/lua-redis-parser
+Synopsis
+ location /foo {
+ set $value 'first';
+ redis2_query set one $value;
+ redis2_pass 127.0.0.1:6379;
+ }
- If you only want to use the "get" redis command, you can try out the
- ngx_redis module here:
+ # GET /get?key=some_key
+ location /get {
+ set_unescape_uri $key $arg_key; # this requires ngx_set_misc
+ redis2_query get $key;
+ redis2_pass foo.com:6379;
+ }
+
+ # GET /set?key=one&val=first%20value
+ location /set {
+ set_unescape_uri $key $arg_key; # this requires ngx_set_misc
+ set_unescape_uri $val $arg_val; # this requires ngx_set_misc
+ redis2_query set $key $val;
+ redis2_pass foo.com:6379;
+ }
+
+ # multiple pipelined queries
+ location /foo {
+ set $value 'first';
+ redis2_query set one $value;
+ redis2_query get one;
+ redis2_query set one two;
+ redis2_query get one;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location /bar {
+ # $ is not special here...
+ redis2_literal_raw_query '*1\r\n$4\r\nping\r\n';
+ redis2_pass 127.0.0.1:6379;
+ }
- http://wiki.nginx.org/HttpRedis
+ location /bar {
+ # variables can be used below and $ is special
+ redis2_raw_query 'get one\r\n';
+ redis2_pass 127.0.0.1:6379;
+ }
- It returns the parsed content part of the redis response because only
- "get" is needed to implement.
+ # GET /baz?get%20foo%0d%0a
+ location /baz {
+ set_unescape_uri $query $query_string; # this requires the ngx_set_misc module
+ redis2_raw_query $query;
+ redis2_pass 127.0.0.1:6379;
+ }
- Another option is to parse the redis responses on your client side yourself.
+Description
+ This is an Nginx upstream module that makes nginx talk to a Redis
+ (<http://redis.io/>) 2.x server in a non-blocking way. The full Redis
+ 2.0 unified protocol has been implemented including the Redis pipelining
+ support.
+
+ This module returns the raw TCP response from the Redis server. It's
+ recommended to use my [[LuaRedisParser]] (written in pure C) to parse
+ these responses into lua data structure when combined with
+ [[HttpLuaModule]].
+
+ If you only want to use the "get" redis command, you can try out the
+ [[HttpRedisModule]]. It returns the parsed content part of the Redis
+ response because only "get" is needed to implement.
+
+ Another option is to parse the redis responses on your client side
+ yourself.
Directives
- redis2_query arg1 arg2 ...
- Specify a redis command by specifying its individual arguments
- (including the redis command name itself) in a
- similar way to the redis-cli utility.
+ redis2_query
+ syntax: *redis2_query cmd arg1 arg2 ...*
- Multiple instances of this directive are allowd in
- a single location and these queries will be pipelined.
+ default: *no*
- For example,
+ context: *location, location if*
- location /pipelined {
- redis2_query set hello world;
- redis2_query get hello;
- redis2_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT;
- }
+ Specify a Redis command by specifying its individual arguments
+ (including the Redis command name itself) in a similar way to the
+ "redis-cli" utility.
- then GET /pipelined will yield two successive raw redis responses
+ Multiple instances of this directive are allowed in a single location
+ and these queries will be pipelined. For example,
- +OK
- $5
- world
+ location /pipelined {
+ redis2_query set hello world;
+ redis2_query get hello;
- while newlines here are actually CR LF ("\r\n").
+ redis2_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT;
+ }
- redis2_raw_query CMD
- Specify raw redis queries and nginx variables are recognized
- in the CMD argument.
+ then "GET /pipelined" will yield two successive raw Redis responses
- Only one redis command is allowed in the CMD argument, or
- you'll receive an error. If you want to specify multiple
- pipelined commands in a single query, use the redis2_raw_queries
- directive instead.
+ +OK
+ $5
+ world
- redis2_raw_queries N CMDS
- Specify N commands in the CMDS argument. Both the N and CMDS
- arguments can take nginx variables.
+ while newlines here are actually "CR LF" ("\r\n").
- Here's some examples
+ redis2_raw_query
+ syntax: *redis2_raw_query QUERY*
- location /pipelined {
- redis2_raw_queries 3 "flushall\r\nget key1\r\nget key2\r\n";
- redis2_pass 127.0.0.1:6379;
- }
+ default: *no*
- # GET /pipelined2?n=2&cmds=flushall%0D%0Aget%20key%0D%0A
- location /pipelined2 {
- set_unescape_uri $n $arg_n;
- set_unescape_uri $cmds $arg_cmds;
+ context: *location, location if*
- redis2_raw_queries $n $cmds;
+ Specify raw Redis queries and nginx variables are recognized in the
+ "QUERY" argument.
- redis2_pass 127.0.0.1:6379;
- }
+ Only *one* Redis command is allowed in the "QUERY" argument, or you'll
+ receive an error. If you want to specify multiple pipelined commands in
+ a single query, use the redis2_raw_queries directive instead.
- Note that in the second sample above, the set_unescape_uri directive
- is provided by the ngx_set_misc module.
+ redis2_raw_queries
+ syntax: *redis2_raw_queries N QUERIES*
- redis2_literal_raw_query CMD
- Specify raw redis queries but nginx variables are *not* recognized,
- that is to say, you're free to use the dollar sign characters ($)
- in your CMD argument.
+ default: *no*
- Only one redis command is allowed in the CMD argument.
+ context: *location, location if*
- redis2_pass
+ Specify "N" commands in the "QUERIES" argument. Both the "N" and
+ "QUERIES" arguments can take Nginx variables.
- redis2_connect_timeout
- syntax: *redis2_connect_timeout <time>*
+ Here's some examples location /pipelined { redis2_raw_queries 3
+ "flushall\r\nget key1\r\nget key2\r\n"; redis2_pass 127.0.0.1:6379; }
- default: *60s*
+ # GET /pipelined2?n=2&cmds=flushall%0D%0Aget%20key%0D%0A
+ location /pipelined2 {
+ set_unescape_uri $n $arg_n;
+ set_unescape_uri $cmds $arg_cmds;
- context: *http, server, location*
+ redis2_raw_queries $n $cmds;
- The timeout for connecting to the redis server, in seconds by
- default.
+ redis2_pass 127.0.0.1:6379;
+ }
+ Note that in the second sample above, the L<set_unescape_uri|HttpSetMiscModule/"set_unescape_uri"> directive is provided by the [[HttpSetMiscModule]].
- It's wise to always explicitly specify the time unit to avoid confusion.
- Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years),
- "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes).
+ redis2_literal_raw_query
+ syntax: *redis2_literal_raw_query QUERY*
- This time must be less than 597 hours.
+ default: *no*
- redis2_send_timeout
- syntax: *redis2_send_timeout <time>*
+ context: *location, location if*
- default: *60s*
+ Specify a raw Redis query but Nginx variables in it will not be *not*
+ recognized. In other words, you're free to use the dollar sign character
+ ("$") in your "QUERY" argument.
- context: *http, server, location*
+ Only One redis command is allowed in the "QUERY" argument.
- The timeout for sending TCP requests to the redis server, in seconds
- by default.
+ redis2_pass
+ syntax: *redis2_pass <upstream_name>*
- It's wise to always explicitly specify the time unit to avoid confusion.
- Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years),
- "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes).
+ syntax: *redis2_pass <host>:<port>*
- This time must be less than 597 hours.
+ default: *no*
- redis2_read_timeout
- syntax: *redis2_read_timeout <time>*
+ context: *location, location if*
- default: *60s*
+ phase: *content*
- context: *http, server, location*
+ Specify the Redis server backend.
- The timeout for reading TCP responses from the redis server, in
- seconds by default.
+ redis2_connect_timeout
+ syntax: *redis2_connect_timeout <time>*
- It's wise to always explicitly specify the time unit to avoid confusion.
- Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years),
- "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes).
+ default: *60s*
- This time must be less than 597 hours.
+ context: *http, server, location*
- redis2_buffer_size
+ The timeout for connecting to the Redis server, in seconds by default.
- redis2_next_upstream
- syntax: redis2_next_upstream [ error | timeout | invalid_response | off ]
- default: error timeout
- context: http, server, location
+ It's wise to always explicitly specify the time unit to avoid confusion.
+ Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years),
+ "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes).
- Specify which failure conditions should cause the request to be forwarded to another
- upstream server. Applies only when the value in redis2_pass is an upstream with two or more
- servers.
+ This time must be less than 597 hours.
- Here's an artificial example:
+ redis2_send_timeout
+ syntax: *redis2_send_timeout <time>*
- upstream redis_cluster {
- server 127.0.0.1:6379;
- server 127.0.0.1:6380;
- }
+ default: *60s*
- server {
- location /redis {
- redis2_next_upstream error timeout invalid_response;
- redis2_query get foo;
- redis2_pass redis_cluster;
- }
+ context: *http, server, location*
+
+ The timeout for sending TCP requests to the Redis server, in seconds by
+ default.
+
+ It's wise to always explicitly specify the time unit to avoid confusion.
+ Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years),
+ "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes).
+
+ redis2_read_timeout
+ syntax: *redis2_read_timeout <time>*
+
+ default: *60s*
+
+ context: *http, server, location*
+
+ The timeout for reading TCP responses from the redis server, in seconds
+ by default.
+
+ It's wise to always explicitly specify the time unit to avoid confusion.
+ Time units supported are "s"(seconds), "ms"(milliseconds), "y"(years),
+ "M"(months), "w"(weeks), "d"(days), "h"(hours), and "m"(minutes).
+
+ redis2_buffer_size
+ syntax: *redis2_buffer_size <size>*
+
+ default: *4k/8k*
+
+ context: *http, server, location*
+
+ This buffer size is used for reading Redis replies, but it's not
+ required to be as big as the largest possible Redis reply.
+
+ This default size is the page size, may be 4k or 8k.
+
+ redis2_next_upstream
+ syntax: *redis2_next_upstream [ error | timeout | invalid_response | off
+ ]*
+
+ default: *error timeout*
+
+ context: *http, server, location*
+
+ Specify which failure conditions should cause the request to be
+ forwarded to another upstream server. Applies only when the value in
+ redis2_pass is an upstream with two or more servers.
+
+ Here's an artificial example: upstream redis_cluster { server
+ 127.0.0.1:6379; server 127.0.0.1:6380; }
+
+ server {
+ location /redis {
+ redis2_next_upstream error timeout invalid_response;
+ redis2_query get foo;
+ redis2_pass redis_cluster;
}
+ }
Connection Pool
- You can use Maxim Dounin's excellent ngx_http_upstream_keepalive module
- ( http://mdounin.ru/hg/ngx_http_upstream_keepalive/ )
- with this module to privide TCP connection pool
- for Redis.
+ You can use the excellent [[HttpUpstreamKeepaliveModule]] with this
+ module to privide TCP connection pool for Redis.
A sample config snippet looks like this
- http {
- upstream backend {
- server 127.0.0.1:6379;
+ http {
+ upstream backend {
+ server 127.0.0.1:6379;
- # a pool with at most 1024 connections
- # and do not distinguish the servers:
- keepalive 1024 single;
- }
+ # a pool with at most 1024 connections
+ # and do not distinguish the servers:
+ keepalive 1024 single;
+ }
- server {
- ...
- location /redis {
- set_unescape_uri $query $arg_query;
- redis2_query $query;
- redis2_pass backend;
+ server {
+ ...
+ location /redis {
+ set_unescape_uri $query $arg_query;
+ redis2_query $query;
+ redis2_pass backend;
+ }
}
}
- }
Lua Interoperability
- This module can be served as a non-blocking redis2 client
- for ngx_lua ( http://github.com/chaoslawful/lua-nginx-module ).
+ This module can be served as a non-blocking redis2 client for
+ [[HttpLuaModule]].
Here's an example using a GET subrequest:
@@ -275,24 +303,24 @@ Lua Interoperability
';
}
- Then accessing /main yields
+ Then accessing "/main" yields
[+PONG\r\n]
- where "\r\n" is CRLF. That is, this module returns the *raw* TCP responses
- from the remote redis server. For lua-based application developers, they
- may want to utilize the lua-redis-parser module (written in pure C) to
- parse such raw responses into Lua data structures:
-
- https://github.com/agentzh/lua-redis-parser
+ where "\r\n" is "CRLF". That is, this module returns the *raw* TCP
+ responses from the remote redis server. For Lua-based application
+ developers, they may want to utilize the [[LuaRedisParser]] library
+ (written in pure C) to parse such raw responses into Lua data
+ structures.
- When moving the inlined Lua code into an external .lua file, it's important
- to use the escape sequence "\r\n" directly. We used "\\r\\n" above just because
- the Lua code itself needs quoting when being put into an nginx string literal.
+ When moving the inlined Lua code into an external ".lua" file, it's
+ important to use the escape sequence "\r\n" directly. We used "\\r\\n"
+ above just because the Lua code itself needs quoting when being put into
+ an Nginx string literal.
- You can also use POST/PUT subrequests to transfer the raw redis
- request via request body, which does not require URI escaping
- and unescaping, thus saving some CPU cycles. Here's such an example:
+ You can also use POST/PUT subrequests to transfer the raw Redis request
+ via request body, which does not require URI escaping and unescaping,
+ thus saving some CPU cycles. Here's such an example:
location /redis {
internal;
@@ -315,9 +343,8 @@ Lua Interoperability
This yeilds exactly the same output as the previous (GET) sample.
- One can also use Lua to pick up a concrete Redis backend
- based on some complicated hashing rules. For
- instance,
+ One can also use Lua to pick up a concrete Redis backend based on some
+ complicated hashing rules. For instance,
upstream redis-a {
server foo.bar.com:6379;
@@ -351,86 +378,164 @@ Lua Interoperability
{ args = {
query = '...',
backend = srv
- } }
+ }
+ }
)
ngx.say(res.body)
";
}
}
+ Pipelined Redis Requests by Lua
+ Here's a complete example demonstrating how to use Lua to issue multiple
+ pipelined Redis requests via this Nginx module.
+
+ First of all, we include the following in our "nginx.conf" file:
+
+ location = /redis2 {
+ internal;
+
+ redis2_raw_queries $args $echo_request_body;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location = /test {
+ content_by_lua_file conf/test.lua;
+ }
+
+ Basically we use URI query args to pass the number of Redis requests and
+ request body to pass the pipelined Redis request string.
+
+ And then we create the "conf/test.lua" file (whose path is relative to
+ the server root of Nginx) to include the following Lua code:
+
+ -- conf/test.lua
+ local parser = require "redis.parser"
+
+ local reqs = {
+ {"set", "foo", "hello world"},
+ {"get", "foo"}
+ }
+
+ local raw_reqs = {}
+ for i, req in ipairs(reqs) do
+ table.insert(raw_reqs, parser.build_query(req))
+ end
+
+ local res = ngx.location.capture("/redis2?" .. #reqs,
+ { body = table.concat(raw_reqs, "") })
+
+ if res.status ~= 200 or not res.body then
+ ngx.log(ngx.ERR, "failed to query redis")
+ ngx.exit(500)
+ end
+
+ local replies = parser.parse_replies(res.body, #reqs)
+ for i, reply in ipairs(replies) do
+ ngx.say(reply[1])
+ end
+
+ Here we assume that your Redis server is listening on the default port
+ (6379) of the localhost. We also make use of the [[LuaRedisParser]]
+ library to construct raw Redis queries for us and also use it to parse
+ the replies.
+
+ Accessing the "/test" location via HTTP clients like "curl" yields the
+ following output
+
+ OK hello world
+
+ A more realistic setting is to use a proper upstream definition for our
+ Redis backend and enable TCP connection pool via the keepalive directive
+ in it.
+
Redis Publish/Subscribe Support
- This module has limited support for redis publish/subscribe feature.
- It cannot be fully supported due to the stateless nature of REST and
- HTTP model.
+ This module has limited support for Redis publish/subscribe feature. It
+ cannot be fully supported due to the stateless nature of REST and HTTP
+ model.
Consider the following example:
- location /redis {
- redis2_raw_queries 2 "subscribe /foo/bar\r\n";
- redis2_pass 127.0.0.1:6379;
- }
+ location /redis {
+ redis2_raw_queries 2 "subscribe /foo/bar\r\n";
+ redis2_pass 127.0.0.1:6379;
+ }
- And then publish a message for the key /foo/bar in the redis-cli
+ And then publish a message for the key "/foo/bar" in the "redis-cli"
command line. And then you'll receive two multi-bulk replies from the
- /redis location.
+ "/redis" location.
- You can surely parse the replies with the lua-redis-parser library if
+ You can surely parse the replies with the [[LuaRedisParser]] library if
you're using Lua to access this module's location.
- Some limitations:
-
- 1. You cannot use ngx_http_upstream_keepalive with this redis
- upstream. Only short redis connections will work.
+ Publish/Subscribe Limitations
+ * You cannot use [[HttpUpstreamKeepaliveModule]] with this Redis
+ upstream. Only short Redis connections will work.
- 2. There may be some race conditions that produce the harmless "Redis
- server returned extra bytes" warnings in your nginx's error.log. Such
- warnings might be rare but just be prepared for it.
+ * There may be some race conditions that produce the harmless "Redis
+ server returned extra bytes" warnings in your nginx's error.log.
+ Such warnings might be rare but just be prepared for it.
- 3. You should tune the various timeout settings provided by ngx_redis2.
+ * You should tune the various timeout settings provided by this module
+ like redis2_connect_timeout and redis2_read_timeout.
Performance Tuning
- When you're using ngx_redis2, please ensure you're using a tcp
- connection pool (provided by ngx_http_upstream_keepalive)
- and redis pipelining wherever possible. These features will
- significantly improve performance. Also, using multiple instance of
- redis servers on your multi-core machines also help a lot due to the
- sequential processing nature of a single redis server instance.
-
- Also, when you're benchmarking performance using something like ab or
- http_load, please ensure that your error log level is high enough (like
- "warn") to prevent nginx workers spend too much cycles on flushing the
- error.log file, which is always unbuffered and blocking and thus very
- expensive.
+ * When you're using this module, please ensure you're using a TCP
+ connection pool (provided by [[HttpUpstreamKeepaliveModule]]) and
+ Redis pipelining wherever possible. These features will
+ significantly improve performance.
+
+ * Using multiple instance of Redis servers on your multi-core machines
+ also help a lot due to the sequential processing nature of a single
+ Redis server instance.
+
+ * When you're benchmarking performance using something like "ab" or
+ "http_load", please ensure that your error log level is high enough
+ (like "warn") to prevent Nginx workers spend too much cycles on
+ flushing the "error.log" file, which is always non-buffered and
+ blocking and thus very expensive.
Installation
- 1. Grab the nginx source code from nginx.net (<http://nginx.net/>), for
- example, the version 0.8.54 (see nginx compatibility),
+ You are recommended to install this module (as well as the Nginx core
+ and many many other goodies) via the ngx_openresty bundle
+ (<http://openresty.org>). Check out the installation instructions
+ (<http://openresty.org/#Installation>) for setting up ngx_openresty
+ (<http://openresty.org>).
- 2. and then build the source with this module:
+ Alternatively, you can install this module manually by recompiling the
+ standard Nginx core as follows:
- $ wget 'http://sysoev.ru/nginx/nginx-0.8.54.tar.gz'
- $ tar -xzvf nginx-0.8.54.tar.gz
- $ cd nginx-0.8.54/
+ * Grab the nginx source code from nginx.org (<http://nginx.org>), for
+ example, the version 1.0.8 (see nginx compatibility),
- # Here we assume you would install you nginx under /opt/nginx/.
- $ ./configure --prefix=/opt/nginx \
- --add-module=/path/to/redis2-nginx-module
+ * and then download the latest version of the release tarball of this
+ module from ngx_redis2's file list
+ (<http://github.com/agentzh/redis2-nginx-module/tags>).
+
+ * and finally build the source with this module:
- $ make -j2
- $ make install
+ wget 'http://nginx.org/download/nginx-1.0.8.tar.gz'
+ tar -xzvf nginx-1.0.8.tar.gz
+ cd nginx-1.0.8/
- Download the latest version of the release tarball of this module from
- redis2-nginx-module file list
- (<http://github.com/agentzh/redis2-nginx-module/downloads>).
+ # Here we assume you would install you nginx under /opt/nginx/.
+ ./configure --prefix=/opt/nginx \
+ --add-module=/path/to/redis2-nginx-module
+
+ make -j2
+ make install
Compatibility
- Redis 2.0, 2.2, and above should work with this module
- without any issues. So is the "Alchemy Database"
- (aka redisql).
+ Redis 2.0, 2.2, and above should work with this module without any
+ issues. So is the Alchemy Database
+ (<http://code.google.com/p/alchemydatabase/>) (aka redisql in its early
+ days).
The following versions of Nginx should work with this module:
- * 1.0.x (last tested: 1.0.2)
+ * 1.1.x (last tested: 1.1.5)
+
+ * 1.0.x (last tested: 1.0.10)
* 0.9.x (last tested: 0.9.4)
@@ -446,33 +551,34 @@ Report Bugs
there must be some serious bugs lurking somewhere in this module. So
whenever you are bitten by any quirks, please don't hesitate to
- 1. send a bug report or even patches to <agentzh@gmail.com>,
+ * create a ticket on the issue tracking interface
+ (<http://github.com/agentzh/redis2-nginx-module/issues>) provided by
+ GitHub.
- 2. or create a ticket on the issue tracking interface
- (<http://github.com/agentzh/redis2-nginx-module/issues>)
- provided by GitHub.
+ * or send a bug report or even patches to the Nginx mailing list
+ (<http://mailman.nginx.org/mailman/listinfo/nginx>).
Source Repository
Available on github at agentzh/redis2-nginx-module
(<http://github.com/agentzh/redis2-nginx-module>).
TODO
- * Add the "redis2_as_json" directive to allow emitting JSON directly.
+ * Add the "redis2_as_json" directive to allow emitting JSON directly.
-AUTHORS
- Yichun "agentzh" Zhang (章亦春) < agentzh@gmail.com >
+Author
+ Zhang "agentzh" Yichun (章亦春) <agentzh@gmail.com>
Getting involved
You'll be very welcomed to submit patches to the author or just ask for
a commit bit to the source repository on GitHub.
Copyright & License
- Copyright (c) 2010, 2011 Taobao Inc., Alibaba Group ( http://www.taobao.com
- ).
+ This module is licenced under the BSD license.
- Copyright (c) 2010, 2011 Yichun "agentzh" Zhang (章亦春) <agentzh@gmail.com>.
+ Copyright (C) 2010, 2011, by Zhang "agentzh" Yichun (章亦春)
+ <agentzh@gmail.com>.
- This module is licensed under the terms of the BSD license.
+ All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
@@ -498,5 +604,12 @@ Copyright & License
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SEE ALSO
- * a redis response parser for Lua: https://github.com/agentzh/lua-redis-parser
+ * The Redis (<http://redis.io/>) server homepage.
+
+ * a redis response parser and a request constructor for Lua:
+ [[LuaRedisParser]].
+
+ * [[HttpLuaModule]]
+
+ * The ngx_openresty bundle (<http://openresty.org>).
View
584 README.markdown
@@ -0,0 +1,584 @@
+Name
+====
+
+ngx_redis2 - Nginx upstream module for the Redis 2.0 protocol
+
+*This module is not distributed with the Nginx source.* See [the installation instructions](http://wiki.nginx.org/HttpRedis2Module#Installation).
+
+Status
+======
+
+This module is already production ready.
+
+Version
+=======
+
+This document describes ngx_redis2 [v0.08rc2](https://github.com/agentzh/redis2-nginx-module/tags) released on 24 November 2011.
+
+Synopsis
+========
+
+
+ location /foo {
+ set $value 'first';
+ redis2_query set one $value;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ # GET /get?key=some_key
+ location /get {
+ set_unescape_uri $key $arg_key; # this requires ngx_set_misc
+ redis2_query get $key;
+ redis2_pass foo.com:6379;
+ }
+
+ # GET /set?key=one&val=first%20value
+ location /set {
+ set_unescape_uri $key $arg_key; # this requires ngx_set_misc
+ set_unescape_uri $val $arg_val; # this requires ngx_set_misc
+ redis2_query set $key $val;
+ redis2_pass foo.com:6379;
+ }
+
+ # multiple pipelined queries
+ location /foo {
+ set $value 'first';
+ redis2_query set one $value;
+ redis2_query get one;
+ redis2_query set one two;
+ redis2_query get one;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location /bar {
+ # $ is not special here...
+ redis2_literal_raw_query '*1\r\n$4\r\nping\r\n';
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location /bar {
+ # variables can be used below and $ is special
+ redis2_raw_query 'get one\r\n';
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ # GET /baz?get%20foo%0d%0a
+ location /baz {
+ set_unescape_uri $query $query_string; # this requires the ngx_set_misc module
+ redis2_raw_query $query;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+
+Description
+===========
+
+This is an Nginx upstream module that makes nginx talk to a [Redis](http://redis.io/) 2.x server in a non-blocking way. The full Redis 2.0 unified protocol has been implemented including the Redis pipelining support.
+
+This module returns the raw TCP response from the Redis server. It's recommended to use my [LuaRedisParser](http://wiki.nginx.org/LuaRedisParser) (written in pure C) to parse these responses into lua data structure when combined with [HttpLuaModule](http://wiki.nginx.org/HttpLuaModule).
+
+If you only want to use the `get` redis command, you can try out the [HttpRedisModule](http://wiki.nginx.org/HttpRedisModule). It returns the parsed content part of the Redis response because only `get` is needed to implement.
+
+Another option is to parse the redis responses on your client side yourself.
+
+Directives
+==========
+
+redis2_query
+------------
+**syntax:** *redis2_query cmd arg1 arg2 ...*
+
+**default:** *no*
+
+**context:** *location, location if*
+
+Specify a Redis command by specifying its individual arguments (including the Redis command name itself) in a similar way to the `redis-cli` utility.
+
+Multiple instances of this directive are allowed in a single location and these queries will be pipelined. For example,
+
+
+ location /pipelined {
+ redis2_query set hello world;
+ redis2_query get hello;
+
+ redis2_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT;
+ }
+
+
+then `GET /pipelined` will yield two successive raw Redis responses
+
+
+ +OK
+ $5
+ world
+
+
+while newlines here are actually `CR LF` (`\r\n`).
+
+redis2_raw_query
+----------------
+**syntax:** *redis2_raw_query QUERY*
+
+**default:** *no*
+
+**context:** *location, location if*
+
+Specify raw Redis queries and nginx variables are recognized in the `QUERY` argument.
+
+Only *one* Redis command is allowed in the `QUERY` argument, or you'll receive an error. If you want to specify multiple pipelined commands in a single query, use the [redis2_raw_queries](http://wiki.nginx.org/HttpRedis2Module#redis2_raw_queries) directive instead.
+
+redis2_raw_queries
+------------------
+**syntax:** *redis2_raw_queries N QUERIES*
+
+**default:** *no*
+
+**context:** *location, location if*
+
+Specify `N` commands in the `QUERIES` argument. Both the `N` and `QUERIES`
+arguments can take Nginx variables.
+
+Here's some examples
+
+ location /pipelined {
+ redis2_raw_queries 3 "flushall\r\nget key1\r\nget key2\r\n";
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ # GET /pipelined2?n=2&cmds=flushall%0D%0Aget%20key%0D%0A
+ location /pipelined2 {
+ set_unescape_uri $n $arg_n;
+ set_unescape_uri $cmds $arg_cmds;
+
+ redis2_raw_queries $n $cmds;
+
+ redis2_pass 127.0.0.1:6379;
+ }
+
+Note that in the second sample above, the [set_unescape_uri](http://wiki.nginx.org/HttpSetMiscModule#set_unescape_uri) directive is provided by the [HttpSetMiscModule](http://wiki.nginx.org/HttpSetMiscModule).
+
+redis2_literal_raw_query
+------------------------
+**syntax:** *redis2_literal_raw_query QUERY*
+
+**default:** *no*
+
+**context:** *location, location if*
+
+Specify a raw Redis query but Nginx variables in it will not be *not* recognized. In other words, you're free to use the dollar sign character (`$`) in your `QUERY` argument.
+
+Only One redis command is allowed in the `QUERY` argument.
+
+redis2_pass
+-----------
+**syntax:** *redis2_pass &lt;upstream_name&gt;*
+
+**syntax:** *redis2_pass &lt;host&gt;:&lt;port&gt;*
+
+**default:** *no*
+
+**context:** *location, location if*
+
+**phase:** *content*
+
+Specify the Redis server backend.
+
+redis2_connect_timeout
+----------------------
+**syntax:** *redis2_connect_timeout &lt;time&gt;*
+
+**default:** *60s*
+
+**context:** *http, server, location*
+
+The timeout for connecting to the Redis server, in seconds by default.
+
+It's wise to always explicitly specify the time unit to avoid confusion. Time units supported are `s`(seconds), `ms`(milliseconds), `y`(years), `M`(months), `w`(weeks), `d`(days), `h`(hours), and `m`(minutes).
+
+This time must be less than 597 hours.
+
+redis2_send_timeout
+-------------------
+**syntax:** *redis2_send_timeout &lt;time&gt;*
+
+**default:** *60s*
+
+**context:** *http, server, location*
+
+The timeout for sending TCP requests to the Redis server, in seconds by default.
+
+It's wise to always explicitly specify the time unit to avoid confusion. Time units supported are `s`(seconds), `ms`(milliseconds), `y`(years), `M`(months), `w`(weeks), `d`(days), `h`(hours), and `m`(minutes).
+
+redis2_read_timeout
+-------------------
+**syntax:** *redis2_read_timeout &lt;time&gt;*
+
+**default:** *60s*
+
+**context:** *http, server, location*
+
+The timeout for reading TCP responses from the redis server, in seconds by default.
+
+It's wise to always explicitly specify the time unit to avoid confusion. Time units supported are `s`(seconds), `ms`(milliseconds), `y`(years), `M`(months), `w`(weeks), `d`(days), `h`(hours), and `m`(minutes).
+
+redis2_buffer_size
+------------------
+**syntax:** *redis2_buffer_size &lt;size&gt;*
+
+**default:** *4k/8k*
+
+**context:** *http, server, location*
+
+This buffer size is used for reading Redis replies, but it's not required to be as big as the largest possible Redis reply.
+
+This default size is the page size, may be 4k or 8k.
+
+redis2_next_upstream
+--------------------
+**syntax:** *redis2_next_upstream [ error | timeout | invalid_response | off ]*
+
+**default:** *error timeout*
+
+**context:** *http, server, location*
+
+Specify which failure conditions should cause the request to be forwarded to another
+upstream server. Applies only when the value in [redis2_pass](http://wiki.nginx.org/HttpRedis2Module#redis2_pass) is an upstream with two or more
+servers.
+
+Here's an artificial example:
+
+ upstream redis_cluster {
+ server 127.0.0.1:6379;
+ server 127.0.0.1:6380;
+ }
+
+ server {
+ location /redis {
+ redis2_next_upstream error timeout invalid_response;
+ redis2_query get foo;
+ redis2_pass redis_cluster;
+ }
+ }
+
+
+Connection Pool
+===============
+
+You can use the excellent [HttpUpstreamKeepaliveModule](http://wiki.nginx.org/HttpUpstreamKeepaliveModule) with this module to privide TCP connection pool for Redis.
+
+A sample config snippet looks like this
+
+
+ http {
+ upstream backend {
+ server 127.0.0.1:6379;
+
+ # a pool with at most 1024 connections
+ # and do not distinguish the servers:
+ keepalive 1024 single;
+ }
+
+ server {
+ ...
+ location /redis {
+ set_unescape_uri $query $arg_query;
+ redis2_query $query;
+ redis2_pass backend;
+ }
+ }
+ }
+
+
+Lua Interoperability
+====================
+
+This module can be served as a non-blocking redis2 client for [HttpLuaModule](http://wiki.nginx.org/HttpLuaModule).
+
+Here's an example using a GET subrequest:
+
+
+ location /redis {
+ internal;
+
+ # set_unescape_uri is provided by ngx_set_misc
+ set_unescape_uri $query $arg_query;
+
+ redis2_raw_query $query;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location /main {
+ content_by_lua '
+ local res = ngx.location.capture("/redis",
+ { args = { query = "ping\\r\\n" } }
+ )
+ ngx.print("[" .. res.body .. "]")
+ ';
+ }
+
+
+Then accessing `/main` yields
+
+
+ [+PONG\r\n]
+
+
+where `\r\n` is `CRLF`. That is, this module returns the *raw* TCP responses from the remote redis server. For Lua-based application developers, they may want to utilize the [LuaRedisParser](http://wiki.nginx.org/LuaRedisParser) library (written in pure C) to parse such raw responses into Lua data structures.
+
+When moving the inlined Lua code into an external `.lua` file, it's important to use the escape sequence `\r\n` directly. We used `\\r\\n` above just because the Lua code itself needs quoting when being put into an Nginx string literal.
+
+You can also use POST/PUT subrequests to transfer the raw Redis request via request body, which does not require URI escaping and unescaping, thus saving some CPU cycles. Here's such an example:
+
+
+ location /redis {
+ internal;
+
+ # $echo_request_body is provided by the ngx_echo module
+ redis2_raw_query $echo_request_body;
+
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location /main {
+ content_by_lua '
+ local res = ngx.location.capture("/redis",
+ { method = ngx.HTTP_PUT,
+ body = "ping\\r\\n" }
+ )
+ ngx.print("[" .. res.body .. "]")
+ ';
+ }
+
+
+This yeilds exactly the same output as the previous (GET) sample.
+
+One can also use Lua to pick up a concrete Redis backend based on some complicated hashing rules. For instance,
+
+
+ upstream redis-a {
+ server foo.bar.com:6379;
+ }
+
+ upstream redis-b {
+ server bar.baz.com:6379;
+ }
+
+ upstream redis-c {
+ server blah.blah.org:6379;
+ }
+
+ server {
+ ...
+
+ location /redis {
+ set_unescape_uri $query $arg_query;
+ redis2_query $query;
+ redis2_pass $arg_backend;
+ }
+
+ location /foo {
+ content_by_lua "
+ -- pick up a server randomly
+ local servers = {'redis-a', 'redis-b', 'redis-c'}
+ local i = ngx.time() % #servers + 1;
+ local srv = servers[i]
+
+ local res = ngx.location.capture('/redis',
+ { args = {
+ query = '...',
+ backend = srv
+ }
+ }
+ )
+ ngx.say(res.body)
+ ";
+ }
+ }
+
+
+Pipelined Redis Requests by Lua
+-------------------------------
+
+Here's a complete example demonstrating how to use Lua to issue multiple pipelined Redis requests via this Nginx module.
+
+First of all, we include the following in our `nginx.conf` file:
+
+
+ location = /redis2 {
+ internal;
+
+ redis2_raw_queries $args $echo_request_body;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location = /test {
+ content_by_lua_file conf/test.lua;
+ }
+
+
+Basically we use URI query args to pass the number of Redis requests and request body to pass the pipelined Redis request string.
+
+And then we create the `conf/test.lua` file (whose path is relative to the server root of Nginx) to include the following Lua code:
+
+
+ -- conf/test.lua
+ local parser = require "redis.parser"
+
+ local reqs = {
+ {"set", "foo", "hello world"},
+ {"get", "foo"}
+ }
+
+ local raw_reqs = {}
+ for i, req in ipairs(reqs) do
+ table.insert(raw_reqs, parser.build_query(req))
+ end
+
+ local res = ngx.location.capture("/redis2?" .. #reqs,
+ { body = table.concat(raw_reqs, "") })
+
+ if res.status ~= 200 or not res.body then
+ ngx.log(ngx.ERR, "failed to query redis")
+ ngx.exit(500)
+ end
+
+ local replies = parser.parse_replies(res.body, #reqs)
+ for i, reply in ipairs(replies) do
+ ngx.say(reply[1])
+ end
+
+
+Here we assume that your Redis server is listening on the default port (6379) of the localhost. We also make use of the [LuaRedisParser](http://wiki.nginx.org/LuaRedisParser) library to construct raw Redis queries for us and also use it to parse the replies.
+
+Accessing the `/test` location via HTTP clients like `curl` yields the following output
+
+
+ OK
+ hello world
+
+
+A more realistic setting is to use a proper upstream definition for our Redis backend and enable TCP connection pool via the [keepalive](http://wiki.nginx.org/HttpUpstreamKeepaliveModule#keepalive) directive in it.
+
+Redis Publish/Subscribe Support
+===============================
+
+This module has limited support for Redis publish/subscribe feature. It cannot be fully supported due to the stateless nature of REST and HTTP model.
+
+Consider the following example:
+
+
+ location /redis {
+ redis2_raw_queries 2 "subscribe /foo/bar\r\n";
+ redis2_pass 127.0.0.1:6379;
+ }
+
+
+And then publish a message for the key `/foo/bar` in the `redis-cli` command line. And then you'll receive two multi-bulk replies from the `/redis` location.
+
+You can surely parse the replies with the [LuaRedisParser](http://wiki.nginx.org/LuaRedisParser) library if you're using Lua to access this module's location.
+
+Publish/Subscribe Limitations
+-----------------------------
+
+* You cannot use [HttpUpstreamKeepaliveModule](http://wiki.nginx.org/HttpUpstreamKeepaliveModule) with this Redis upstream. Only short Redis connections will work.
+* There may be some race conditions that produce the harmless `Redis server returned extra bytes` warnings in your nginx's error.log. Such warnings might be rare but just be prepared for it.
+* You should tune the various timeout settings provided by this module like [redis2_connect_timeout](http://wiki.nginx.org/HttpRedis2Module#redis2_connect_timeout) and [redis2_read_timeout](http://wiki.nginx.org/HttpRedis2Module#redis2_read_timeout).
+
+Performance Tuning
+==================
+
+* When you're using this module, please ensure you're using a TCP connection pool (provided by [HttpUpstreamKeepaliveModule](http://wiki.nginx.org/HttpUpstreamKeepaliveModule)) and Redis pipelining wherever possible. These features will significantly improve performance.
+* Using multiple instance of Redis servers on your multi-core machines also help a lot due to the sequential processing nature of a single Redis server instance.
+* When you're benchmarking performance using something like `ab` or `http_load`, please ensure that your error log level is high enough (like `warn`) to prevent Nginx workers spend too much cycles on flushing the `error.log` file, which is always non-buffered and blocking and thus very expensive.
+
+Installation
+============
+
+You are recommended to install this module (as well as the Nginx core and many many other goodies) via the [ngx_openresty bundle](http://openresty.org). Check out the [installation instructions](http://openresty.org/#Installation) for setting up [ngx_openresty](http://openresty.org).
+
+Alternatively, you can install this module manually by recompiling the standard Nginx core as follows:
+
+* Grab the nginx source code from [nginx.org](http://nginx.org), for example, the version 1.0.8 (see nginx compatibility),
+* and then download the latest version of the release tarball of this module from ngx_redis2's [file list](http://github.com/agentzh/redis2-nginx-module/tags).
+* and finally build the source with this module:
+
+ wget 'http://nginx.org/download/nginx-1.0.8.tar.gz'
+ tar -xzvf nginx-1.0.8.tar.gz
+ cd nginx-1.0.8/
+
+ # Here we assume you would install you nginx under /opt/nginx/.
+ ./configure --prefix=/opt/nginx \
+ --add-module=/path/to/redis2-nginx-module
+
+ make -j2
+ make install
+
+
+Compatibility
+=============
+
+Redis 2.0, 2.2, and above should work with this module without any issues. So is the [Alchemy Database](http://code.google.com/p/alchemydatabase/) (aka redisql in its early days).
+
+The following versions of Nginx should work with this module:
+
+* 1.1.x (last tested: 1.1.5)
+* 1.0.x (last tested: 1.0.10)
+* 0.9.x (last tested: 0.9.4)
+* 0.8.x >= 0.8.31 (last tested: 0.8.54)
+
+Earlier versions of Nginx will *not* work.
+
+If you find that any particular version of Nginx above 0.8.31 does not work with this module, please consider reporting a bug.
+
+Report Bugs
+===========
+
+Although a lot of effort has been put into testing and code tuning, there must be some serious bugs lurking somewhere in this module. So whenever you are bitten by any quirks, please don't hesitate to
+
+* create a ticket on the [issue tracking interface](http://github.com/agentzh/redis2-nginx-module/issues) provided by GitHub.
+* or send a bug report or even patches to the [Nginx mailing list](http://mailman.nginx.org/mailman/listinfo/nginx).
+
+Source Repository
+=================
+
+Available on github at [agentzh/redis2-nginx-module](http://github.com/agentzh/redis2-nginx-module).
+
+TODO
+====
+* Add the `redis2_as_json` directive to allow emitting JSON directly.
+
+Author
+======
+
+Zhang "agentzh" Yichun (章亦春) <agentzh@gmail.com>
+
+Getting involved
+================
+
+You'll be very welcomed to submit patches to the author or just ask for
+a commit bit to the source repository on GitHub.
+
+Copyright & License
+===================
+
+This module is licenced under the BSD license.
+
+Copyright (C) 2010, 2011, by Zhang "agentzh" Yichun (章亦春) <agentzh@gmail.com>.
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+SEE ALSO
+========
+* The [Redis](http://redis.io/) server homepage.
+* a redis response parser and a request constructor for Lua: [LuaRedisParser](http://wiki.nginx.org/LuaRedisParser).
+* [HttpLuaModule](http://wiki.nginx.org/HttpLuaModule)
+* The [ngx_openresty bundle](http://openresty.org).
+
View
553 doc/HttpRedis2Module.wiki
@@ -0,0 +1,553 @@
+= Name =
+
+ngx_redis2 - Nginx upstream module for the Redis 2.0 protocol
+
+''This module is not distributed with the Nginx source.'' See [[#Installation|the installation instructions]].
+
+= Status =
+
+This module is already production ready.
+
+= Version =
+
+This document describes ngx_redis2 [https://github.com/agentzh/redis2-nginx-module/tags v0.08rc2] released on 24 November 2011.
+
+= Synopsis =
+
+<geshi lang="nginx">
+ location /foo {
+ set $value 'first';
+ redis2_query set one $value;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ # GET /get?key=some_key
+ location /get {
+ set_unescape_uri $key $arg_key; # this requires ngx_set_misc
+ redis2_query get $key;
+ redis2_pass foo.com:6379;
+ }
+
+ # GET /set?key=one&val=first%20value
+ location /set {
+ set_unescape_uri $key $arg_key; # this requires ngx_set_misc
+ set_unescape_uri $val $arg_val; # this requires ngx_set_misc
+ redis2_query set $key $val;
+ redis2_pass foo.com:6379;
+ }
+
+ # multiple pipelined queries
+ location /foo {
+ set $value 'first';
+ redis2_query set one $value;
+ redis2_query get one;
+ redis2_query set one two;
+ redis2_query get one;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location /bar {
+ # $ is not special here...
+ redis2_literal_raw_query '*1\r\n$4\r\nping\r\n';
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location /bar {
+ # variables can be used below and $ is special
+ redis2_raw_query 'get one\r\n';
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ # GET /baz?get%20foo%0d%0a
+ location /baz {
+ set_unescape_uri $query $query_string; # this requires the ngx_set_misc module
+ redis2_raw_query $query;
+ redis2_pass 127.0.0.1:6379;
+ }
+</geshi>
+
+= Description =
+
+This is an Nginx upstream module that makes nginx talk to a [http://redis.io/ Redis] 2.x server in a non-blocking way. The full Redis 2.0 unified protocol has been implemented including the Redis pipelining support.
+
+This module returns the raw TCP response from the Redis server. It's recommended to use my [[LuaRedisParser]] (written in pure C) to parse these responses into lua data structure when combined with [[HttpLuaModule]].
+
+If you only want to use the <code>get</code> redis command, you can try out the [[HttpRedisModule]]. It returns the parsed content part of the Redis response because only <code>get</code> is needed to implement.
+
+Another option is to parse the redis responses on your client side yourself.
+
+= Directives =
+
+== redis2_query ==
+'''syntax:''' ''redis2_query cmd arg1 arg2 ...''
+
+'''default:''' ''no''
+
+'''context:''' ''location, location if''
+
+Specify a Redis command by specifying its individual arguments (including the Redis command name itself) in a similar way to the <code>redis-cli</code> utility.
+
+Multiple instances of this directive are allowed in a single location and these queries will be pipelined. For example,
+
+<geshi lang="nginx">
+ location /pipelined {
+ redis2_query set hello world;
+ redis2_query get hello;
+
+ redis2_pass 127.0.0.1:$TEST_NGINX_REDIS_PORT;
+ }
+</geshi>
+
+then <code>GET /pipelined</code> will yield two successive raw Redis responses
+
+<geshi lang="nginx">
+ +OK
+ $5
+ world
+</geshi>
+
+while newlines here are actually <code>CR LF</code> (<code>\r\n</code>).
+
+== redis2_raw_query ==
+'''syntax:''' ''redis2_raw_query QUERY''
+
+'''default:''' ''no''
+
+'''context:''' ''location, location if''
+
+Specify raw Redis queries and nginx variables are recognized in the <code>QUERY</code> argument.
+
+Only ''one'' Redis command is allowed in the <code>QUERY</code> argument, or you'll receive an error. If you want to specify multiple pipelined commands in a single query, use the [[#redis2_raw_queries|redis2_raw_queries]] directive instead.
+
+== redis2_raw_queries ==
+'''syntax:''' ''redis2_raw_queries N QUERIES''
+
+'''default:''' ''no''
+
+'''context:''' ''location, location if''
+
+Specify <code>N</code> commands in the <code>QUERIES</code> argument. Both the <code>N</code> and <code>QUERIES</code>
+arguments can take Nginx variables.
+
+Here's some examples
+<geshi lang="nginx">
+ location /pipelined {
+ redis2_raw_queries 3 "flushall\r\nget key1\r\nget key2\r\n";
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ # GET /pipelined2?n=2&cmds=flushall%0D%0Aget%20key%0D%0A
+ location /pipelined2 {
+ set_unescape_uri $n $arg_n;
+ set_unescape_uri $cmds $arg_cmds;
+
+ redis2_raw_queries $n $cmds;
+
+ redis2_pass 127.0.0.1:6379;
+ }
+</geshi>
+Note that in the second sample above, the [[HttpSetMiscModule#set_unescape_uri|set_unescape_uri]] directive is provided by the [[HttpSetMiscModule]].
+
+== redis2_literal_raw_query ==
+'''syntax:''' ''redis2_literal_raw_query QUERY''
+
+'''default:''' ''no''
+
+'''context:''' ''location, location if''
+
+Specify a raw Redis query but Nginx variables in it will not be ''not'' recognized. In other words, you're free to use the dollar sign character (<code>$</code>) in your <code>QUERY</code> argument.
+
+Only One redis command is allowed in the <code>QUERY</code> argument.
+
+== redis2_pass ==
+'''syntax:''' ''redis2_pass <upstream_name>''
+
+'''syntax:''' ''redis2_pass <host>:<port>''
+
+'''default:''' ''no''
+
+'''context:''' ''location, location if''
+
+'''phase:''' ''content''
+
+Specify the Redis server backend.
+
+== redis2_connect_timeout ==
+'''syntax:''' ''redis2_connect_timeout <time>''
+
+'''default:''' ''60s''
+
+'''context:''' ''http, server, location''
+
+The timeout for connecting to the Redis server, in seconds by default.
+
+It's wise to always explicitly specify the time unit to avoid confusion. Time units supported are <code>s</code>(seconds), <code>ms</code>(milliseconds), <code>y</code>(years), <code>M</code>(months), <code>w</code>(weeks), <code>d</code>(days), <code>h</code>(hours), and <code>m</code>(minutes).
+
+This time must be less than 597 hours.
+
+== redis2_send_timeout ==
+'''syntax:''' ''redis2_send_timeout <time>''
+
+'''default:''' ''60s''
+
+'''context:''' ''http, server, location''
+
+The timeout for sending TCP requests to the Redis server, in seconds by default.
+
+It's wise to always explicitly specify the time unit to avoid confusion. Time units supported are <code>s</code>(seconds), <code>ms</code>(milliseconds), <code>y</code>(years), <code>M</code>(months), <code>w</code>(weeks), <code>d</code>(days), <code>h</code>(hours), and <code>m</code>(minutes).
+
+== redis2_read_timeout ==
+'''syntax:''' ''redis2_read_timeout <time>''
+
+'''default:''' ''60s''
+
+'''context:''' ''http, server, location''
+
+The timeout for reading TCP responses from the redis server, in seconds by default.
+
+It's wise to always explicitly specify the time unit to avoid confusion. Time units supported are <code>s</code>(seconds), <code>ms</code>(milliseconds), <code>y</code>(years), <code>M</code>(months), <code>w</code>(weeks), <code>d</code>(days), <code>h</code>(hours), and <code>m</code>(minutes).
+
+== redis2_buffer_size ==
+'''syntax:''' ''redis2_buffer_size <size>''
+
+'''default:''' ''4k/8k''
+
+'''context:''' ''http, server, location''
+
+This buffer size is used for reading Redis replies, but it's not required to be as big as the largest possible Redis reply.
+
+This default size is the page size, may be 4k or 8k.
+
+== redis2_next_upstream ==
+'''syntax:''' ''redis2_next_upstream [ error | timeout | invalid_response | off ]''
+
+'''default:''' ''error timeout''
+
+'''context:''' ''http, server, location''
+
+Specify which failure conditions should cause the request to be forwarded to another
+upstream server. Applies only when the value in [[#redis2_pass|redis2_pass]] is an upstream with two or more
+servers.
+
+Here's an artificial example:
+<geshi lang="nginx">
+ upstream redis_cluster {
+ server 127.0.0.1:6379;
+ server 127.0.0.1:6380;
+ }
+
+ server {
+ location /redis {
+ redis2_next_upstream error timeout invalid_response;
+ redis2_query get foo;
+ redis2_pass redis_cluster;
+ }
+ }
+</geshi>
+
+= Connection Pool =
+
+You can use the excellent [[HttpUpstreamKeepaliveModule]] with this module to privide TCP connection pool for Redis.
+
+A sample config snippet looks like this
+
+<geshi lang="nginx">
+ http {
+ upstream backend {
+ server 127.0.0.1:6379;
+
+ # a pool with at most 1024 connections
+ # and do not distinguish the servers:
+ keepalive 1024 single;
+ }
+
+ server {
+ ...
+ location /redis {
+ set_unescape_uri $query $arg_query;
+ redis2_query $query;
+ redis2_pass backend;
+ }
+ }
+ }
+</geshi>
+
+= Lua Interoperability =
+
+This module can be served as a non-blocking redis2 client for [[HttpLuaModule]].
+
+Here's an example using a GET subrequest:
+
+<geshi lang="nginx">
+ location /redis {
+ internal;
+
+ # set_unescape_uri is provided by ngx_set_misc
+ set_unescape_uri $query $arg_query;
+
+ redis2_raw_query $query;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location /main {
+ content_by_lua '
+ local res = ngx.location.capture("/redis",
+ { args = { query = "ping\\r\\n" } }
+ )
+ ngx.print("[" .. res.body .. "]")
+ ';
+ }
+</geshi>
+
+Then accessing <code>/main</code> yields
+
+<geshi lang="text">
+ [+PONG\r\n]
+</geshi>
+
+where <code>\r\n</code> is <code>CRLF</code>. That is, this module returns the ''raw'' TCP responses from the remote redis server. For Lua-based application developers, they may want to utilize the [[LuaRedisParser]] library (written in pure C) to parse such raw responses into Lua data structures.
+
+When moving the inlined Lua code into an external <code>.lua</code> file, it's important to use the escape sequence <code>\r\n</code> directly. We used <code>\\r\\n</code> above just because the Lua code itself needs quoting when being put into an Nginx string literal.
+
+You can also use POST/PUT subrequests to transfer the raw Redis request via request body, which does not require URI escaping and unescaping, thus saving some CPU cycles. Here's such an example:
+
+<geshi lang="nginx">
+ location /redis {
+ internal;
+
+ # $echo_request_body is provided by the ngx_echo module
+ redis2_raw_query $echo_request_body;
+
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location /main {
+ content_by_lua '
+ local res = ngx.location.capture("/redis",
+ { method = ngx.HTTP_PUT,
+ body = "ping\\r\\n" }
+ )
+ ngx.print("[" .. res.body .. "]")
+ ';
+ }
+</geshi>
+
+This yeilds exactly the same output as the previous (GET) sample.
+
+One can also use Lua to pick up a concrete Redis backend based on some complicated hashing rules. For instance,
+
+<geshi lang="nginx">
+ upstream redis-a {
+ server foo.bar.com:6379;
+ }
+
+ upstream redis-b {
+ server bar.baz.com:6379;
+ }
+
+ upstream redis-c {
+ server blah.blah.org:6379;
+ }
+
+ server {
+ ...
+
+ location /redis {
+ set_unescape_uri $query $arg_query;
+ redis2_query $query;
+ redis2_pass $arg_backend;
+ }
+
+ location /foo {
+ content_by_lua "
+ -- pick up a server randomly
+ local servers = {'redis-a', 'redis-b', 'redis-c'}
+ local i = ngx.time() % #servers + 1;
+ local srv = servers[i]
+
+ local res = ngx.location.capture('/redis',
+ { args = {
+ query = '...',
+ backend = srv
+ }
+ }
+ )
+ ngx.say(res.body)
+ ";
+ }
+ }
+</geshi>
+
+== Pipelined Redis Requests by Lua ==
+
+Here's a complete example demonstrating how to use Lua to issue multiple pipelined Redis requests via this Nginx module.
+
+First of all, we include the following in our <code>nginx.conf</code> file:
+
+<geshi lang="nginx">
+ location = /redis2 {
+ internal;
+
+ redis2_raw_queries $args $echo_request_body;
+ redis2_pass 127.0.0.1:6379;
+ }
+
+ location = /test {
+ content_by_lua_file conf/test.lua;
+ }
+</geshi>
+
+Basically we use URI query args to pass the number of Redis requests and request body to pass the pipelined Redis request string.
+
+And then we create the <code>conf/test.lua</code> file (whose path is relative to the server root of Nginx) to include the following Lua code:
+
+<geshi lang="lua">
+ -- conf/test.lua
+ local parser = require "redis.parser"
+
+ local reqs = {
+ {"set", "foo", "hello world"},
+ {"get", "foo"}
+ }
+
+ local raw_reqs = {}
+ for i, req in ipairs(reqs) do
+ table.insert(raw_reqs, parser.build_query(req))
+ end
+
+ local res = ngx.location.capture("/redis2?" .. #reqs,
+ { body = table.concat(raw_reqs, "") })
+
+ if res.status ~= 200 or not res.body then
+ ngx.log(ngx.ERR, "failed to query redis")
+ ngx.exit(500)
+ end
+
+ local replies = parser.parse_replies(res.body, #reqs)
+ for i, reply in ipairs(replies) do
+ ngx.say(reply[1])
+ end
+</geshi>
+
+Here we assume that your Redis server is listening on the default port (6379) of the localhost. We also make use of the [[LuaRedisParser]] library to construct raw Redis queries for us and also use it to parse the replies.
+
+Accessing the <code>/test</code> location via HTTP clients like <code>curl</code> yields the following output
+
+<geshi lang="text">
+OK
+hello world
+</geshi>
+
+A more realistic setting is to use a proper upstream definition for our Redis backend and enable TCP connection pool via the [[HttpUpstreamKeepaliveModule#keepalive|keepalive]] directive in it.
+
+= Redis Publish/Subscribe Support =
+
+This module has limited support for Redis publish/subscribe feature. It cannot be fully supported due to the stateless nature of REST and HTTP model.
+
+Consider the following example:
+
+<geshi lang="nginx">
+ location /redis {
+ redis2_raw_queries 2 "subscribe /foo/bar\r\n";
+ redis2_pass 127.0.0.1:6379;
+ }
+</geshi>
+
+And then publish a message for the key <code>/foo/bar</code> in the <code>redis-cli</code> command line. And then you'll receive two multi-bulk replies from the <code>/redis</code> location.
+
+You can surely parse the replies with the [[LuaRedisParser]] library if you're using Lua to access this module's location.
+
+== Publish/Subscribe Limitations ==
+
+* You cannot use [[HttpUpstreamKeepaliveModule]] with this Redis upstream. Only short Redis connections will work.
+* There may be some race conditions that produce the harmless <code>Redis server returned extra bytes</code> warnings in your nginx's error.log. Such warnings might be rare but just be prepared for it.
+* You should tune the various timeout settings provided by this module like [[#redis2_connect_timeout|redis2_connect_timeout]] and [[#redis2_read_timeout|redis2_read_timeout]].
+
+= Performance Tuning =
+
+* When you're using this module, please ensure you're using a TCP connection pool (provided by [[HttpUpstreamKeepaliveModule]]) and Redis pipelining wherever possible. These features will significantly improve performance.
+* Using multiple instance of Redis servers on your multi-core machines also help a lot due to the sequential processing nature of a single Redis server instance.
+* When you're benchmarking performance using something like <code>ab</code> or <code>http_load</code>, please ensure that your error log level is high enough (like <code>warn</code>) to prevent Nginx workers spend too much cycles on flushing the <code>error.log</code> file, which is always non-buffered and blocking and thus very expensive.
+
+= Installation =
+
+You are recommended to install this module (as well as the Nginx core and many many other goodies) via the [http://openresty.org ngx_openresty bundle]. Check out the [http://openresty.org/#Installation installation instructions] for setting up [http://openresty.org ngx_openresty].
+
+Alternatively, you can install this module manually by recompiling the standard Nginx core as follows:
+
+* Grab the nginx source code from [http://nginx.org nginx.org], for example, the version 1.0.8 (see nginx compatibility),
+* and then download the latest version of the release tarball of this module from ngx_redis2's [http://github.com/agentzh/redis2-nginx-module/tags file list].
+* and finally build the source with this module:
+<geshi lang="bash">
+ wget 'http://nginx.org/download/nginx-1.0.8.tar.gz'
+ tar -xzvf nginx-1.0.8.tar.gz
+ cd nginx-1.0.8/
+
+ # Here we assume you would install you nginx under /opt/nginx/.
+ ./configure --prefix=/opt/nginx \
+ --add-module=/path/to/redis2-nginx-module
+
+ make -j2
+ make install
+</geshi>
+
+= Compatibility =
+
+Redis 2.0, 2.2, and above should work with this module without any issues. So is the [http://code.google.com/p/alchemydatabase/ Alchemy Database] (aka redisql in its early days).
+
+The following versions of Nginx should work with this module:
+
+* 1.1.x (last tested: 1.1.5)
+* 1.0.x (last tested: 1.0.10)
+* 0.9.x (last tested: 0.9.4)
+* 0.8.x >= 0.8.31 (last tested: 0.8.54)
+
+Earlier versions of Nginx will ''not'' work.
+
+If you find that any particular version of Nginx above 0.8.31 does not work with this module, please consider reporting a bug.
+
+= Report Bugs =
+
+Although a lot of effort has been put into testing and code tuning, there must be some serious bugs lurking somewhere in this module. So whenever you are bitten by any quirks, please don't hesitate to
+
+* create a ticket on the [http://github.com/agentzh/redis2-nginx-module/issues issue tracking interface] provided by GitHub.
+* or send a bug report or even patches to the [http://mailman.nginx.org/mailman/listinfo/nginx Nginx mailing list].
+
+= Source Repository =
+
+Available on github at [http://github.com/agentzh/redis2-nginx-module agentzh/redis2-nginx-module].
+
+= TODO =
+* Add the <code>redis2_as_json</code> directive to allow emitting JSON directly.
+
+= Author =
+
+Zhang "agentzh" Yichun (章亦春) <agentzh@gmail.com>
+
+= Getting involved =
+
+You'll be very welcomed to submit patches to the author or just ask for
+a commit bit to the source repository on GitHub.
+
+= Copyright & License =
+
+This module is licenced under the BSD license.
+
+Copyright (C) 2010, 2011, by Zhang "agentzh" Yichun (章亦春) <agentzh@gmail.com>.
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+= SEE ALSO =
+* The [http://redis.io/ Redis] server homepage.
+* a redis response parser and a request constructor for Lua: [[LuaRedisParser]].
+* [[HttpLuaModule]]
+* The [http://openresty.org ngx_openresty bundle].
+
View
2  src/ngx_http_redis2_handler.c
@@ -1,4 +1,6 @@
+#ifndef DDEBUG
#define DDEBUG 0
+#endif
#include "ddebug.h"
#include "ngx_http_redis2_handler.h"
View
2  src/ngx_http_redis2_module.c
@@ -1,4 +1,6 @@
+#ifndef DDEBUG
#define DDEBUG 0
+#endif
#include "ddebug.h"
#include "ngx_http_redis2_module.h"
View
59 src/ngx_http_redis2_reply.c
@@ -1,17 +1,20 @@
#line 1 "src/ngx_http_redis2_reply.rl"
+#ifndef DDEBUG
#define DDEBUG 0
+#endif
#include "ddebug.h"
#include "ngx_http_redis2_reply.h"
#include "ngx_http_redis2_util.h"
+#include <nginx.h>
-#line 17 "src/ngx_http_redis2_reply.rl"
+#line 20 "src/ngx_http_redis2_reply.rl"
-#line 15 "src/ngx_http_redis2_reply.c"
+#line 18 "src/ngx_http_redis2_reply.c"
static const int reply_start = 1;
static const int reply_first_final = 50;
static const int reply_error = 0;
@@ -19,7 +22,7 @@ static const int reply_error = 0;
static const int reply_en_main = 1;
-#line 20 "src/ngx_http_redis2_reply.rl"
+#line 23 "src/ngx_http_redis2_reply.rl"
ngx_int_t
ngx_http_redis2_process_reply(ngx_http_redis2_ctx_t *ctx,
@@ -33,15 +36,15 @@ ngx_http_redis2_process_reply(ngx_http_redis2_ctx_t *ctx,
ngx_chain_t **ll = NULL;
int cs;
- char *p;
- char *orig_p;
+ signed char *p;
+ signed char *orig_p;
ssize_t orig_len;
- char *pe;
+ signed char *pe;
u = ctx->request->upstream;
b = &u->buffer;
- orig_p = (char *) b->last;
+ orig_p = (signed char *) b->last;
orig_len = bytes;
while (ctx->query_count) {
@@ -51,12 +54,12 @@ ngx_http_redis2_process_reply(ngx_http_redis2_ctx_t *ctx,
dd("init the state machine");
-#line 55 "src/ngx_http_redis2_reply.c"
+#line 58 "src/ngx_http_redis2_reply.c"
{
cs = reply_start;
}
-#line 51 "src/ngx_http_redis2_reply.rl"
+#line 54 "src/ngx_http_redis2_reply.rl"
ctx->state = cs;
@@ -65,13 +68,13 @@ ngx_http_redis2_process_reply(ngx_http_redis2_ctx_t *ctx,
dd("resumed the old state %d", cs);
}
- p = (char *) b->last;
- pe = (char *) b->last + bytes;
+ p = (signed char *) b->last;
+ pe = (signed char *) b->last + bytes;
dd("response body: %.*s", (int) bytes, p);
-#line 75 "src/ngx_http_redis2_reply.c"
+#line 78 "src/ngx_http_redis2_reply.c"
{
short _widec;
if ( p == pe )
@@ -159,7 +162,7 @@ case 5:
if ( ++p == pe )
goto _test_eof50;
case 50:
-#line 163 "src/ngx_http_redis2_reply.c"
+#line 166 "src/ngx_http_redis2_reply.c"
goto st0;
st6:
if ( ++p == pe )
@@ -210,7 +213,7 @@ case 8:
if ( ++p == pe )
goto _test_eof9;
case 9:
-#line 214 "src/ngx_http_redis2_reply.c"
+#line 217 "src/ngx_http_redis2_reply.c"
if ( (*p) == 13 )
goto st10;
if ( 48 <= (*p) && (*p) <= 57 )
@@ -388,7 +391,7 @@ case 13:
if ( ++p == pe )
goto _test_eof51;
case 51:
-#line 392 "src/ngx_http_redis2_reply.c"
+#line 395 "src/ngx_http_redis2_reply.c"
_widec = (*p);
if ( (*p) < 13 ) {
if ( (*p) <= 12 ) {
@@ -499,7 +502,7 @@ case 16:
if ( ++p == pe )
goto _test_eof17;
case 17:
-#line 503 "src/ngx_http_redis2_reply.c"
+#line 506 "src/ngx_http_redis2_reply.c"
if ( (*p) == 13 )
goto st18;
if ( 48 <= (*p) && (*p) <= 57 )
@@ -523,7 +526,7 @@ case 18:
if ( ++p == pe )
goto _test_eof19;
case 19:
-#line 527 "src/ngx_http_redis2_reply.c"
+#line 530 "src/ngx_http_redis2_reply.c"
_widec = (*p);
if ( (*p) < 43 ) {
if ( 36 <= (*p) && (*p) <= 36 ) {
@@ -729,7 +732,7 @@ case 23:
if ( ++p == pe )
goto _test_eof52;
case 52:
-#line 733 "src/ngx_http_redis2_reply.c"
+#line 736 "src/ngx_http_redis2_reply.c"
_widec = (*p);
if ( (*p) < 43 ) {
if ( 36 <= (*p) && (*p) <= 36 ) {
@@ -1014,7 +1017,7 @@ case 28:
if ( ++p == pe )
goto _test_eof29;
case 29:
-#line 1018 "src/ngx_http_redis2_reply.c"
+#line 1021 "src/ngx_http_redis2_reply.c"
_widec = (*p);
if ( (*p) > 13 ) {
if ( 48 <= (*p) && (*p) <= 57 ) {
@@ -1342,7 +1345,7 @@ case 33:
if ( ++p == pe )
goto _test_eof53;
case 53:
-#line 1346 "src/ngx_http_redis2_reply.c"
+#line 1349 "src/ngx_http_redis2_reply.c"
_widec = (*p);
if ( (*p) < 43 ) {
if ( (*p) < 14 ) {
@@ -2525,7 +2528,7 @@ case 40:
if ( ++p == pe )
goto _test_eof54;
case 54:
-#line 2529 "src/ngx_http_redis2_reply.c"
+#line 2532 "src/ngx_http_redis2_reply.c"
_widec = (*p);
if ( (*p) < 43 ) {
if ( (*p) > 13 ) {
@@ -2757,7 +2760,7 @@ case 41:
if ( ++p == pe )
goto _test_eof55;
case 55:
-#line 2761 "src/ngx_http_redis2_reply.c"
+#line 2764 "src/ngx_http_redis2_reply.c"
_widec = (*p);
if ( (*p) < 43 ) {
if ( (*p) < 14 ) {
@@ -3298,7 +3301,7 @@ case 43:
if ( ++p == pe )
goto _test_eof44;
case 44:
-#line 3302 "src/ngx_http_redis2_reply.c"
+#line 3305 "src/ngx_http_redis2_reply.c"
_widec = (*p);
if ( (*p) < 14 ) {
if ( (*p) > 12 ) {
@@ -3617,7 +3620,7 @@ case 46:
if ( ++p == pe )
goto _test_eof56;
case 56:
-#line 3621 "src/ngx_http_redis2_reply.c"
+#line 3624 "src/ngx_http_redis2_reply.c"
_widec = (*p);
if ( (*p) < 44 ) {
if ( (*p) < 36 ) {
@@ -4043,7 +4046,7 @@ case 49:
_out: {}
}
-#line 65 "src/ngx_http_redis2_reply.rl"
+#line 68 "src/ngx_http_redis2_reply.rl"
dd("state after exec: %d, done: %d, %.*s", cs, (int) done,
(int) (bytes - ((u_char *) p - b->last)), p);
@@ -4124,9 +4127,15 @@ case 49:
return NGX_HTTP_INTERNAL_SERVER_ERROR;
#endif
+
+ } else {
+#if defined(nginx_version) && nginx_version >= 1001004
+ u->keepalive = 1;
+#endif
}
u->length = 0;
+
break;
} else {
View
21 src/ngx_http_redis2_reply.rl
@@ -1,8 +1,11 @@
+#ifndef DDEBUG
#define DDEBUG 0
+#endif
#include "ddebug.h"
#include "ngx_http_redis2_reply.h"
#include "ngx_http_redis2_util.h"
+#include <nginx.h>
%%{
machine reply;
@@ -30,15 +33,15 @@ ngx_http_redis2_process_reply(ngx_http_redis2_ctx_t *ctx,
ngx_chain_t **ll = NULL;
int cs;
- char *p;
- char *orig_p;
+ signed char *p;
+ signed char *orig_p;
ssize_t orig_len;
- char *pe;
+ signed char *pe;
u = ctx->request->upstream;
b = &u->buffer;
- orig_p = (char *) b->last;
+ orig_p = (signed char *) b->last;
orig_len = bytes;
while (ctx->query_count) {
@@ -56,8 +59,8 @@ ngx_http_redis2_process_reply(ngx_http_redis2_ctx_t *ctx,
dd("resumed the old state %d", cs);
}
- p = (char *) b->last;
- pe = (char *) b->last + bytes;
+ p = (signed char *) b->last;
+ pe = (signed char *) b->last + bytes;
dd("response body: %.*s", (int) bytes, p);
@@ -142,9 +145,15 @@ ngx_http_redis2_process_reply(ngx_http_redis2_ctx_t *ctx,
return NGX_HTTP_INTERNAL_SERVER_ERROR;
#endif
+
+ } else {
+#if defined(nginx_version) && nginx_version >= 1001004
+ u->keepalive = 1;
+#endif
}
u->length = 0;
+
break;
} else {
View
2  src/ngx_http_redis2_util.c
@@ -1,4 +1,6 @@
+#ifndef DDEBUG
#define DDEBUG 0
+#endif
#include "ddebug.h"
#include "ngx_http_redis2_util.h"
View
6 t/sanity.t
@@ -14,7 +14,7 @@ $ENV{TEST_NGINX_REDIS_PORT} ||= 6379;
#no_diff;
-log_level 'warn';
+#log_level 'warn';
run_tests();
@@ -323,7 +323,7 @@ __DATA__
--- http_config
upstream backend {
server 127.0.0.1:$TEST_NGINX_REDIS_PORT;
- keepalive 500 single;
+ keepalive 1 single;
}
--- config
location /a {
@@ -349,7 +349,7 @@ __DATA__
--- http_config
upstream backend {
server 127.0.0.1:$TEST_NGINX_REDIS_PORT;
- keepalive 500 single;
+ keepalive 1 single;
}
--- config
location /a {
View
39 util/build.sh
@@ -5,38 +5,14 @@
ragel -I src -G2 src/ngx_http_redis2_reply.rl || exit 1
root=`pwd`
-#cd ~/work
version=$1
-#opts=$2
home=~
-target=$root/work/nginx
+force=$2
-if [ ! -d ./buildroot ]; then
- mkdir ./buildroot || exit 1
-fi
-
-cd buildroot || exit 1
-
-if [ ! -s "nginx-$version.tar.gz" ]; then
- if [ -f ~/work/nginx-$version.tar.gz ]; then
- cp ~/work/nginx-$version.tar.gz ./ || exit 1
- else
- wget "http://sysoev.ru/nginx/nginx-$version.tar.gz" -O nginx-$version.tar.gz || exit 1
- fi
-
- tar -xzvf nginx-$version.tar.gz || exit 1
- cp $root/../no-pool-nginx/nginx-$version-no_pool.patch ./ || exit 1
- patch -p0 < nginx-$version-no_pool.patch || exit 1
-fi
-
-#patch -p0 < ~/work/nginx-$version-rewrite_phase_fix.patch || exit 1
-
-cd nginx-$version/ || exit 1
-
-if [[ "$BUILD_CLEAN" -eq 1 || ! -f Makefile || "$root/config" -nt Makefile || "$root/util/build.sh" -nt Makefile ]]; then
#--with-cc-opt="-O3" \
#--with-cc-opt="-fast" \
- ./configure --prefix=$target \
+ngx-build $force $version \
+ --with-cc-opt="-O3 -funsigned-char" \
--with-http_addition_module \
--add-module=$root $opts \
--add-module=$root/../eval-nginx-module \
@@ -50,12 +26,3 @@ if [[ "$BUILD_CLEAN" -eq 1 || ! -f Makefile || "$root/config" -nt Makefile || "$
#--add-module=$home/work/nginx/nginx_upstream_hash-0.3 \
#--without-http_ssi_module # we cannot disable ssi because echo_location_async depends on it (i dunno why?!)
-fi
-if [ -f $target/sbin/nginx ]; then
- rm -f $target/sbin/nginx
-fi
-if [ -f $target/logs/nginx.pid ]; then
- kill `cat $target/logs/nginx.pid`
-fi
-make -j3 && make install
-
View
10 valgrind.suppress
@@ -110,5 +110,13 @@
fun:ngx_init_cycle
fun:main
}
-
+{
+ <insert_a_suppression_name_here>
+ Memcheck:Leak
+ fun:malloc
+ fun:ngx_alloc
+ fun:ngx_create_pool
+ fun:ngx_http_upstream_connect
+ fun:ngx_http_upstream_init_request
+}
Please sign in to comment.
Something went wrong with that request. Please try again.