Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ngx_tcp_upstream_keepalive_module #81

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d3a4ad0
add keepalive file, not available currently
ideal Oct 16, 2013
acedd90
add file
ideal Oct 17, 2013
b28f336
modify gitignore
ideal Oct 17, 2013
43c120b
change tcp
ideal Oct 17, 2013
8f8cf80
change tcp
ideal Oct 17, 2013
5f76056
囧,少了个分号
ideal Oct 17, 2013
a0ac70e
ngx_tcp_upstream_keepalive_create_conf should be at create_srv_conf
ideal Oct 17, 2013
eabc02c
add module to config
ideal Oct 17, 2013
fcfa127
remove eof
ideal Oct 17, 2013
213c7a0
set upstream to keepalive in ngx_tcp_upstream_keepalive.c
ideal Oct 17, 2013
382fa34
do not close connection if keepalive
ideal Oct 17, 2013
f9ba862
ugly ignore quit
ideal Oct 18, 2013
fbef10f
change error handling when connecting to a tcp upstream
ideal Oct 21, 2013
f924ffd
add a server_type directive to specify the upstream type
ideal Oct 21, 2013
cb90006
add document about ngx_tcp_upstream_keepalive_module
ideal Nov 1, 2013
b66dd18
add monitor server module
ideal Nov 15, 2013
f9c0bcd
add header definition
ideal Nov 18, 2013
87f4cb6
update handler
ideal Nov 22, 2013
a22dc02
add header and request_body for each connection
ideal Nov 25, 2013
cc8cdfb
read client data
ideal Nov 25, 2013
b321957
read client data
ideal Nov 25, 2013
2484a6d
finalize session if client data is not correct
ideal Nov 26, 2013
ef41976
add query for redis
ideal Nov 27, 2013
20e480f
add query for redis
ideal Nov 27, 2013
13d8d72
add query for redis
ideal Nov 27, 2013
d8aaac7
init upstream
ideal Nov 27, 2013
e9061bd
add query for redis
ideal Nov 28, 2013
1ec7f22
change redis query protocol
ideal Nov 28, 2013
9b08725
change ngx_get_num_size() to inline
ideal Nov 28, 2013
b473b47
it works, but need to reconstruct
ideal Nov 29, 2013
32bc82c
it's better add __attribute__ ((packed))
ideal Dec 11, 2013
b823978
add rpc server module
ideal Dec 15, 2013
2e8f848
remove end blank
ideal Dec 15, 2013
6d14242
init first_read
ideal Dec 20, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
Makefile
cscope*
objs
tags
24 changes: 24 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,30 @@ Directives
(<http://wiki.nginx.org/NginxHttpUpstreamModule#server>). Default port
is 80.

server_type
syntax: *server_type parameter*

default: *none*

context: *upstream*

description: Specify the server type, currently only "redis" is
supported.

keepalive
syntax: *keepalive num [single]*

default: *none*

context: *upstream*

description: Enables keep-alive connections for the upstream. Num
specifies the max number of connections to keep open before, if
the max is reached it will close the least recently used connections.

Single treats everything as a single host. With this flag connections
to different backends are treated as equal.

check
syntax: *check interval=milliseconds [fall=count] [rise=count]
[timeout=milliseconds] [type=tcp|ssl_hello|smtp|mysql|pop3|imap]*
Expand Down
6 changes: 3 additions & 3 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ ngx_feature_name=
ngx_feature_run=no
ngx_feature_incs=
ngx_feature_path="$ngx_addon_dir/modules $ngx_addon_dir/parsers $ngx_addon_dir"
ngx_feature_deps="$ngx_addon_dir/ngx_tcp.h $ngx_addon_dir/ngx_tcp_session.h $ngx_addon_dir/ngx_tcp_upstream.h $ngx_addon_dir/ngx_tcp_upstream_check.h $ngx_addon_dir/ngx_tcp_upstream_round_robin.h"
ngx_tcp_src="$ngx_addon_dir/ngx_tcp.c $ngx_addon_dir/ngx_tcp_core_module.c $ngx_addon_dir/ngx_tcp_session.c $ngx_addon_dir/ngx_tcp_access.c $ngx_addon_dir/ngx_tcp_log.c $ngx_addon_dir/ngx_tcp_upstream.c $ngx_addon_dir/ngx_tcp_upstream_round_robin.c $ngx_addon_dir/modules/ngx_tcp_generic_proxy_module.c $ngx_addon_dir/modules/ngx_tcp_websocket_proxy_module.c $ngx_addon_dir/modules/ngx_tcp_upstream_ip_hash_module.c $ngx_addon_dir/modules/ngx_tcp_upstream_busyness_module.c $ngx_addon_dir/ngx_tcp_upstream_check.c "
ngx_feature_deps="$ngx_addon_dir/ngx_tcp.h $ngx_addon_dir/ngx_tcp_session.h $ngx_addon_dir/ngx_tcp_upstream.h $ngx_addon_dir/ngx_tcp_upstream_check.h $ngx_addon_dir/ngx_tcp_upstream_round_robin.h $ngx_addon_dir/ngx_tcp_upstream_keepalive.h"
ngx_tcp_src="$ngx_addon_dir/ngx_tcp.c $ngx_addon_dir/ngx_tcp_core_module.c $ngx_addon_dir/ngx_tcp_session.c $ngx_addon_dir/ngx_tcp_access.c $ngx_addon_dir/ngx_tcp_log.c $ngx_addon_dir/ngx_tcp_upstream.c $ngx_addon_dir/ngx_tcp_upstream_round_robin.c $ngx_addon_dir/modules/ngx_tcp_generic_proxy_module.c $ngx_addon_dir/modules/ngx_tcp_websocket_proxy_module.c $ngx_addon_dir/modules/ngx_tcp_upstream_ip_hash_module.c $ngx_addon_dir/modules/ngx_tcp_upstream_busyness_module.c $ngx_addon_dir/ngx_tcp_upstream_check.c $ngx_addon_dir/ngx_tcp_upstream_keepalive.c $ngx_addon_dir/modules/ngx_tcp_monitor_server_module.c $ngx_addon_dir/modules/ngx_tcp_rpc_server_module.c"
ngx_tcp_ssl_deps="$ngx_addon_dir/modules/ngx_tcp_ssl_module.h"
ngx_tcp_ssl_src="$ngx_addon_dir/modules/ngx_tcp_ssl_module.c"
ngx_tcp_parser_deps="$ngx_addon_dir/parsers/parser.h $ngx_addon_dir/parsers/http_request_parser.h $ngx_addon_dir/parsers/http_response_parser.h $ngx_addon_dir/parsers/smtp_response_parser.h"
Expand All @@ -17,7 +17,7 @@ if [ $ngx_found = yes ]; then
ngx_addon_name=ngx_tcp_module

TCP_CORE_MODULES="ngx_tcp_module ngx_tcp_core_module ngx_tcp_upstream_module"
TCP_MODULES="ngx_tcp_proxy_module ngx_tcp_websocket_module ngx_tcp_upstream_ip_hash_module ngx_tcp_upstream_busyness_module"
TCP_MODULES="ngx_tcp_proxy_module ngx_tcp_websocket_module ngx_tcp_upstream_ip_hash_module ngx_tcp_upstream_busyness_module ngx_tcp_upstream_keepalive_module ngx_tcp_monitor_module ngx_tcp_rpc_module"

NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_feature_deps $ngx_tcp_parser_deps"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_tcp_src $ngx_tcp_parser_src"
Expand Down
66 changes: 52 additions & 14 deletions modules/ngx_tcp_generic_proxy_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ static void *ngx_tcp_proxy_create_conf(ngx_conf_t *cf);
static char *ngx_tcp_proxy_merge_conf(ngx_conf_t *cf, void *parent,
void *child);

static ngx_keyval_t ngx_tcp_server_types[] = {

{ ngx_string("redis"),
ngx_string("quit")
},

{ ngx_null_string,
ngx_null_string
}
};

static ngx_tcp_protocol_t ngx_tcp_generic_protocol = {

Expand Down Expand Up @@ -301,18 +311,19 @@ ngx_tcp_upstream_init_proxy_handler(ngx_tcp_session_t *s, ngx_tcp_upstream_t *u)
static void
ngx_tcp_proxy_handler(ngx_event_t *ev)
{
char *action, *recv_action, *send_action;
off_t *read_bytes, *write_bytes;
size_t size;
ssize_t n;
ngx_buf_t *b;
ngx_err_t err;
ngx_uint_t do_write, first_read;
ngx_connection_t *c, *src, *dst;
ngx_tcp_session_t *s;
ngx_tcp_proxy_conf_t *pcf;
ngx_tcp_proxy_ctx_t *pctx;
ngx_tcp_core_srv_conf_t *cscf;
char *action, *recv_action, *send_action;
off_t *read_bytes, *write_bytes;
size_t size;
ssize_t n;
ngx_buf_t *b;
ngx_err_t err;
ngx_uint_t do_write, first_read, i;
ngx_connection_t *c, *src, *dst;
ngx_tcp_session_t *s;
ngx_tcp_proxy_conf_t *pcf;
ngx_tcp_proxy_ctx_t *pctx;
ngx_tcp_core_srv_conf_t *cscf;
ngx_tcp_upstream_srv_conf_t *uscf;

c = ev->data;
s = c->data;
Expand Down Expand Up @@ -380,6 +391,8 @@ ngx_tcp_proxy_handler(ngx_event_t *ev)
/* SSL Need this */
if (s->connection->ssl) {
first_read = 1;
} else {
first_read = 0;
}
#else
first_read = 0;
Expand All @@ -389,6 +402,9 @@ ngx_tcp_proxy_handler(ngx_event_t *ev)
"tcp proxy handler: %d, #%d > #%d, time:%ui",
do_write, src->fd, dst->fd, ngx_current_msec);

pcf = ngx_tcp_get_module_srv_conf(s, ngx_tcp_proxy_module);
uscf = pcf->upstream.upstream;

for ( ;; ) {

if (do_write) {
Expand All @@ -398,6 +414,30 @@ ngx_tcp_proxy_handler(ngx_event_t *ev)
if (size && dst->write->ready) {
c->log->action = send_action;

if (uscf->server_type.data) {
for (i = 0; ngx_tcp_server_types[i].key.data; i++) {
if (ngx_strncmp(uscf->server_type.data,
ngx_tcp_server_types[i].key.data,
ngx_min(uscf->server_type.len,
ngx_tcp_server_types[i].key.len)) == 0) {
break;
}

}
if (ngx_tcp_server_types[i].value.data
&& ngx_strncasecmp(b->pos,
ngx_tcp_server_types[i].value.data,
ngx_tcp_server_types[i].value.len) == 0) {
ngx_log_debug0(NGX_LOG_DEBUG_TCP, ev->log, 0,
"received quit, close session");

ngx_tcp_finalize_session(s);
return;
}
} else {
s->upstream->keepalive = 0;
}

n = dst->send(dst, b->pos, size);
err = ngx_socket_errno;

Expand Down Expand Up @@ -501,8 +541,6 @@ ngx_tcp_proxy_handler(ngx_event_t *ev)
return;
}

pcf = ngx_tcp_get_module_srv_conf(s, ngx_tcp_proxy_module);

if (c == s->connection) {
ngx_add_timer(c->read, cscf->timeout);
}
Expand Down
Loading