Skip to content

Commit

Permalink
add nginx thread pool usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigzagAK committed Apr 26, 2019
1 parent eda641e commit 8fc6582
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 188 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Add servers directly into `upstream` section is incorrect and you will see fake

## dns_update

|Syntax |dns_update 60s|
|Syntax |dns_update 60s [thread pool]|
|-------|----------------|
|Default|-|
|Context|upstream|
Expand Down
28 changes: 10 additions & 18 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ build_deps=0

DIR="$(pwd)"

VERSION="1.13.6"
PCRE_VERSION="8.39"
LUAJIT_VERSION="2.1.0-beta2"
VERSION="1.15.6"
PCRE_VERSION="8.40"
ZLIB_VERSION="1.2.11"

SUFFIX=""
Expand Down Expand Up @@ -48,7 +47,7 @@ fi

function build_luajit() {
echo "Build luajit"
cd LuaJIT-$LUAJIT_VERSION
cd luajit2
make -j1 > /dev/null
r=$?
if [ $r -ne 0 ]; then
Expand Down Expand Up @@ -76,6 +75,7 @@ function build_debug() {
$EMBEDDED_OPTS \
--with-stream \
--with-debug \
--with-threads \
--with-cc-opt="-O0" \
--add-module=../ngx_devel_kit \
--add-module=../lua-nginx-module \
Expand Down Expand Up @@ -107,6 +107,7 @@ function build_release() {
./configure --prefix="$INSTALL_PREFIX/nginx-$VERSION$SUFFIX" \
$EMBEDDED_OPTS \
--with-stream \
--with-threads \
--add-module=../ngx_devel_kit \
--add-module=../lua-nginx-module \
--add-module=../stream-lua-nginx-module \
Expand Down Expand Up @@ -163,15 +164,6 @@ function download_nginx() {
fi
}

function download_luajit() {
if [ $download -eq 1 ] || [ ! -e LuaJIT-$LUAJIT_VERSION.tar.gz ]; then
echo "Download LuaJIT-$LUAJIT_VERSION"
curl -s -L -O http://luajit.org/download/LuaJIT-$LUAJIT_VERSION.tar.gz
else
echo "Get LuaJIT-$LUAJIT_VERSION.tar.gz"
fi
}

function download_pcre() {
if [ $download -eq 1 ] || [ ! -e pcre-$PCRE_VERSION.tar.gz ]; then
echo "Download PCRE-$PCRE_VERSION"
Expand Down Expand Up @@ -211,14 +203,14 @@ function download() {

cd download

download_luajit
download_pcre
download_nginx

download_module openresty stream-lua-nginx-module v0.0.6
download_module openresty lua-nginx-module v0.10.14
download_module openresty luajit2 v2.1-agentzh
download_module ZigzagAK ngx_dynamic_upstream master
download_module openresty stream-lua-nginx-module master
download_module simpl ngx_devel_kit master
download_module openresty lua-nginx-module master
download_module openresty lua-cjson master

download_dep http://zlib.net zlib $ZLIB_VERSION tar.gz
Expand Down Expand Up @@ -252,8 +244,8 @@ function build() {

build_cJSON

make clean > /dev/null 2>&1
build_debug
# make clean > /dev/null 2>&1
# build_debug

make clean > /dev/null 2>&1
build_release
Expand Down
88 changes: 32 additions & 56 deletions src/ngx_dynamic_upstream_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ extern "C" {

template <class S> static ngx_int_t
ngx_dynamic_upstream_op_add(typename TypeSelect<S>::peers_type *primary,
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool, ngx_log_t *log);
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool,
ngx_pool_t *temp_pool, ngx_log_t *log);


template <class S> static ngx_int_t
ngx_dynamic_upstream_op_sync(typename TypeSelect<S>::peers_type *primary,
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool, ngx_log_t *log);
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool,
ngx_pool_t *temp_pool, ngx_log_t *log);


template <class S> static ngx_int_t
ngx_dynamic_upstream_op_del(typename TypeSelect<S>::peers_type *primary,
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool, ngx_log_t *log);
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool,
ngx_pool_t *temp_pool, ngx_log_t *log);


template <class S> static ngx_int_t
Expand Down Expand Up @@ -61,7 +64,7 @@ ngx_shm_calloc(ngx_slab_pool_t *shpool, size_t size = 0)

ngx_int_t
ngx_dynamic_upstream_op_impl(ngx_log_t *log, ngx_dynamic_upstream_op_t *op,
ngx_slab_pool_t *shpool, void *peers)
ngx_slab_pool_t *shpool, ngx_pool_t *temp_pool, void *peers)
{
ngx_int_t rc = NGX_OK;

Expand All @@ -71,15 +74,18 @@ ngx_dynamic_upstream_op_impl(ngx_log_t *log, ngx_dynamic_upstream_op_t *op,
switch (op->op) {

case NGX_DYNAMIC_UPSTEAM_OP_ADD:
rc = CALL(ngx_dynamic_upstream_op_add, peers, op, shpool, log);
rc = CALL(ngx_dynamic_upstream_op_add, peers, op, shpool,
temp_pool, log);
break;

case NGX_DYNAMIC_UPSTEAM_OP_REMOVE:
rc = CALL(ngx_dynamic_upstream_op_del, peers, op, shpool, log);
rc = CALL(ngx_dynamic_upstream_op_del, peers, op, shpool,
temp_pool, log);
break;

case NGX_DYNAMIC_UPSTEAM_OP_SYNC:
rc = CALL(ngx_dynamic_upstream_op_sync, peers, op, shpool, log);
rc = CALL(ngx_dynamic_upstream_op_sync, peers, op, shpool,
temp_pool, log);
break;

case NGX_DYNAMIC_UPSTEAM_OP_PARAM:
Expand Down Expand Up @@ -365,7 +371,8 @@ ngx_dynamic_upstream_op_add_peer(ngx_log_t *log,
template <class S> static ngx_int_t
ngx_dynamic_upstream_op_add_impl(ngx_log_t *log,
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool,
typename TypeSelect<S>::peers_type *primary, ngx_url_t *u)
ngx_pool_t *temp_pool, typename TypeSelect<S>::peers_type *primary,
ngx_url_t *u)
{
unsigned j;
unsigned count = 0;
Expand Down Expand Up @@ -398,7 +405,8 @@ ngx_dynamic_upstream_op_add_impl(ngx_log_t *log,
del_op.server = noaddr;
del_op.name = noaddr;

ngx_dynamic_upstream_op_del<S>(primary, &del_op, shpool, log);
ngx_dynamic_upstream_op_del<S>(primary, &del_op, shpool,
temp_pool, log);
}

op->status = count != 0 ? NGX_HTTP_OK : NGX_HTTP_NOT_MODIFIED;
Expand All @@ -407,39 +415,15 @@ ngx_dynamic_upstream_op_add_impl(ngx_log_t *log,
}


struct ngx_pool_auto {
ngx_pool_t *pool;

ngx_pool_auto(ngx_log_t *log)
: pool(ngx_create_pool(ngx_pagesize, log))
{}

~ngx_pool_auto()
{
if (pool != NULL)
ngx_destroy_pool(pool);
}
};


template <class S> static ngx_int_t
ngx_dynamic_upstream_op_add(typename TypeSelect<S>::peers_type *primary,
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool, ngx_log_t *log)
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool,
ngx_pool_t *temp_pool, ngx_log_t *log)
{
ngx_url_t u;
ngx_int_t rc;

ngx_pool_auto guard(log);

if (guard.pool == NULL) {

op->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
op->err = "no memory";

return NGX_ERROR;
}

if ((rc = ngx_dynamic_upstream_parse_url(&u, guard.pool, op)) == NGX_ERROR)
if ((rc = ngx_dynamic_upstream_parse_url(&u, temp_pool, op)) == NGX_ERROR)
return NGX_ERROR;

if (rc == NGX_AGAIN) {
Expand All @@ -459,8 +443,8 @@ ngx_dynamic_upstream_op_add(typename TypeSelect<S>::peers_type *primary,
}
}

if (ngx_dynamic_upstream_op_add_impl<S>(log, op, shpool, primary, &u)
== NGX_ERROR)
if (ngx_dynamic_upstream_op_add_impl<S>(log, op, shpool, temp_pool,
primary, &u) == NGX_ERROR)
return NGX_ERROR;

if (op->status == NGX_HTTP_NOT_MODIFIED)
Expand Down Expand Up @@ -624,7 +608,8 @@ ngx_dynamic_upstream_op_hash(typename TypeSelect<S>::peers_type *primary,

template <class S> static ngx_int_t
ngx_dynamic_upstream_op_sync(typename TypeSelect<S>::peers_type *primary,
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool, ngx_log_t *log)
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool,
ngx_pool_t *temp_pool, ngx_log_t *log)
{
typename TypeSelect<S>::peers_type *peers;
typename TypeSelect<S>::peer_type *peer;
Expand All @@ -644,17 +629,7 @@ ngx_dynamic_upstream_op_sync(typename TypeSelect<S>::peers_type *primary,

op->hash = hash;

ngx_pool_auto guard(log);

if (guard.pool == NULL) {

op->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
op->err = "no memory";

return NGX_ERROR;
}

servers = ngx_array_create(guard.pool, 100, sizeof(ngx_server_t));
servers = ngx_array_create(temp_pool, 100, sizeof(ngx_server_t));
if (servers == NULL) {

op->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
Expand All @@ -667,7 +642,7 @@ ngx_dynamic_upstream_op_sync(typename TypeSelect<S>::peers_type *primary,

if (ngx_dynamic_upstream_op_servers<S>(primary,
servers,
guard.pool,
temp_pool,
&hash)
== NGX_ERROR)
{
Expand All @@ -682,7 +657,7 @@ ngx_dynamic_upstream_op_sync(typename TypeSelect<S>::peers_type *primary,

op->server = server[j].name;

if (ngx_dynamic_upstream_parse_url(&server[j].u, guard.pool,
if (ngx_dynamic_upstream_parse_url(&server[j].u, temp_pool,
op) != NGX_OK) {

ngx_log_error(NGX_LOG_WARN, log, 0, "%V: server %V: %s",
Expand Down Expand Up @@ -750,8 +725,8 @@ ngx_dynamic_upstream_op_sync(typename TypeSelect<S>::peers_type *primary,
op->server = peer->server;
op->name = peer->name;

if (ngx_dynamic_upstream_op_del<S>(primary, op, shpool, log)
== NGX_ERROR)
if (ngx_dynamic_upstream_op_del<S>(primary, op, shpool,
temp_pool, log) == NGX_ERROR)
return NGX_ERROR;

count++;
Expand Down Expand Up @@ -909,7 +884,8 @@ ngx_dynamic_upstream_op_free_peer(ngx_slab_pool_t *shpool, PeerT *peer)

template <class S> static ngx_int_t
ngx_dynamic_upstream_op_del(typename TypeSelect<S>::peers_type *primary,
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool, ngx_log_t *log)
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool,
ngx_pool_t *temp_pool, ngx_log_t *log)
{
typename TypeSelect<S>::peers_type *peers, *backup = primary->next;
typename TypeSelect<S>::peer_type *peer, *deleted, *prev;
Expand Down Expand Up @@ -960,7 +936,7 @@ ngx_dynamic_upstream_op_del(typename TypeSelect<S>::peers_type *primary,
add_op.down = 1;

if (ngx_dynamic_upstream_op_add<S>(primary, &add_op,
shpool, log) != NGX_OK) {
shpool, temp_pool, log) != NGX_OK) {

op->err = add_op.err;
op->status = add_op.status;
Expand Down
2 changes: 1 addition & 1 deletion src/ngx_dynamic_upstream_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ struct TypeSelect<ngx_stream_upstream_srv_conf_t> {

ngx_int_t ngx_dynamic_upstream_op_impl(ngx_log_t *log,
ngx_dynamic_upstream_op_t *op, ngx_slab_pool_t *shpool,
void *peers);
ngx_pool_t *temp_pool, void *peers);

ngx_inline ngx_flag_t
str_eq(ngx_str_t s1, ngx_str_t s2)
Expand Down
Loading

0 comments on commit 8fc6582

Please sign in to comment.