Showing preview only (319K chars total). Download the full file or copy to clipboard to get everything.
Repository: openresty/drizzle-nginx-module
Branch: master
Commit: 1312cf99b711
Files: 47
Total size: 304.0 KB
Directory structure:
gitextract_0fqk8n_f/
├── .gitattributes
├── .github/
│ └── workflows/
│ └── coverity.yml
├── .gitignore
├── .travis.yml
├── Changes
├── README.markdown
├── config
├── doc/
│ └── design
├── src/
│ ├── ddebug.h
│ ├── ngx_http_drizzle_checker.c
│ ├── ngx_http_drizzle_checker.h
│ ├── ngx_http_drizzle_handler.c
│ ├── ngx_http_drizzle_handler.h
│ ├── ngx_http_drizzle_keepalive.c
│ ├── ngx_http_drizzle_keepalive.h
│ ├── ngx_http_drizzle_module.c
│ ├── ngx_http_drizzle_module.h
│ ├── ngx_http_drizzle_output.c
│ ├── ngx_http_drizzle_output.h
│ ├── ngx_http_drizzle_processor.c
│ ├── ngx_http_drizzle_processor.h
│ ├── ngx_http_drizzle_quoting.c
│ ├── ngx_http_drizzle_quoting.h
│ ├── ngx_http_drizzle_upstream.c
│ ├── ngx_http_drizzle_upstream.h
│ ├── ngx_http_drizzle_util.c
│ ├── ngx_http_drizzle_util.h
│ └── resty_dbd_stream.h
├── t/
│ ├── 000_init.t
│ ├── bugs.t
│ ├── charset/
│ │ ├── 000_init.t
│ │ ├── errors.t
│ │ ├── keepalive.t
│ │ ├── methods.t
│ │ ├── sanity.t
│ │ └── timeout.t
│ ├── errors.t
│ ├── keepalive.t
│ ├── methods.t
│ ├── sanity.t
│ ├── status.t
│ └── timeout.t
├── util/
│ ├── build.sh
│ ├── ngx-links
│ ├── update-readme.sh
│ └── wiki2pod.pl
└── valgrind.suppress
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitattributes
================================================
*.t linguist-language=Text
================================================
FILE: .github/workflows/coverity.yml
================================================
name: Coverity
on:
schedule:
- cron: "0 0 * * *"
jobs:
scan:
runs-on: ubuntu-18.04
if: ${{ github.repository_owner == 'openresty' }}
env:
COVERITY_SCAN_PROJECT_NAME: 'drizzle-nginx-module'
COVERITY_SCAN_BRANCH_PATTERN: '*'
COVERITY_SCAN_NOTIFICATION_EMAIL: 'chipitsine@gmail.com'
LUAJIT_PREFIX: '/opt/luajit21'
LUAJIT_LIB: '/opt/luajit21/lib'
LUAJIT_INC: '/opt/luajit21/include/luajit-2.1'
LUA_INCLUDE_DIR: '/opt/luajit21/include/luajit-2.1'
LUA_CMODULE_DIR: '/lib'
JOBS: 3
NGX_BUILD_JOBS: 3
NGINX_VERSION: 1.19.9
CC: gcc
steps:
- uses: actions/checkout@v3
- name: Install apt dependencies
run: |
sudo apt-get update
sudo apt-get install -y axel libgd-dev
- name: clone OpenResty satellites
run: |
git clone https://github.com/openresty/nginx-devel-utils.git
git clone https://github.com/openresty/openresty.git ../openresty
git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache
git clone https://github.com/openresty/nginx-eval-module.git ../eval-nginx-module
git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
git clone https://github.com/openresty/set-misc-nginx-module.git ../set-misc-nginx-module
git clone https://github.com/openresty/headers-more-nginx-module.git ../headers-more-nginx-module
git clone https://github.com/openresty/rds-json-nginx-module.git ../rds-json-nginx-module
git clone https://github.com/openresty/openresty.git ../ngx_openresty
- name: Install libdrizzle
run: |
wget http://openresty.org/download/drizzle7-2011.07.21.tar.gz
tar xzf drizzle7-2011.07.21.tar.gz && cd drizzle7-2011.07.21
./configure --prefix=/usr --without-server
sudo PATH=$PATH make libdrizzle-1.0 install-libdrizzle-1.0
- name: Install luajit2
run: |
git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git
cd luajit2
make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT'
sudo make install PREFIX=$LUAJIT_PREFIX
- name: Run Coverity Scan
env:
COVERITY_SCAN_TOKEN: ${{ secrets.COVERITY_SCAN_TOKEN }}
run: |
export COVERITY_SCAN_BUILD_COMMAND="sh util/build.sh $NGINX_VERSION"
export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:$PATH
export NGX_BUILD_CC=gcc
curl -fsSL "https://scan.coverity.com/scripts/travisci_build_coverity_scan.sh" | bash || true
================================================
FILE: .gitignore
================================================
*.mobi
genmobi.sh
.libs
*.swp
*.slo
*.la
*.swo
*.lo
*~
*.o
print.txt
.rsync
*.tar.gz
dist
build[789]
build
tags
update-readme
*.tmp
test/Makefile
test/blib
test.sh
t.sh
t/t.sh
test/t/servroot/
releng
reset
*.t_
src/handler.h
src/util.c
src/module.h
src/module.c
src/drizzle.c
src/processor.h
src/handler.c
src/util.h
src/drizzle.h
src/processor.c
src/output.c
src/output.h
libdrizzle
ctags
src/stream.h
nginx
keepalive
reindex
src/keepalive.c
src/keepalive.h
src/checker.h
src/checker.c
src/quoting.h
src/quoting.c
src/upstream.[ch]
all
go
buildroot/
pack
t/servroot/
build1[0-9]
*.plist
Makefile
*.patch
analyze
*.html
================================================
FILE: .travis.yml
================================================
sudo: required
dist: focal
branches:
only:
- "master"
os: linux
language: c
cache:
apt: true
directories:
- download-cache
addons:
apt:
packages:
- axel
- cpanminus
- libtest-base-perl
- libtext-diff-perl
- liburi-perl
- libwww-perl
- libtest-longstring-perl
- liblist-moreutils-perl
- libgd-dev
services:
- mysql
compiler:
- gcc
env:
global:
- LUAJIT_PREFIX=/opt/luajit21
- LUAJIT_LIB=$LUAJIT_PREFIX/lib
- LIBDRIZZLE_PREFIX=/opt/drizzle
- LIBDRIZZLE_INC=$LIBDRIZZLE_PREFIX/include/libdrizzle-1.0
- LIBDRIZZLE_LIB=$LIBDRIZZLE_PREFIX/lib
- LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1
- LUA_INCLUDE_DIR=$LUAJIT_INC
- LUA_CMODULE_DIR=/lib
- JOBS=3
- NGX_BUILD_JOBS=$JOBS
- TEST_NGINX_SLEEP=0.006
matrix:
- NGINX_VERSION=1.29.8
install:
- if [ ! -f download-cache/drizzle7-2011.07.21.tar.gz ]; then wget -P download-cache https://github.com/openresty/openresty-deps-prebuild/releases/download/v20230902/drizzle7-2011.07.21.tar.gz; fi
- git clone https://github.com/openresty/nginx-devel-utils.git
- git clone https://github.com/openresty/openresty.git ../openresty
- git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
- git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
- git clone https://github.com/openresty/test-nginx.git
- git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git
- git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
- git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core
- git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache
- git clone https://github.com/openresty/nginx-eval-module.git ../eval-nginx-module
- git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
- git clone https://github.com/openresty/set-misc-nginx-module.git ../set-misc-nginx-module
- git clone https://github.com/openresty/headers-more-nginx-module.git ../headers-more-nginx-module
- git clone https://github.com/openresty/rds-json-nginx-module.git ../rds-json-nginx-module
- git clone https://github.com/openresty/openresty.git ../ngx_openresty
before_script:
- mysql -uroot -e "create database ngx_test; CREATE USER 'ngx_test'@'%' IDENTIFIED WITH mysql_native_password BY 'ngx_test'; grant all on ngx_test.* to 'ngx_test'@'%'; flush privileges;"
script:
- tar xzf download-cache/drizzle7-2011.07.21.tar.gz && cd drizzle7-2011.07.21
- ./configure --prefix=$LIBDRIZZLE_PREFIX --without-server > build.log 2>&1 || (cat build.log && exit 1)
- sudo PATH=$PATH make libdrizzle-1.0 install-libdrizzle-1.0 > build.log 2>&1 || (cat build.log && exit 1)
- cd ../luajit2
- make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT' > build.log 2>&1 || (cat build.log && exit 1)
- sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1)
- cpanm --sudo --notest Test::Nginx IPC::Run Test2::Util > build.log 2>&1 || (cat build.log && exit 1)
- cd ..
- cd test-nginx && sudo cpanm . && cd ..
- export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:$PATH
- export NGX_BUILD_CC=$CC
- sh util/build.sh $NGINX_VERSION > build.log 2>&1 || (cat build.log && exit 1)
- nginx -V
- ldd `which nginx`|grep luajit
- prove -I. -r t
================================================
FILE: Changes
================================================
v0.1.0 - 5 July 2011
* now we require at least libdrizzle 1.0, which supports official mysql 5.5+ and is much more stable under load. thanks Taylor Weibley for pushing this. We no longer require patching libdrizzle any more.
* fixed a compilation issue on Mac OS X: we should include drizzle.h prior to nginx headers, or we will not get the "bool" type properly installed on Mac OS X.
* fixed the spots that trigger -Wunused-but-set-variable by gcc 4.6.
* fixed the duplicate last chunk issue: we should not set the last_buf flag ourselves in drizzle_output.c because ngx_http_upstream already sends a last buf for us.
* ported over Maxim Dounin's patch for ngx_http_upstream_keepalive connection pool fixes: we should have discarded stale read events for cached tcp connections in the pool.
* implemented the new drizzle_status directive to provide connection pool status monitoring capability.
* fixed a minor bug in the connection pool: we should resume the "name", "sockaddr", and "socklen" fields for the connection from the pool such that we can get more detailed error log messages with the "upstream: drizzle://ip.add.re.ss:port" bit.
* implemented the $drizzle_thread_id variable which is automatically set when mysql/drizzle times out.
* now we use the 2-clause bsd license.
* report an error message when upstream name not found for drizzle_pass.
* now we implemented the charset option for the "drizzle_server" diredctive which causes ngx_drizzle send "set names xxx" automatcially for every connection to that drizzle server.
* added a lot of more documentation.
================================================
FILE: README.markdown
================================================
Name
====
drizzle-nginx-module - Upstream module for talking to MySQL and Drizzle directly
*This module is not distributed with the Nginx source.* See [the installation instructions](#installation).
Table of Contents
=================
* [Status](#status)
* [Version](#version)
* [Synopsis](#synopsis)
* [Description](#description)
* [Keepalive connection pool](#keepalive-connection-pool)
* [Last Insert ID](#last-insert-id)
* [Directives](#directives)
* [drizzle_server](#drizzle_server)
* [drizzle_keepalive](#drizzle_keepalive)
* [drizzle_query](#drizzle_query)
* [drizzle_pass](#drizzle_pass)
* [drizzle_connect_timeout](#drizzle_connect_timeout)
* [drizzle_send_query_timeout](#drizzle_send_query_timeout)
* [drizzle_recv_cols_timeout](#drizzle_recv_cols_timeout)
* [drizzle_recv_rows_timeout](#drizzle_recv_rows_timeout)
* [drizzle_buffer_size](#drizzle_buffer_size)
* [drizzle_module_header](#drizzle_module_header)
* [drizzle_status](#drizzle_status)
* [Variables](#variables)
* [$drizzle_thread_id](#drizzle_thread_id)
* [Output Format](#output-format)
* [RDS Header Part](#rds-header-part)
* [RDS Body Part](#rds-body-part)
* [Columns](#columns)
* [Rows](#rows)
* [Row Flag](#row-flag)
* [Fields Data](#fields-data)
* [RDS buffer Limitations](#rds-buffer-limitations)
* [Status Code](#status-code)
* [Caveats](#caveats)
* [Trouble Shooting](#trouble-shooting)
* [Known Issues](#known-issues)
* [Installation](#installation)
* [Compatibility](#compatibility)
* [Community](#community)
* [English Mailing List](#english-mailing-list)
* [Chinese Mailing List](#chinese-mailing-list)
* [Report Bugs](#report-bugs)
* [Source Repository](#source-repository)
* [Test Suite](#test-suite)
* [TODO](#todo)
* [Changes](#changes)
* [Authors](#authors)
* [Copyright & License](#copyright--license)
* [See Also](#see-also)
Status
======
This module is already production ready.
Version
=======
This document describes ngx_drizzle [v0.1.11](https://github.com/openresty/drizzle-nginx-module/tags) released on 19 April 2018.
Synopsis
========
```nginx
http {
...
upstream cluster {
# simple round-robin
drizzle_server 127.0.0.1:3306 dbname=test
password=some_pass user=monty protocol=mysql;
drizzle_server 127.0.0.1:1234 dbname=test2
password=pass user=bob protocol=drizzle;
}
upstream backend {
drizzle_server 127.0.0.1:3306 dbname=test
password=some_pass user=monty protocol=mysql;
}
server {
location /mysql {
set $my_sql 'select * from cats';
drizzle_query $my_sql;
drizzle_pass backend;
drizzle_connect_timeout 500ms; # default 60s
drizzle_send_query_timeout 2s; # default 60s
drizzle_recv_cols_timeout 1s; # default 60s
drizzle_recv_rows_timeout 1s; # default 60s
}
...
# for connection pool monitoring
location /mysql-pool-status {
allow 127.0.0.1;
deny all;
drizzle_status;
}
}
}
```
[Back to TOC](#table-of-contents)
Description
===========
This is an nginx upstream module integrating [libdrizzle](https://launchpad.net/drizzle) into Nginx in a non-blocking and streamming way.
Essentially it provides a very efficient and flexible way for nginx internals to access MySQL, Drizzle, as well as other RDBMS's that support the Drizzle or MySQL wired protocol. Also it can serve as a direct REST interface to those RDBMS backends.
This module does not generate human-readable outputs, rather, in a binary format called Resty-DBD-Stream (RDS) designed by ourselves. You usually need other components, like [rds-json-nginx-module](http://github.com/openresty/rds-json-nginx-module), [rds-csv-nginx-module](http://github.com/openresty/rds-csv-nginx-module), or [lua-rds-parser](http://github.com/openresty/lua-rds-parser), to work with this module. See [Output Format](#output-format) for details.
[Back to TOC](#table-of-contents)
Keepalive connection pool
-------------------------
This module also provides a builtin per-worker connection pool mechanism for MySQL or Drizzle TCP connections.
Here's a sample configuration:
```nginx
upstream backend {
drizzle_server 127.0.0.1:3306 dbname=test
password=some_pass user=monty protocol=mysql;
drizzle_keepalive max=100 mode=single overflow=reject;
}
```
For now, the connection pool uses a simple LIFO algorithm to assign idle connections in the pool. That is, most recently (successfully) used connections will be reused first the next time. And new idle connections will always replace the oldest idle connections in the pool even if the pool is already full.
See the [drizzle_keepalive](#drizzle_keepalive) directive for more details.
[Back to TOC](#table-of-contents)
Last Insert ID
--------------
If you want to get LAST_INSERT_ID, then ngx_drizzle already returns that automatically for you when you're doing a SQL insert query. Consider the following sample `nginx.conf` snippet:
```nginx
location /test {
echo_location /mysql "drop table if exists foo";
echo;
echo_location /mysql "create table foo (id serial not null, primary key (id), val real);";
echo;
echo_location /mysql "insert into foo (val) values (3.1415926);";
echo;
echo_location /mysql "select * from foo;";
echo;
}
location /mysql {
drizzle_pass backend;
drizzle_module_header off;
drizzle_query $query_string;
rds_json on;
}
```
Then request `GET /test` gives the following outputs:
```javascript
{"errcode":0}
{"errcode":0}
{"errcode":0,"insert_id":1,"affected_rows":1}
[{"id":1,"val":3.1415926}]
```
You can see the `insert_id` field (as well as the `affected_rows` field in the 3rd JSON response.
[Back to TOC](#table-of-contents)
Directives
==========
[Back to TOC](#table-of-contents)
drizzle_server
--------------
**syntax:** *drizzle_server <host> user=<user> password=<pass> dbname=<database>*
**syntax:** *drizzle_server <host>:<port> user=<user> password=<pass> dbname=<database> protocol=<protocol> charset=<charset>*
**default:** *no*
**context:** *upstream*
Directive assigns the name and the parameters of server. For the name it is possible to use a domain name, an address, with an optional port (default: 3306). If domain name resolves to several addresses, then all are used.
The following options are supported:
**user=**`<user>`
MySQL/Drizzle user name `<user>` for login.
**password=**`<pass>`
Specify mysql password `<pass>`for login. If you have special characters like `#` or spaces in your password text, then you'll have to quote the whole key-value pair with either single-quotes or double-quotes, as in
```nginx
drizzle_server 127.0.0.1:3306 user=monty "password=a b#1"
dbname=test protocol=mysql;
```
**dbname=**`<database>`
Specify default MySQL database `<database>` for the connection. Note that MySQL does allow referencing tables belonging to different databases by qualifying table names with database names in SQL queries.
**protocol=**`<protocol>`
Specify which wire protocol to use, `drizzle` or `mysql`. Default to `drizzle`.
**charset=**`<charset>`
Explicitly specify the character set for the MySQL connections. Setting this option to a non-empty value will make this module send out a `set names '<charset>'` query right after the mysql connection is established.
If the default character encoding of the MySQL connection is already what you want, you needn't set this option because it has extra runtime cost.
Here is a small example:
```nginx
drizzle_server foo.bar.com:3306 user=monty password=some_pass
dbname=test protocol=mysql
charset=utf8;
```
Please note that for the mysql server, "utf-8" is not a valid encoding name while `utf8` is.
[Back to TOC](#table-of-contents)
drizzle_keepalive
-----------------
**syntax:** *drizzle_keepalive max=<size> mode=<mode>*
**default:** *drizzle_keepalive max=0 mode=single*
**context:** *upstream*
Configures the keep-alive connection pool for MySQL/Drizzle connections.
The following options are supported:
**max=**`<num>`
Specify the capacity of the connection pool for the current upstream block. The <num> value *must* be non-zero. If set to `0`, it effectively disables the connection pool. This option is default to `0`.
**mode=**`<mode>`
This supports two values, `single` and `multi`. The `single` mode means the pool does not distinguish various drizzle servers in the current upstream block while `multi` means the pool will merely reuse connections which have identical server host names and ports. Note that even under `multi`, differences between `dbname` or `user` parameters will be silently ignored. Default to `single`.
**overflow=**`<action>`
This option specifies what to do when the connection pool is already full while new database connection is required. Either `reject` or `ignore` can be specified. In case of `reject`, it will reject the current request, and returns the `503 Service Unavailable` error page. For `ignore`, this module will go on creating a new database connection.
[Back to TOC](#table-of-contents)
drizzle_query
-------------
**syntax:** *drizzle_query <sql>*
**default:** *no*
**context:** *http, server, location, location if*
Specify the SQL queries sent to the Drizzle/MySQL backend.
Nginx variable interpolation is supported, but you must be careful with SQL injection attacks. You can use the [set_quote_sql_str](http://github.com/openresty/set-misc-nginx-module#set_quote_sql_str) directive, for example, to quote values for SQL interpolation:
```nginx
location /cat {
set_unescape_uri $name $arg_name;
set_quote_sql_str $quoted_name $name;
drizzle_query "select * from cats where name = $quoted_name";
drizzle_pass my_backend;
}
```
[Back to TOC](#table-of-contents)
drizzle_pass
------------
**syntax:** *drizzle_pass <remote>*
**default:** *no*
**context:** *location, location if*
**phase:** *content*
This directive specifies the Drizzle or MySQL upstream name to be queried in the current location. The `<remote>` argument can be any upstream name defined with the [drizzle_server](#drizzle_server) directive.
Nginx variables can also be interpolated into the `<remote>` argument, so as to do dynamic backend routing, for example:
```nginx
upstream moon { drizzle_server ...; }
server {
location /cat {
set $backend 'moon';
drizzle_query ...;
drizzle_pass $backend;
}
}
```
[Back to TOC](#table-of-contents)
drizzle_connect_timeout
-----------------------
**syntax:** *drizzle_connect_time <time>*
**default:** *drizzle_connect_time 60s*
**context:** *http, server, location, location if*
Specify the (total) timeout for connecting to a remote Drizzle or MySQL server.
The `<time>` argument can be an integer, with an optional time unit, like `s` (second), `ms` (millisecond), `m` (minute). The default time unit is `s`, i.e., "second". The default setting is `60s`.
[Back to TOC](#table-of-contents)
drizzle_send_query_timeout
--------------------------
**syntax:** *drizzle_send_query_timeout <time>*
**default:** *drizzle_send_query_timeout 60s*
**context:** *http, server, location, location if*
Specify the (total) timeout for sending a SQL query to a remote Drizzle or MySQL server.
The `<time>` argument can be an integer, with an optional time unit, like `s` (second), `ms` (millisecond), `m` (minute). The default time unit is `s`, ie, "second". The default setting is `60s`.
[Back to TOC](#table-of-contents)
drizzle_recv_cols_timeout
-------------------------
**syntax:** *drizzle_recv_cols_timeout <time>*
**default:** *drizzle_recv_cols_timeout 60s*
**context:** *http, server, location, location if*
Specify the (total) timeout for receiving the columns metadata of the result-set to a remote Drizzle or MySQL server.
The `<time>` argument can be an integer, with an optional time unit, like `s` (second), `ms` (millisecond), `m` (minute). The default time unit is `s`, ie, "second". The default setting is `60s`.
[Back to TOC](#table-of-contents)
drizzle_recv_rows_timeout
-------------------------
**syntax:** *drizzle_recv_rows_timeout <time>*
**default:** *drizzle_recv_rows_timeout 60s*
**context:** *http, server, location, location if*
Specify the (total) timeout for receiving the rows data of the result-set (if any) to a remote Drizzle or MySQL server.
The `<time>` argument can be an integer, with an optional time unit, like `s` (second), `ms` (millisecond), `m` (minute). The default time unit is `s`, ie, "second". The default setting is `60s`.
[Back to TOC](#table-of-contents)
drizzle_buffer_size
-------------------
**syntax:** *drizzle_buffer_size <size>*
**default:** *drizzle_buffer_size 4k/8k*
**context:** *http, server, location, location if*
Specify the buffer size for drizzle outputs. Default to the page size (4k/8k). The larger the buffer, the less streammy the outputing process will be.
[Back to TOC](#table-of-contents)
drizzle_module_header
---------------------
**syntax:** *drizzle_module_header on|off*
**default:** *drizzle_module_header on*
**context:** *http, server, location, location if*
Controls whether to output the drizzle header in the response. Default on.
The drizzle module header looks like this:
X-Resty-DBD-Module: ngx_drizzle 0.1.0
[Back to TOC](#table-of-contents)
drizzle_status
--------------
**syntax:** *drizzle_status*
**default:** *no*
**context:** *location, location if*
**phase:** *content*
When specified, the current Nginx location will output a status report for all the drizzle upstream servers in the virtual server of the current Nginx worker process.
The output looks like this:
worker process: 15231
upstream backend
active connections: 0
connection pool capacity: 10
overflow: reject
cached connection queue: 0
free'd connection queue: 10
cached connection successfully used count:
free'd connection successfully used count: 3 0 0 0 0 0 0 0 0 0
servers: 1
peers: 1
upstream backend2
active connections: 0
connection pool capacity: 0
servers: 1
peers: 1
Note that, this is *not* the global statistics if you do have multiple Nginx worker processes configured in your `nginx.conf`.
[Back to TOC](#table-of-contents)
Variables
=========
This module creates the following Nginx variables:
[Back to TOC](#table-of-contents)
$drizzle_thread_id
------------------
This variable will be assigned a textual number of the underlying MySQL or Drizzle query thread ID when the current SQL query times out. This thread ID can be further used in a SQL kill command to cancel the timed-out query.
Here's an example:
```nginx
drizzle_connect_timeout 1s;
drizzle_send_query_timeout 2s;
drizzle_recv_cols_timeout 1s;
drizzle_recv_rows_timeout 1s;
location /query {
drizzle_query 'select sleep(10)';
drizzle_pass my_backend;
rds_json on;
more_set_headers -s 504 'X-Mysql-Tid: $drizzle_thread_id';
}
location /kill {
drizzle_query "kill query $arg_tid";
drizzle_pass my_backend;
rds_json on;
}
location /main {
content_by_lua '
local res = ngx.location.capture("/query")
if res.status ~= ngx.HTTP_OK then
local tid = res.header["X-Mysql-Tid"]
if tid and tid ~= "" then
ngx.location.capture("/kill", { args = {tid = tid} })
end
return ngx.HTTP_INTERNAL_SERVER_ERROR;
end
ngx.print(res.body)
'
}
```
where we make use of [headers-more-nginx-module](http://github.com/openresty/headers-more-nginx-module), [lua-nginx-module](http://github.com/openresty/lua-nginx-module), and [rds-json-nginx-module](http://github.com/openresty/rds-json-nginx-module) too. When the SQL query timed out, we'll explicitly cancel it immediately. One pitfall here is that you have to add these modules in this order while building Nginx:
* [lua-nginx-module](http://github.com/openresty/lua-nginx-module)
* [headers-more-nginx-module](http://github.com/openresty/headers-more-nginx-module)
* [rds-json-nginx-module](http://github.com/openresty/rds-json-nginx-module)
Such that, their output filters will work in the *reversed* order, i.e., first convert RDS to JSON, and then add our `X-Mysql-Tid` custom header, and finally capture the whole (subrequest) response with the Lua module. You're recommended to use the [OpenResty bundle](http://openresty.org/) though, it ensures the module building order automatically for you.
[Back to TOC](#table-of-contents)
Output Format
=============
This module generates binary query results in a format that is shared among the various Nginx database driver modules like [ngx_postgres](http://github.com/FRiCKLE/ngx_postgres/). This data format is named `Resty DBD Stream` (RDS).
If you're a web app developer, you may be more interested in
* using [rds-json-nginx-module](http://github.com/openresty/rds-json-nginx-module) to obtain JSON output,
* using [rds-csv-nginx-module](http://github.com/openresty/rds-csv-nginx-module) to obain Comma-Separated-Value (CSV) output,
* or using [lua-rds-parser](http://github.com/openresty/lua-rds-parser) to parse the RDS data into Lua data structures.
For the HTTP response header part, the `200 OK` status code should always be returned. The `Content-Type` header *must* be set to `application/x-resty-dbd-stream`. And the driver generating this response also sets a `X-Resty-DBD` header. For instance, this module adds the following output header:
X-Resty-DBD-Module: drizzle 0.1.0
where `0.1.0` is this module's own version number. This `X-Resty-DBD-Module` header is optional though.
Below is the HTTP response body format (version 0.0.3):
[Back to TOC](#table-of-contents)
RDS Header Part
---------------
The RDS Header Part consists of the following fields:
**uint8_t**
endian type (1 means big-endian and little endian otherwise)
**uint32_t**
format version (v1.2.3 is represented as 1002003 in decimal)
**uint8_t**
result type (0 means normal SQL result type, fixed for now)
**uint16_t**
standard error code
**uint16_t**
driver-specific error code
**uint16_t**
driver-specific error string length
**u_char ***
driver-specific error string data
**uint64_t**
database rows affected
**uint64_t**
insert id (if none, 0)
**uint16_t**
column count
[Back to TOC](#table-of-contents)
RDS Body Part
-------------
When the `column count` field in the [RDS Header Part](#rds-header-part) is zero, then the whole RDS Body Part is omitted.
The RDS Body Part consists of two sections, [Columns](#columns) and [Rows](#rows).
### Columns
The columns part consists of zero or more column data. The number of columns is determined by `column count` field in [RDS Header Part](#rds-header-part).
Each column consists of the following fields
**uint16_t**
non-zero value for standard column type code and for the column list terminator and zero otherwise.
**uint16_t**
driver-specific column type code
**uint16_t**
column name length
**u_char ***
column name data
### Rows
The rows part consists of zero or more row data, terminated by a 8-bit zero.
Each row data consists of a [Row Flag](#row-flag) and an optional [Fields Data](#fields-data) part.
#### Row Flag
**uint8_t**
valid row (1 means valid, and 0 means the row list terminator)
#### Fields Data
The Fields Data consists zero or more fields of data. The field count is predetermined by the <code>column number</code) specified in [RDS Header Part](#rds-header-part).
**uint32_t**
field length ((uint32_t) -1 represents NULL)
**u_char ***
field data in textual representation), is empty (0) if field length == (uint32_t) -1
[Back to TOC](#table-of-contents)
RDS buffer Limitations
----------------------
On the nginx output chain link level, the following components should be put into a single `ngx_buf_t` struct:
* the header
* each column and the column list terminator
* each row's valid flag byte and row list terminator
* each field in each row (if any) but the field data can span multiple bufs.
[Back to TOC](#table-of-contents)
Status Code
===========
If the MySQL error code in MySQL's query result is not OK, then a 500 error page is returned by this module, except for the table non-existent error, which results in the `410 Gone` error page.
[Back to TOC](#table-of-contents)
Caveats
=======
* Other usptream modules like `upstream_hash` and [HttpUpstreamKeepaliveModule](http://wiki.nginx.org/HttpUpstreamKeepaliveModule) *must not* be used with this module in a single upstream block.
* Directives like [server](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server) *must not* be mixed with [drizzle_server](#drizzle_server) either.
* Upstream backends that don't use [drizzle_server](#drizzle_server) to define server entries *must not* be used in the [drizzle_pass](#drizzle_pass) directive.
[Back to TOC](#table-of-contents)
Trouble Shooting
================
* When you see the following error message in `error.log`:
failed to connect: 15: drizzle_state_handshake_result_read:
old insecure authentication mechanism not supported in upstream, ...
then you may checkout if your MySQL is too old (at least 5.x is required) or your mysql config file explicitly forces the use of old authentication method (you should remove the `old-passwords` line from your `my.cnf` and add the line `secure_auth 1`).
* When you see the following error message in `error.log`:
failed to connect: 23: Access denied for user 'root'@'ubuntu'
(using password: YES) while connecting to drizzle upstream, ...
You should check if your MySQL account does have got TCP login access on your MySQL server side. A quick check is to use MySQL's official client to connect to your server:
```bash
mysql --protocol=tcp -u user --password=password -h foo.bar.com dbname
```
Note that the `--protocol=tcp` option is required here, or your MySQL client may use Unix Domain Socket to connect to your MySQL server.
[Back to TOC](#table-of-contents)
Known Issues
============
* Calling mysql procedures are currently not supported because the underlying libdrizzle library does not support the `CLIENT_MULTI_RESULTS` flag yet :( But we'll surely work on it.
* Multiple SQL statements in a single query are not supported due to the lack of `CLIENT_MULTI_STATEMENTS` support in the underlying libdrizzle library.
* This module does not (yet) work with the `RTSIG` event model.
[Back to TOC](#table-of-contents)
Installation
============
You're recommended to install this module as well as [rds-json-nginx-module](http://github.com/openresty/rds-json-nginx-module) via the OpenResty bundle:
<http://openresty.org>
The installation steps are usually as simple as `./configure --with-http_drizzle_module && make && make install` (But you still need to install the libdrizzle library manually, see [<http://openresty.org/en/drizzle-nginx-module.html]>(http://openresty.org/en/drizzle-nginx-module.html) for detailed instructions.
Alternatively, you can compile this module with Nginx core's source by hand:
* You should first install libdrizzle 1.0 which is now distributed with the drizzle project and can be obtained from [<https://launchpad.net/drizzle]>(https://launchpad.net/drizzle). The latest drizzle7 release does not support building libdrizzle 1.0 separately and requires a lot of external dependencies like Boost and Protobuf which are painful to install. The last version supporting building libdrizzle 1.0 separately is `2011.07.21`. You can download it from <http://agentzh.org/misc/nginx/drizzle7-2011.07.21.tar.gz> . Which this version of drizzle7, installation of libdrizzle 1.0 is usually as simple as
```nginx
tar xzvf drizzle7-2011.07.21.tar.gz
cd drizzle7-2011.07.21/
./configure --without-server
make libdrizzle-1.0
make install-libdrizzle-1.0
```
Ensure that you have the `python` command point to a `python2` interpreter. It's known that on recent : Arch Linux distribution, `python` is linked to `python3` by default, and while running `make libdrizzle-1.0` will yield the error
```bash
File "config/pandora-plugin", line 185
print "Dependency loop detected with %s" % plugin['name']
^
SyntaxError: invalid syntax
make: *** [.plugin.scan] Error 1
```
You can fix this by pointing `python` to `python2`.
* Download the latest version of the release tarball of this module from drizzle-nginx-module [file list](http://github.com/openresty/drizzle-nginx-module/tags).
* Grab the nginx source code from [nginx.org](http://nginx.org/), for example, the version 1.13.6 (see [nginx compatibility](#compatibility)), and then build the source with this module:
```bash
wget 'http://nginx.org/download/nginx-1.13.6.tar.gz'
tar -xzvf nginx-1.13.6.tar.gz
cd nginx-1.13.6/
# if you have installed libdrizzle to the prefix /opt/drizzle, then
# specify the following environments:
# export LIBDRIZZLE_INC=/opt/drizzle/include/libdrizzle-1.0
# export LIBDRIZZLE_LIB=/opt/drizzle/lib
# Here we assume you would install you nginx under /opt/nginx/.
./configure --prefix=/opt/nginx \
--add-module=/path/to/drizzle-nginx-module
make -j2
make install
```
You usually also need [rds-json-nginx-module](http://github.com/openresty/rds-json-nginx-module) to obtain JSON output from the binary RDS output generated by this upstream module.
[Back to TOC](#table-of-contents)
Compatibility
=============
If you're using MySQL, then MySQL `5.0 ~ 5.5` is required. We're not sure if MySQL `5.6+` work; reports welcome!
This module has been tested on Linux and Mac OS X. Reports on other POSIX-compliant systems will be highly appreciated.
The following versions of Nginx should work with this module:
* 1.16.x
* 1.15.x (last tested: 1.15.8)
* 1.14.x
* 1.13.x (last tested: 1.13.6)
* 1.12.x
* 1.11.x (last tested: 1.11.2)
* 1.10.x
* 1.9.x (last tested: 1.9.15)
* 1.8.x
* 1.7.x (last tested: 1.7.10)
* 1.6.x
* 1.5.x (last tested: 1.5.8)
* 1.4.x (last tested: 1.4.4)
* 1.3.x (last tested: 1.3.7)
* 1.2.x (last tested: 1.2.9)
* 1.1.x (last tested: 1.1.5)
* 1.0.x (last tested: 1.0.8)
* 0.8.x (last tested: 0.8.55)
* 0.7.x >= 0.7.44 (last tested version is 0.7.67)
Earlier versions of Nginx like `0.6.x` and `0.5.x` will *not* work.
If you find that any particular version of Nginx above `0.7.44` does not work with this module, please consider reporting a bug.
[Back to TOC](#table-of-contents)
Community
=========
[Back to TOC](#table-of-contents)
English Mailing List
--------------------
The [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers.
[Back to TOC](#table-of-contents)
Chinese Mailing List
--------------------
The [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers.
[Back to TOC](#table-of-contents)
Report Bugs
===========
Please submit bug reports, wishlists, or patches by
1. creating a ticket on the [issue tracking interface](http://github.com/openresty/drizzle-nginx-module/issues) provided by GitHub,
1. or sending an email to the [OpenResty community](#community).
[Back to TOC](#table-of-contents)
Source Repository
=================
Available on github at [openresty/drizzle-nginx-module](http://github.com/openresty/drizzle-nginx-module).
[Back to TOC](#table-of-contents)
Test Suite
==========
This module comes with a Perl-driven test suite. The [test cases](http://github.com/openresty/drizzle-nginx-module/tree/master/t/) are
[declarative](http://github.com/openresty/drizzle-nginx-module/blob/master/t/sanity.t) too. Thanks to the [Test::Nginx](http://search.cpan.org/perldoc?Test::Nginx) module in the Perl world.
To run it on your side:
```bash
$ PATH=/path/to/your/nginx-with-echo-module:$PATH prove -r t
```
Because a single nginx server (by default, `localhost:1984`) is used across all the test scripts (`.t` files), it's meaningless to run the test suite in parallel by specifying `-jN` when invoking the `prove` utility.
[Back to TOC](#table-of-contents)
TODO
====
* add the MySQL transaction support.
* add multi-statement MySQL query support.
* implement the "drizzle_max_output_size" directive. When the RDS data is larger then the size specified, the module will try to terminate the output as quickly as possible but will still ensure the resulting response body is still in valid RDS format.
* implement the `drizzle_upstream_next` mechanism for failover support.
* add support for multiple "drizzle_query" directives in a single location.
* implement *weighted* round-robin algorithm for the upstream server clusters.
* add the `max_idle_time` option to the [drizzle_server](#drizzle_server) directive, so that the connection pool will automatically release idle connections for the timeout you specify.
* add the `min` option to the "drizzle_server" directive so that the connection pool will automatically create that number of connections and put them into the pool.
* add Unix domain socket support in the `drizzle_server` directive.
* make the [drizzle_query](#drizzle_query) directive reject variables that have not been processed by a [drizzle_process](#drizzle_process) directive. This will pretect us from SQL injections. There will also be an option ("strict=no") to disable such checks.
[Back to TOC](#table-of-contents)
Changes
=======
The changes of every release of this module can be obtained from the OpenResty bundle's change logs:
<http://openresty.org/#Changes>
[Back to TOC](#table-of-contents)
Authors
=======
* chaoslawful (王晓哲) <chaoslawful at gmail dot com>
* Yichun "agentzh" Zhang (章亦春) <agentzh at gmail dot com>, OpenResty Inc.
* Piotr Sikora <piotr.sikora at frickle dot com>, Google Inc.
[Back to TOC](#table-of-contents)
Copyright & License
===================
This module is licenced under the BSD license.
Copyright (C) 2009-2014, by Xiaozhe Wang (chaoslawful) <chaoslawful@gmail.com>.
Copyright (C) 2009-2018, by Yichun "agentzh" Zhang (章亦春) <agentzh@gmail.com>, OpenResty Inc.
Copyright (C) 2010-2014, by FRiCKLE Piotr Sikora <info@frickle.com>, Google Inc.
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.
[Back to TOC](#table-of-contents)
See Also
========
* [rds-json-nginx-module](http://github.com/openresty/rds-json-nginx-module)
* [rds-csv-nginx-module](http://github.com/openresty/rds-csv-nginx-module)
* [lua-rds-parser](http://github.com/openresty/lua-rds-parser)
* [The OpenResty bundle](http://openresty.org)
* [DrizzleNginxModule bundled by OpenResty](http://openresty.org/#DrizzleNginxModule)
* [postgres-nginx-module](http://github.com/FRiCKLE/ngx_postgres)
* [lua-nginx-module](http://github.com/openresty/lua-nginx-module)
* The [lua-resty-mysql](https://github.com/openresty/lua-resty-mysql) library based on the [lua-nginx-module](http://github.com/openresty/lua-nginx-module) cosocket API.
================================================
FILE: config
================================================
ngx_feature_name=
ngx_feature_run=no
ngx_feature_incs="#include <libdrizzle/drizzle_client.h>"
ngx_feature_test="drizzle_version();"
if [ -n "$LIBDRIZZLE_INC" -o -n "$LIBDRIZZLE_LIB" ]; then
# explicit set libdrizzle lib path
ngx_feature="libdrizzle library in directories specified by LIBDRIZZLE_INC ($LIBDRIZZLE_INC) and LIBDRIZZLE_LIB ($LIBDRIZZLE_LIB)"
ngx_feature_path="$LIBDRIZZLE_INC"
if [ $NGX_RPATH = YES ]; then
ngx_feature_libs="-R$LIBDRIZZLE_LIB -L$LIBDRIZZLE_LIB -ldrizzle"
else
ngx_feature_libs="-L$LIBDRIZZLE_LIB -ldrizzle"
fi
. auto/feature
else
# auto-discovery
ngx_feature="libdrizzle library"
ngx_feature_path=
ngx_feature_libs="-ldrizzle"
. auto/feature
if [ $ngx_found = no ]; then
# Ubuntu 12.04
ngx_feature="libdrizzle library in /usr/local/"
ngx_feature_path="/usr/include/libdrizzle-1.0"
if [ $NGX_RPATH = YES ]; then
ngx_feature_libs="-R/usr/lib -L/usr/lib -ldrizzle"
else
ngx_feature_libs="-L/usr/lib -ldrizzle"
fi
. auto/feature
fi
if [ $ngx_found = no ]; then
# FreeBSD, OpenBSD
ngx_feature="libdrizzle library in /usr/local/"
ngx_feature_path="/usr/local/include/libdrizzle-1.0"
if [ $NGX_RPATH = YES ]; then
ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -ldrizzle"
else
ngx_feature_libs="-L/usr/local/lib -ldrizzle"
fi
. auto/feature
fi
if [ $ngx_found = no ]; then
# NetBSD
ngx_feature="libdrizzle library in /usr/pkg/"
ngx_feature_path="/usr/pkg/include/libdrizzle-1.0"
if [ $NGX_RPATH = YES ]; then
ngx_feature_libs="-R/usr/pkg/lib -L/usr/pkg/lib -ldrizzle"
else
ngx_feature_libs="-L/usr/pkg/lib -ldrizzle"
fi
. auto/feature
fi
if [ $ngx_found = no ]; then
# MacPorts
ngx_feature="libdrizzle library in /opt/local/"
ngx_feature_path="/opt/local/include/libdrizzle-1.0"
if [ $NGX_RPATH = YES ]; then
ngx_feature_libs="-R/opt/local/lib -L/opt/local/lib -ldrizzle"
else
ngx_feature_libs="-L/opt/local/lib -ldrizzle"
fi
. auto/feature
fi
fi
if [ $ngx_found = yes ]; then
CORE_INCS="$CORE_INCS $ngx_feature_path"
CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
else
cat << END
$0: error: the ngx_drizzle addon requires the libdrizzle library.
END
exit 1
fi
ngx_addon_name=ngx_http_drizzle_module
HTTP_MODULES="$HTTP_MODULES ngx_http_drizzle_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_drizzle_module.c $ngx_addon_dir/src/ngx_http_drizzle_handler.c $ngx_addon_dir/src/ngx_http_drizzle_processor.c $ngx_addon_dir/src/ngx_http_drizzle_upstream.c $ngx_addon_dir/src/ngx_http_drizzle_util.c $ngx_addon_dir/src/ngx_http_drizzle_output.c $ngx_addon_dir/src/ngx_http_drizzle_keepalive.c $ngx_addon_dir/src/ngx_http_drizzle_quoting.c $ngx_addon_dir/src/ngx_http_drizzle_checker.c"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/ngx_http_drizzle_module.h $ngx_addon_dir/src/ngx_http_drizzle_handler.h $ngx_addon_dir/src/ngx_http_drizzle_processor.h $ngx_addon_dir/src/ngx_http_drizzle_upstream.h $ngx_addon_dir/src/ngx_http_drizzle_util.h $ngx_addon_dir/src/ngx_http_drizzle_output.h $ngx_addon_dir/src/resty_dbd_stream.h $ngx_addon_dir/src/ngx_http_drizzle_keepalive.h $ngx_addon_dir/src/ngx_http_drizzle_quoting.h $ngx_addon_dir/src/ngx_http_drizzle_checker.h"
have=NGX_DRIZZLE_MODULE . auto/have
================================================
FILE: doc/design
================================================
main request to location "/drizzle" (handled by mod_drizzle)
-> mod_drizzle content handler (is main request)
-> emit subrequest to location "/drizzle"
-> return NGX_DONE
-> mod_drizzle content handler (is subrequest)
-> connect db (through libdrizzle), get db connection fd
-> wrap db connection fd into a ngx_event, reg rd/wr event handler
-> return NGX_DONE
-> i/o event occured on db connection
-> rd/wr event handler called, call mod_drizzle content handler using subrequest struct
-> mod_drizzle content handler called (is subrequest)
-> continue libdrizzle state machine
-> generate response header and body if done,
then unreg db connection events,
and call ngx_http_finalize_request to release self
-> return NGX_OK
================================================
FILE: src/ddebug.h
================================================
#ifndef DDEBUG_H
#define DDEBUG_H
#include <ngx_config.h>
#include <ngx_core.h>
#if defined(DDEBUG) && (DDEBUG)
# define dd_dump_chain_size() { \
int n; \
ngx_chain_t *cl; \
\
for (n = 0, cl = u->out_bufs; cl; cl = cl->next, n++) { \
} \
\
dd("chain size: %d", n); \
}
# if (NGX_HAVE_VARIADIC_MACROS)
# define dd(...) fprintf(stderr, "drizzle *** %s: ", __func__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, " at %s line %d.\n", __FILE__, __LINE__)
# else
#include <stdarg.h>
#include <stdio.h>
#include <stdarg.h>
static ngx_inline void
dd(const char * fmt, ...) {
}
# endif
#else
# define dd_dump_chain_size()
# if (NGX_HAVE_VARIADIC_MACROS)
# define dd(...)
# else
#include <stdarg.h>
static ngx_inline void
dd(const char * fmt, ...) {
}
# endif
#endif
#if defined(DDEBUG) && (DDEBUG)
#define dd_check_read_event_handler(r) \
dd("r->read_event_handler = %s", \
r->read_event_handler == ngx_http_block_reading ? \
"ngx_http_block_reading" : \
r->read_event_handler == ngx_http_test_reading ? \
"ngx_http_test_reading" : \
r->read_event_handler == ngx_http_request_empty_handler ? \
"ngx_http_request_empty_handler" : "UNKNOWN")
#define dd_check_write_event_handler(r) \
dd("r->write_event_handler = %s", \
r->write_event_handler == ngx_http_handler ? \
"ngx_http_handler" : \
r->write_event_handler == ngx_http_core_run_phases ? \
"ngx_http_core_run_phases" : \
r->write_event_handler == ngx_http_request_empty_handler ? \
"ngx_http_request_empty_handler" : "UNKNOWN")
#else
#define dd_check_read_event_handler(r)
#define dd_check_write_event_handler(r)
#endif
#define dd_drizzle_result(result) \
dd("drizzle result: row_count=%" PRId64 "\n" \
" insert_id=%" PRId64 "\n" \
" warning_count=%u\n" \
" column_count=%u\n\n", \
drizzle_result_row_count(result), \
drizzle_result_insert_id(result), \
drizzle_result_warning_count(result), \
drizzle_result_column_count(result))
#define dd_drizzle_column(column) \
dd("drizzle column: catalog=%s\n" \
" db=%s\n" \
" table=%s\n" \
" org_table=%s\n" \
" name=%s\n" \
" org_name=%s\n" \
" charset=%u\n" \
" size=%u\n" \
" max_size=%zu\n" \
" type=%u\n" \
" flags=%u\n\n", \
drizzle_column_catalog(column), drizzle_column_db(column), \
drizzle_column_table(column), drizzle_column_orig_table(column), \
drizzle_column_name(column), drizzle_column_orig_name(column), \
drizzle_column_charset(column), drizzle_column_size(column), \
drizzle_column_max_size(column), drizzle_column_type(column), \
drizzle_column_flags(column));
#endif /* DDEBUG_H */
================================================
FILE: src/ngx_http_drizzle_checker.c
================================================
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include <ngx_core.h>
#include <ngx_http.h>
#include "ngx_http_drizzle_quoting.h"
ngx_int_t
ngx_http_drizzle_check_int(ngx_str_t *value, void *data)
{
/* TODO */
return NGX_OK;
}
ngx_int_t
ngx_http_drizzle_check_float(ngx_str_t *value, void *data)
{
/* TODO */
return NGX_OK;
}
ngx_int_t
ngx_http_drizzle_check_bool(ngx_str_t *value, void *data)
{
/* TODO */
return NGX_OK;
}
ngx_int_t
ngx_http_drizzle_check_col(ngx_str_t *value, void *data)
{
/* TODO */
return NGX_OK;
}
ngx_int_t
ngx_http_drizzle_check_table(ngx_str_t *value, void *data)
{
/* TODO */
return NGX_OK;
}
ngx_int_t
ngx_http_drizzle_check_keyword(ngx_str_t *value, void *data)
{
/* TODO */
return NGX_OK;
}
================================================
FILE: src/ngx_http_drizzle_checker.h
================================================
#ifndef NGX_HTTP_DRIZZLE_CHECKER_H
#define NGX_HTTP_DRIZZLE_CHECKER_H
#include "ngx_http_drizzle_module.h"
ngx_int_t ngx_http_drizzle_check_int(ngx_str_t *value, void *data);
ngx_int_t ngx_http_drizzle_check_float(ngx_str_t *value, void *data);
ngx_int_t ngx_http_drizzle_check_bool(ngx_str_t *value, void *data);
ngx_int_t ngx_http_drizzle_check_col(ngx_str_t *value, void *data);
ngx_int_t ngx_http_drizzle_check_table(ngx_str_t *value, void *data);
ngx_int_t ngx_http_drizzle_check_keyword(ngx_str_t *value, void *data);
#endif /* NGX_HTTP_DRIZZLE_CHECKER_H */
================================================
FILE: src/ngx_http_drizzle_handler.c
================================================
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_drizzle_module.h"
#include "ngx_http_drizzle_handler.h"
#include "ngx_http_drizzle_processor.h"
#include "ngx_http_drizzle_util.h"
#include "ngx_http_drizzle_upstream.h"
#include "ngx_http_drizzle_keepalive.h"
#ifdef _WIN32
/* import the POLLIN and POLLOUT flags */
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <winsock2.h>
#endif
/* for read/write event handlers */
static ngx_int_t ngx_http_drizzle_create_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_drizzle_reinit_request(ngx_http_request_t *r);
static void ngx_http_drizzle_abort_request(ngx_http_request_t *r);
static void ngx_http_drizzle_finalize_request(ngx_http_request_t *r,
ngx_int_t rc);
static ngx_int_t ngx_http_drizzle_process_header(ngx_http_request_t *r);
static ngx_int_t ngx_http_drizzle_input_filter_init(void *data);
static ngx_int_t ngx_http_drizzle_input_filter(void *data, ssize_t bytes);
ngx_int_t
ngx_http_drizzle_handler(ngx_http_request_t *r)
{
ngx_http_upstream_t *u;
ngx_http_drizzle_loc_conf_t *dlcf;
ngx_http_drizzle_ctx_t *dctx;
ngx_http_core_loc_conf_t *clcf;
ngx_str_t target;
ngx_url_t url;
ngx_connection_t *c;
dd("request: %p", r);
dd("subrequest in memory: %d", (int) r->subrequest_in_memory);
dd("connection: %p", r->connection);
dd("connection log: %p", r->connection->log);
if (r->subrequest_in_memory) {
/* TODO: add support for subrequest in memory by
* emitting output into u->buffer instead */
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
"ngx_http_drizzle_module does not support "
"subrequest in memory");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
dlcf = ngx_http_get_module_loc_conf(r, ngx_http_drizzle_module);
if ((dlcf->default_query == NULL) && !(dlcf->methods_set & r->method)) {
if (dlcf->methods_set != 0) {
return NGX_HTTP_NOT_ALLOWED;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"drizzle: missing \"drizzle_query\" in location \"%V\"",
&clcf->name);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
dd("XXX upstream already exists? %p", r->upstream);
#if defined(nginx_version) && \
((nginx_version >= 7063 && nginx_version < 8000) \
|| nginx_version >= 8007)
dd("creating upstream.......");
if (ngx_http_upstream_create(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
u = r->upstream;
#else /* 0.7.x < 0.7.63, 0.8.x < 0.8.7 */
dd("XXX create upstream");
u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
if (u == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
u->peer.log = r->connection->log;
u->peer.log_error = NGX_ERROR_ERR;
# if (NGX_THREADS)
u->peer.lock = &r->connection->lock;
# endif
r->upstream = u;
#endif
if (dlcf->complex_target) {
/* variables used in the drizzle_pass directive */
if (ngx_http_complex_value(r, dlcf->complex_target, &target)
!= NGX_OK)
{
dd("failed to compile");
return NGX_ERROR;
}
if (target.len == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"drizzle: handler: empty \"drizzle_pass\" target");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
url.host = target;
url.port = 0;
url.no_resolve = 1;
dlcf->upstream.upstream = ngx_http_upstream_drizzle_add(r, &url);
if (dlcf->upstream.upstream == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"drizzle: upstream \"%V\" not found", &target);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
dctx = ngx_pcalloc(r->pool, sizeof(ngx_http_drizzle_ctx_t));
if (dctx == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_http_set_ctx(r, dctx, ngx_http_drizzle_module);
u->schema.len = sizeof("drizzle://") - 1;
u->schema.data = (u_char *) "drizzle://";
u->output.tag = (ngx_buf_tag_t) &ngx_http_drizzle_module;
dd("drizzle tag: %p", (void *) u->output.tag);
u->conf = &dlcf->upstream;
u->create_request = ngx_http_drizzle_create_request;
u->reinit_request = ngx_http_drizzle_reinit_request;
u->process_header = ngx_http_drizzle_process_header;
u->abort_request = ngx_http_drizzle_abort_request;
u->finalize_request = ngx_http_drizzle_finalize_request;
/* we bypass the upstream input filter mechanism in
* ngx_http_upstream_process_headers */
u->input_filter_init = ngx_http_drizzle_input_filter_init;
u->input_filter = ngx_http_drizzle_input_filter;
u->input_filter_ctx = NULL;
#if defined(nginx_version) && nginx_version >= 8011
r->main->count++;
#endif
dd("XXX connect timeout: %d", (int) dlcf->upstream.connect_timeout);
ngx_http_upstream_dbd_init(r);
/* override the read/write event handler to our own */
u->write_event_handler = ngx_http_drizzle_wev_handler;
u->read_event_handler = ngx_http_drizzle_rev_handler;
/* a bit hack-ish way to return error response (clean-up part) */
if ((u->peer.connection) && (u->peer.connection->fd == 0)) {
c = u->peer.connection;
u->peer.connection = NULL;
if (c->write->timer_set) {
ngx_del_timer(c->write);
}
ngx_free_connection(c);
ngx_http_upstream_drizzle_finalize_request(r, u,
#if defined(nginx_version) && (nginx_version >= 8017)
NGX_HTTP_SERVICE_UNAVAILABLE);
#else
dctx->status ? dctx->status : NGX_HTTP_INTERNAL_SERVER_ERROR);
#endif
}
return NGX_DONE;
}
void
ngx_http_drizzle_wev_handler(ngx_http_request_t *r, ngx_http_upstream_t *u)
{
ngx_connection_t *c;
dd("drizzle wev handler");
/* just to ensure u->reinit_request always gets called for
* upstream_next */
u->request_sent = 1;
c = u->peer.connection;
if (c->write->timedout) {
dd("drizzle connection write timeout");
ngx_http_drizzle_set_thread_id_variable(r, u);
ngx_http_upstream_drizzle_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
return;
}
if (ngx_http_upstream_drizzle_test_connect(c) != NGX_OK) {
dd("drizzle connection is broken");
ngx_http_upstream_drizzle_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
return;
}
ngx_http_drizzle_set_libdrizzle_ready(r);
(void) ngx_http_drizzle_process_events(r);
}
void
ngx_http_drizzle_rev_handler(ngx_http_request_t *r, ngx_http_upstream_t *u)
{
ngx_connection_t *c;
dd("drizzle rev handler");
/* just to ensure u->reinit_request always gets called for
* upstream_next */
u->request_sent = 1;
c = u->peer.connection;
if (c->read->timedout) {
dd("drizzle connection read timeout");
ngx_http_drizzle_set_thread_id_variable(r, u);
ngx_http_upstream_drizzle_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
return;
}
if (ngx_http_upstream_drizzle_test_connect(c) != NGX_OK) {
dd("drizzle connection is broken");
ngx_http_upstream_drizzle_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
return;
}
ngx_http_drizzle_set_libdrizzle_ready(r);
(void) ngx_http_drizzle_process_events(r);
}
static ngx_int_t
ngx_http_drizzle_create_request(ngx_http_request_t *r)
{
r->upstream->request_bufs = NULL;
return NGX_OK;
}
static ngx_int_t
ngx_http_drizzle_reinit_request(ngx_http_request_t *r)
{
ngx_http_upstream_t *u;
u = r->upstream;
/* override the read/write event handler to our own */
u->write_event_handler = ngx_http_drizzle_wev_handler;
u->read_event_handler = ngx_http_drizzle_rev_handler;
return NGX_OK;
}
static void
ngx_http_drizzle_abort_request(ngx_http_request_t *r)
{
}
static void
ngx_http_drizzle_finalize_request(ngx_http_request_t *r,
ngx_int_t rc)
{
}
static ngx_int_t
ngx_http_drizzle_process_header(ngx_http_request_t *r)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_drizzle_process_header should not be called"
" by the upstream");
return NGX_ERROR;
}
static ngx_int_t
ngx_http_drizzle_input_filter_init(void *data)
{
ngx_http_request_t *r = data;
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_drizzle_input_filter_init should not be called"
" by the upstream");
return NGX_ERROR;
}
static ngx_int_t
ngx_http_drizzle_input_filter(void *data, ssize_t bytes)
{
ngx_http_request_t *r = data;
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_drizzle_input_filter should not be called"
" by the upstream");
return NGX_ERROR;
}
void
ngx_http_drizzle_set_libdrizzle_ready(ngx_http_request_t *r)
{
ngx_http_upstream_drizzle_peer_data_t *dp;
drizzle_con_st *dc;
#if 1
short revents = 0;
#endif
dp = ngx_http_drizzle_get_peer_data(r);
dc = dp->drizzle_con;
#if 0
/* libdrizzle uses standard poll() event constants
* and depends on drizzle_con_wait() to set them.
* we can directly call drizzle_con_wait() here to
* set those drizzle internal event states, because
* epoll() and other underlying event mechamism used
* by the nginx core can play well enough with poll().
* */
(void) drizzle_con_wait(dc->drizzle);
#endif
#if 1
revents |= POLLOUT;
revents |= POLLIN;
/* drizzle_con_set_revents() isn't declared external in libdrizzle-0.4.0, */
/* so we have to do its job all by ourselves... */
dc->options |= DRIZZLE_CON_IO_READY;
dc->revents = revents;
dc->events &= (short) ~revents;
#endif
}
ngx_int_t
ngx_http_drizzle_status_handler(ngx_http_request_t *r)
{
ngx_http_upstream_main_conf_t *umcf;
ngx_http_upstream_srv_conf_t **uscfp;
ngx_http_upstream_srv_conf_t *uscf;
ngx_uint_t i, n;
ngx_chain_t *cl;
ngx_buf_t *b;
size_t len;
ngx_int_t rc;
ngx_queue_t *q;
ngx_http_drizzle_keepalive_cache_t *item;
ngx_http_upstream_drizzle_srv_conf_t *dscf;
umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
uscfp = umcf->upstreams.elts;
/* calculate the output buffer length */
len = 0;
if (ngx_process == NGX_PROCESS_WORKER) {
len += sizeof("worker process: \n\n") - 1
+ ngx_http_drizzle_get_num_size(ngx_pid);
}
n = 0;
for (i = 0; i < umcf->upstreams.nelts; i++) {
uscf = uscfp[i];
if (uscf->srv_conf == NULL) {
/* skip implicit upstream specified directly by the fastcgi_pass,
* proxy_pass, and similar directives */
continue;
}
dscf = ngx_http_conf_upstream_srv_conf(uscf, ngx_http_drizzle_module);
if (dscf == NULL || dscf->servers == NULL) {
continue;
}
if (n != 0) {
len += sizeof("\n") - 1;
}
n++;
len += sizeof("upstream \n") - 1
+ uscf->host.len
+ sizeof(" active connections: \n") - 1
+ ngx_http_drizzle_get_num_size(dscf->active_conns)
+ sizeof(" connection pool capacity: \n") - 1
+ ngx_http_drizzle_get_num_size(dscf->max_cached);
if (dscf->max_cached) {
/* dump overflow flag for the connection pool */
switch (dscf->overflow) {
case drizzle_keepalive_overflow_ignore:
len += sizeof(" overflow: ignore\n") - 1;
break;
case drizzle_keepalive_overflow_reject:
len += sizeof(" overflow: reject\n") - 1;
break;
default:
len += sizeof(" overflow: N/A\n") - 1;
break;
}
/* dump the lengths of the "cache" and "free" queues in the pool */
len += sizeof(" cached connection queue: \n") - 1
+ ngx_http_drizzle_get_num_size(
ngx_http_drizzle_queue_size(&dscf->cache)
)
+ sizeof(" free'd connection queue: \n") - 1
+ ngx_http_drizzle_get_num_size(
ngx_http_drizzle_queue_size(&dscf->free)
)
/* dump how many times that each individual connection in the
* pool has been successfully used in the "cache" queue */
+ sizeof(" cached connection successfully used count:\n") - 1;
for (q = ngx_queue_head(&dscf->cache);
q != ngx_queue_sentinel(&dscf->cache);
q = ngx_queue_next(q))
{
item = ngx_queue_data(q, ngx_http_drizzle_keepalive_cache_t,
queue);
len += sizeof(" ") - 1
+ ngx_http_drizzle_get_num_size(item->used);
}
/* dump how many times that each individual connection in the
* pool has been successfully used in the "free" queue */
len += sizeof(" free'd connection successfully used count:\n") - 1;
for (q = ngx_queue_head(&dscf->free);
q != ngx_queue_sentinel(&dscf->free);
q = ngx_queue_next(q))
{
item = ngx_queue_data(q, ngx_http_drizzle_keepalive_cache_t,
queue);
len += sizeof(" ") - 1
+ ngx_http_drizzle_get_num_size(item->used);
}
}
len += sizeof(" servers: \n") - 1
+ ngx_http_drizzle_get_num_size(dscf->servers->nelts)
+ sizeof(" peers: \n") - 1
+ ngx_http_drizzle_get_num_size(dscf->peers->number);
}
/* allocate the output buffer */
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* fill in the output buffer with the actual data */
if (ngx_process == NGX_PROCESS_WORKER) {
b->last = ngx_sprintf(b->last, "worker process: %P\n\n", ngx_pid);
}
n = 0;
for (i = 0; i < umcf->upstreams.nelts; i++) {
uscf = uscfp[i];
if (uscf->srv_conf == NULL) {
/* skip implicit upstream specified directly by the fastcgi_pass,
* proxy_pass, and similar directives */
continue;
}
dscf = ngx_http_conf_upstream_srv_conf(uscf, ngx_http_drizzle_module);
if (dscf == NULL || dscf->servers == NULL) {
continue;
}
if (n != 0) {
*b->last++ = '\n';
}
n++;
b->last = ngx_copy_const_str(b->last, "upstream ");
b->last = ngx_copy(b->last, uscf->host.data, uscf->host.len);
*b->last++ = '\n';
b->last = ngx_sprintf(b->last, " active connections: %uD\n",
dscf->active_conns);
b->last = ngx_sprintf(b->last, " connection pool capacity: %uD\n",
dscf->max_cached);
if (dscf->max_cached) {
/* dump overflow flag for the connection pool */
switch (dscf->overflow) {
case drizzle_keepalive_overflow_ignore:
b->last = ngx_copy_const_str(b->last,
" overflow: ignore\n");
break;
case drizzle_keepalive_overflow_reject:
b->last = ngx_copy_const_str(b->last,
" overflow: reject\n");
break;
default:
b->last = ngx_copy_const_str(b->last, " overflow: N/A\n");
break;
}
/* dump the lengths of the "cache" and "free" queues in the pool */
b->last = ngx_sprintf(b->last, " cached connection queue: %uD\n",
ngx_http_drizzle_queue_size(&dscf->cache));
b->last = ngx_sprintf(b->last, " free'd connection queue: %uD\n",
ngx_http_drizzle_queue_size(&dscf->free));
/* dump how many times that each individual connection in the
* pool has been successfully used in the "cache" queue */
b->last = ngx_copy_const_str(b->last, " cached connection "
"successfully used count:");
for (q = ngx_queue_head(&dscf->cache);
q != ngx_queue_sentinel(&dscf->cache);
q = ngx_queue_next(q))
{
item = ngx_queue_data(q, ngx_http_drizzle_keepalive_cache_t,
queue);
b->last = ngx_sprintf(b->last, " %uD", item->used);
}
*b->last++ = '\n';
/* dump how many times that each individual connection in the
* pool has been successfully used in the "free" queue */
b->last = ngx_copy_const_str(b->last, " free'd connection "
"successfully used count:");
for (q = ngx_queue_head(&dscf->free);
q != ngx_queue_sentinel(&dscf->free);
q = ngx_queue_next(q))
{
item = ngx_queue_data(q, ngx_http_drizzle_keepalive_cache_t,
queue);
b->last = ngx_sprintf(b->last, " %uD", item->used);
}
*b->last++ = '\n';
}
b->last = ngx_sprintf(b->last, " servers: %uD\n",
dscf->servers->nelts);
b->last = ngx_sprintf(b->last, " peers: %uD\n", dscf->peers->number);
}
if (b->last != b->end) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
"drizzle_status output buffer error: %O != %O",
(off_t) (b->last - b->pos),
(off_t) (b->end - b->pos));
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (r == r->main) {
b->last_buf = 1;
}
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
cl->buf = b;
cl->next = NULL;
r->headers_out.status = NGX_HTTP_OK;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
return ngx_http_output_filter(r, cl);
}
================================================
FILE: src/ngx_http_drizzle_handler.h
================================================
#ifndef NGX_HTTP_DRIZZLE_HANDLER_H
#define NGX_HTTP_DRIZZLE_HANDLER_H
#include <ngx_core.h>
#include <ngx_http.h>
void ngx_http_drizzle_set_libdrizzle_ready(ngx_http_request_t *r);
ngx_int_t ngx_http_drizzle_handler(ngx_http_request_t *r);
void ngx_http_drizzle_rev_handler(ngx_http_request_t *r,
ngx_http_upstream_t *u);
void ngx_http_drizzle_wev_handler(ngx_http_request_t *r,
ngx_http_upstream_t *u);
ngx_int_t ngx_http_drizzle_status_handler(ngx_http_request_t *r);
#endif /* NGX_HTTP_DRIZZLE_HANDLER_H */
================================================
FILE: src/ngx_http_drizzle_keepalive.c
================================================
/*
* Copyright (C) Xiaozhe Wang (chaoslawful)
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_drizzle_keepalive.h"
#include "ngx_http_drizzle_util.h"
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
static void ngx_http_drizzle_keepalive_dummy_handler(ngx_event_t *ev);
static void ngx_http_drizzle_keepalive_close_handler(ngx_event_t *ev);
char *
ngx_http_upstream_drizzle_keepalive(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
ngx_http_upstream_drizzle_srv_conf_t *dscf = conf;
ngx_str_t *value;
ngx_uint_t i;
ngx_int_t n;
u_char *data;
ngx_uint_t len;
if (dscf->max_cached) {
return "is duplicate";
}
value = cf->args->elts;
for (i = 1; i < cf->args->nelts; i++) {
if (ngx_http_drizzle_strcmp_const(value[i].data, "max=")
== 0)
{
len = value[i].len - (sizeof("max=") - 1);
data = &value[i].data[sizeof("max=") - 1];
n = ngx_atoi(data, len);
if (n == NGX_ERROR || n < 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid \"max\" value \"%V\" "
"in \"%V\" directive",
&value[i], &cmd->name);
return NGX_CONF_ERROR;
}
dscf->max_cached = n;
continue;
}
if (ngx_http_drizzle_strcmp_const(value[i].data, "mode=") == 0) {
len = value[i].len - (sizeof("mode=") - 1);
data = &value[i].data[sizeof("mode=") - 1];
switch (len) {
case 6:
if (ngx_http_drizzle_strcmp_const(data, "single") == 0) {
dscf->single = 1;
}
break;
case 5:
if (ngx_http_drizzle_strcmp_const(data, "multi") == 0) {
dscf->single = 0;
}
break;
default:
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"drizzle: invalid \"mode\" value \"%V\" "
"in \"%V\" directive",
&value[i], &cmd->name);
return NGX_CONF_ERROR;
}
continue;
}
if (ngx_http_drizzle_strcmp_const(value[i].data, "overflow=") == 0) {
len = value[i].len - (sizeof("overflow=") - 1);
data = &value[i].data[sizeof("overflow=") - 1];
switch (len) {
case 6:
if (ngx_http_drizzle_strcmp_const(data, "reject") == 0) {
dscf->overflow = drizzle_keepalive_overflow_reject;
} else if (ngx_http_drizzle_strcmp_const(data, "ignore") == 0) {
dscf->overflow = drizzle_keepalive_overflow_ignore;
}
break;
default:
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"drizzle: invalid \"overflow\" value \"%V\" "
"in \"%V\" directive",
&value[i], &cmd->name);
return NGX_CONF_ERROR;
}
continue;
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"drizzle: invalid parameter \"%V\" in"
" \"%V\" directive",
&value[i], &cmd->name);
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
ngx_int_t
ngx_http_drizzle_keepalive_init(ngx_pool_t *pool,
ngx_http_upstream_drizzle_srv_conf_t *dscf)
{
ngx_uint_t i;
ngx_http_drizzle_keepalive_cache_t *cached;
/* allocate cache items and add to free queue */
cached = ngx_pcalloc(pool,
sizeof(ngx_http_drizzle_keepalive_cache_t)
* dscf->max_cached);
if (cached == NULL) {
return NGX_ERROR;
}
ngx_queue_init(&dscf->cache);
ngx_queue_init(&dscf->free);
for (i = 0; i < dscf->max_cached; i++) {
ngx_queue_insert_head(&dscf->free, &cached[i].queue);
cached[i].srv_conf = dscf;
}
return NGX_OK;
}
ngx_int_t
ngx_http_drizzle_keepalive_get_peer_single(ngx_peer_connection_t *pc,
ngx_http_upstream_drizzle_peer_data_t *dp,
ngx_http_upstream_drizzle_srv_conf_t *dscf)
{
ngx_http_drizzle_keepalive_cache_t *item;
ngx_queue_t *q;
ngx_connection_t *c;
if (!ngx_queue_empty(&dscf->cache)) {
dd("getting cached mysql connection...");
q = ngx_queue_head(&dscf->cache);
ngx_queue_remove(q);
item = ngx_queue_data(q, ngx_http_drizzle_keepalive_cache_t, queue);
c = item->connection;
ngx_queue_insert_head(&dscf->free, q);
c->idle = 0;
c->log = pc->log;
c->read->log = pc->log;
c->write->log = pc->log;
dp->name.data = item->name.data;
dp->name.len = item->name.len;
dp->sockaddr = item->sockaddr;
dp->drizzle_con = item->drizzle_con;
dp->has_set_names = item->has_set_names;
dp->used = item->used;
pc->connection = c;
pc->cached = 1;
pc->name = &dp->name;
pc->sockaddr = &dp->sockaddr;
pc->socklen = item->socklen;
return NGX_DONE;
}
return NGX_DECLINED;
}
ngx_int_t
ngx_http_drizzle_keepalive_get_peer_multi(ngx_peer_connection_t *pc,
ngx_http_upstream_drizzle_peer_data_t *dp,
ngx_http_upstream_drizzle_srv_conf_t *dscf)
{
ngx_queue_t *q, *cache;
ngx_http_drizzle_keepalive_cache_t *item;
ngx_connection_t *c;
/* search cache for suitable connection */
cache = &dscf->cache;
for (q = ngx_queue_head(cache);
q != ngx_queue_sentinel(cache);
q = ngx_queue_next(q))
{
item = ngx_queue_data(q, ngx_http_drizzle_keepalive_cache_t, queue);
c = item->connection;
/* XXX maybe we should take dbname and user into account
* as well? */
if (ngx_memn2cmp((u_char *) &item->sockaddr, (u_char *) pc->sockaddr,
item->socklen, pc->socklen)
== 0)
{
ngx_queue_remove(q);
ngx_queue_insert_head(&dscf->free, q);
c->idle = 0;
c->log = pc->log;
c->read->log = pc->log;
c->write->log = pc->log;
pc->connection = c;
pc->cached = 1;
/* we do not need to resume dp->name here because
* it already takes the right value in the
* ngx_http_upstream_drizzle_get_peer function */
dp->drizzle_con = item->drizzle_con;
dp->has_set_names = item->has_set_names;
dp->used = item->used;
return NGX_DONE;
}
}
return NGX_DECLINED;
}
void
ngx_http_drizzle_keepalive_free_peer(ngx_peer_connection_t *pc,
ngx_http_upstream_drizzle_peer_data_t *dp,
ngx_http_upstream_drizzle_srv_conf_t *dscf, ngx_uint_t state)
{
ngx_uint_t status;
ngx_http_drizzle_keepalive_cache_t *item;
ngx_queue_t *q;
ngx_connection_t *c;
ngx_http_upstream_t *u;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"drizzle: free keepalive peer");
if (state & NGX_PEER_FAILED) {
dp->failed = 1;
}
u = dp->upstream;
status = u->headers_in.status_n;
dd("dp failed: %d", (int) dp->failed);
dd("pc->connection: %p", pc->connection);
dd("status = %d", (int) status);
if (!dp->failed
&& pc->connection != NULL
&& (status == NGX_HTTP_NOT_FOUND
|| status == NGX_HTTP_GONE
|| (status == NGX_HTTP_OK && u->header_sent && u->length == 0)))
{
c = pc->connection;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"drizzle: free keepalive peer: saving connection %p",
c);
if (ngx_queue_empty(&dscf->free)) {
/* connection pool is already full */
dd("caching connection forcibly and the pool is already full");
q = ngx_queue_last(&dscf->cache);
ngx_queue_remove(q);
item = ngx_queue_data(q, ngx_http_drizzle_keepalive_cache_t,
queue);
ngx_http_upstream_drizzle_free_connection(pc->log, item->connection,
item->drizzle_con, dscf);
} else {
dd("caching idle connection to the pool");
q = ngx_queue_head(&dscf->free);
ngx_queue_remove(q);
item = ngx_queue_data(q, ngx_http_drizzle_keepalive_cache_t,
queue);
}
item->connection = c;
ngx_queue_insert_head(&dscf->cache, q);
pc->connection = NULL;
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
if (c->write->timer_set) {
ngx_del_timer(c->write);
}
c->write->handler = ngx_http_drizzle_keepalive_dummy_handler;
c->read->handler = ngx_http_drizzle_keepalive_close_handler;
c->data = item;
c->idle = 1;
c->log = ngx_cycle->log;
c->read->log = ngx_cycle->log;
c->write->log = ngx_cycle->log;
item->socklen = pc->socklen;
ngx_memcpy(&item->sockaddr, pc->sockaddr, pc->socklen);
item->drizzle_con = dp->drizzle_con;
item->has_set_names = dp->has_set_names;
item->name.data = dp->name.data;
item->name.len = dp->name.len;
item->used = ++dp->used;
}
}
static void
ngx_http_drizzle_keepalive_dummy_handler(ngx_event_t *ev)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
"drizzle: keepalive dummy handler");
}
static void
ngx_http_drizzle_keepalive_close_handler(ngx_event_t *ev)
{
ngx_http_upstream_drizzle_srv_conf_t *dscf;
ngx_http_drizzle_keepalive_cache_t *item;
int n;
char buf[1];
ngx_connection_t *c;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
"drizzle: keepalive close handler");
c = ev->data;
if (c->close) {
goto close;
}
n = recv(c->fd, buf, 1, MSG_PEEK);
if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
/* stale event */
#if 0
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
goto close;
}
#endif
return;
}
close:
item = c->data;
dscf = item->srv_conf;
dd("closing fd %d", c->fd);
ngx_http_upstream_drizzle_free_connection(ev->log, c, item->drizzle_con,
dscf);
ngx_queue_remove(&item->queue);
ngx_queue_insert_head(&dscf->free, &item->queue);
}
================================================
FILE: src/ngx_http_drizzle_keepalive.h
================================================
#ifndef NGX_HTTP_DRIZZLE_KEEPALIVE_H
#define NGX_HTTP_DRIZZLE_KEEPALIVE_H
#include "ngx_http_drizzle_upstream.h"
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
typedef struct {
ngx_http_upstream_drizzle_srv_conf_t *srv_conf;
ngx_queue_t queue;
ngx_connection_t *connection;
socklen_t socklen;
struct sockaddr sockaddr;
drizzle_con_st *drizzle_con;
ngx_str_t name;
/* how many times this connection has been successfully used */
ngx_uint_t used;
unsigned has_set_names:1;
} ngx_http_drizzle_keepalive_cache_t;
char *ngx_http_upstream_drizzle_keepalive(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
ngx_int_t ngx_http_drizzle_keepalive_init(ngx_pool_t *pool,
ngx_http_upstream_drizzle_srv_conf_t *dscf);
ngx_int_t ngx_http_drizzle_keepalive_get_peer_single(ngx_peer_connection_t *pc,
ngx_http_upstream_drizzle_peer_data_t *dp,
ngx_http_upstream_drizzle_srv_conf_t *dscf);
ngx_int_t ngx_http_drizzle_keepalive_get_peer_multi(ngx_peer_connection_t *pc,
ngx_http_upstream_drizzle_peer_data_t *dp,
ngx_http_upstream_drizzle_srv_conf_t *dscf);
void ngx_http_drizzle_keepalive_free_peer(ngx_peer_connection_t *pc,
ngx_http_upstream_drizzle_peer_data_t *dp,
ngx_http_upstream_drizzle_srv_conf_t *dscf,
ngx_uint_t state);
#endif /* NGX_HTTP_DRIZZLE_KEEPALIVE_H */
================================================
FILE: src/ngx_http_drizzle_module.c
================================================
/*
* Copyright (C) Xiaozhe Wang (chaoslawful)
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_drizzle_module.h"
#include "ngx_http_drizzle_handler.h"
#include "ngx_http_drizzle_upstream.h"
#include "ngx_http_drizzle_keepalive.h"
static ngx_str_t ngx_http_drizzle_tid_var_name =
ngx_string("drizzle_thread_id");
/* Forward declaration */
static char *ngx_http_drizzle_set_complex_value_slot(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_http_drizzle_query(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_drizzle_pass(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static void *ngx_http_drizzle_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_drizzle_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
static ngx_int_t ngx_http_drizzle_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_http_drizzle_tid_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static char *ngx_http_drizzle_enable_status(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static ngx_http_variable_t ngx_http_drizzle_variables[] = {
{ ngx_string("drizzle_thread_id"), NULL,
ngx_http_drizzle_tid_variable, 0,
NGX_HTTP_VAR_CHANGEABLE, 0 },
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
/* config directives for module drizzle */
static ngx_command_t ngx_http_drizzle_cmds[] = {
{ ngx_string("drizzle_server"),
NGX_HTTP_UPS_CONF|NGX_CONF_1MORE,
ngx_http_upstream_drizzle_server,
NGX_HTTP_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("drizzle_keepalive"),
NGX_HTTP_UPS_CONF|NGX_CONF_1MORE,
ngx_http_upstream_drizzle_keepalive,
NGX_HTTP_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("drizzle_query"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|NGX_HTTP_LIF_CONF|NGX_CONF_1MORE,
ngx_http_drizzle_query,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("drizzle_dbname"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_http_drizzle_set_complex_value_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_drizzle_loc_conf_t, dbname),
NULL },
{ ngx_string("drizzle_pass"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_http_drizzle_pass,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("drizzle_connect_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_drizzle_loc_conf_t, upstream.connect_timeout),
NULL },
{ ngx_string("drizzle_send_query_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_drizzle_loc_conf_t, upstream.send_timeout),
NULL },
{ ngx_string("drizzle_recv_cols_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_drizzle_loc_conf_t, recv_cols_timeout),
NULL },
{ ngx_string("drizzle_recv_rows_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_drizzle_loc_conf_t, recv_rows_timeout),
NULL },
{ ngx_string("drizzle_module_header"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|NGX_HTTP_LIF_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_drizzle_loc_conf_t, enable_module_header),
NULL },
{ ngx_string("drizzle_buffer_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_drizzle_loc_conf_t, buf_size),
NULL },
{ ngx_string("drizzle_status"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_NOARGS,
ngx_http_drizzle_enable_status,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
/* Nginx HTTP subsystem module hooks */
static ngx_http_module_t ngx_http_drizzle_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create_main_conf */
NULL, /* merge_main_conf */
ngx_http_upstream_drizzle_create_srv_conf,
/* create_srv_conf */
NULL, /* merge_srv_conf */
ngx_http_drizzle_create_loc_conf, /* create_loc_conf */
ngx_http_drizzle_merge_loc_conf /* merge_loc_conf */
};
ngx_module_t ngx_http_drizzle_module = {
NGX_MODULE_V1,
&ngx_http_drizzle_module_ctx, /* module context */
ngx_http_drizzle_cmds, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
ngx_drizzle_http_method_t ngx_drizzle_http_methods[] = {
{ (u_char *) "GET", (uint32_t) NGX_HTTP_GET },
{ (u_char *) "HEAD", (uint32_t) NGX_HTTP_HEAD },
{ (u_char *) "POST", (uint32_t) NGX_HTTP_POST },
{ (u_char *) "PUT", (uint32_t) NGX_HTTP_PUT },
{ (u_char *) "DELETE", (uint32_t) NGX_HTTP_DELETE },
{ (u_char *) "MKCOL", (uint32_t) NGX_HTTP_MKCOL },
{ (u_char *) "COPY", (uint32_t) NGX_HTTP_COPY },
{ (u_char *) "MOVE", (uint32_t) NGX_HTTP_MOVE },
{ (u_char *) "OPTIONS", (uint32_t) NGX_HTTP_OPTIONS },
{ (u_char *) "PROPFIND" , (uint32_t) NGX_HTTP_PROPFIND },
{ (u_char *) "PROPPATCH", (uint32_t) NGX_HTTP_PROPPATCH },
{ (u_char *) "LOCK", (uint32_t) NGX_HTTP_LOCK },
{ (u_char *) "UNLOCK", (uint32_t) NGX_HTTP_UNLOCK },
#if defined(nginx_version) && (nginx_version >= 8041)
{ (u_char *) "PATCH", (uint32_t) NGX_HTTP_PATCH },
#endif
{ NULL, 0 }
};
static void *
ngx_http_drizzle_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_drizzle_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_drizzle_loc_conf_t));
if (conf == NULL) {
return NULL;
}
conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
conf->recv_cols_timeout = NGX_CONF_UNSET_MSEC;
conf->recv_rows_timeout = NGX_CONF_UNSET_MSEC;
conf->enable_module_header = NGX_CONF_UNSET;
/* the hardcoded values */
conf->upstream.cyclic_temp_file = 0;
conf->upstream.buffering = 0;
conf->upstream.ignore_client_abort = 0;
conf->upstream.send_lowat = 0;
conf->upstream.bufs.num = 0;
conf->upstream.busy_buffers_size = 0;
conf->upstream.max_temp_file_size = 0;
conf->upstream.temp_file_write_size = 0;
conf->upstream.intercept_errors = 1;
conf->upstream.intercept_404 = 1;
conf->upstream.pass_request_headers = 0;
conf->upstream.pass_request_body = 0;
/* set by ngx_pcalloc:
* conf->dbname = NULL
* conf->query = NULL
*/
conf->complex_target = NGX_CONF_UNSET_PTR;
conf->buf_size = NGX_CONF_UNSET_SIZE;
conf->tid_var_index = NGX_CONF_UNSET;
return conf;
}
static char *
ngx_http_drizzle_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_drizzle_loc_conf_t *prev = parent;
ngx_http_drizzle_loc_conf_t *conf = child;
ngx_conf_merge_value(conf->enable_module_header,
prev->enable_module_header, 1);
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);
ngx_conf_merge_msec_value(conf->upstream.send_timeout,
prev->upstream.send_timeout, 60000);
ngx_conf_merge_msec_value(conf->recv_cols_timeout,
prev->recv_cols_timeout, 60000);
ngx_conf_merge_msec_value(conf->recv_rows_timeout,
prev->recv_rows_timeout, 60000);
if (conf->dbname == NULL) {
conf->dbname = prev->dbname;
}
if ((conf->default_query == NULL) && (conf->queries == NULL)) {
conf->default_query = prev->default_query;
conf->methods_set = prev->methods_set;
conf->queries = prev->queries;
}
ngx_conf_merge_size_value(conf->buf_size, prev->buf_size,
(size_t) ngx_pagesize);
if (conf->tid_var_index == NGX_CONF_UNSET) {
conf->tid_var_index = prev->tid_var_index;
}
return NGX_CONF_OK;
}
static char *
ngx_http_drizzle_set_complex_value_slot(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
char *p = conf;
ngx_http_complex_value_t **field;
ngx_str_t *value;
ngx_http_compile_complex_value_t ccv;
field = (ngx_http_complex_value_t **) (p + cmd->offset);
if (*field) {
return "is duplicate";
}
*field = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
if (*field == NULL) {
return NGX_CONF_ERROR;
}
value = cf->args->elts;
if (value[1].len == 0) {
ngx_memzero(*field, sizeof(ngx_http_complex_value_t));
return NGX_OK;
}
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[1];
ccv.complex_value = *field;
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
char *
ngx_http_drizzle_query(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value = cf->args->elts;
ngx_str_t sql = value[cf->args->nelts - 1];
ngx_http_drizzle_loc_conf_t *dlcf = conf;
ngx_http_compile_complex_value_t ccv;
ngx_drizzle_mixed_t *query;
ngx_drizzle_http_method_t *method;
ngx_uint_t methods, i;
if (sql.len == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"drizzle: empty value in \"%V\" directive",
&cmd->name);
return NGX_CONF_ERROR;
}
if (cf->args->nelts == 2) {
/* default query */
dd("default query");
if (dlcf->default_query != NULL) {
return "is duplicate";
}
dlcf->default_query = ngx_pcalloc(cf->pool,
sizeof(ngx_drizzle_mixed_t));
if (dlcf->default_query == NULL) {
return NGX_CONF_ERROR;
}
methods = 0xFFFF;
query = dlcf->default_query;
} else {
/* method-specific query */
dd("method-specific query");
methods = 0;
for (i = 1; i < cf->args->nelts - 1; i++) {
for (method = ngx_drizzle_http_methods; method->name; method++) {
if (ngx_strcasecmp(value[i].data, method->name) == 0) {
/* correct method name */
if (dlcf->methods_set & method->key) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"drizzle: \"%V\" directive"
" for method \"%V\" is duplicate",
&cmd->name, &value[i]);
return NGX_CONF_ERROR;
}
methods |= method->key;
goto next;
}
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"drizzle: invalid method \"%V\"", &value[i]);
return NGX_CONF_ERROR;
next:
continue;
}
if (dlcf->queries == NULL) {
dlcf->queries = ngx_array_create(cf->pool, 4,
sizeof(ngx_drizzle_mixed_t));
if (dlcf->queries == NULL) {
return NGX_CONF_ERROR;
}
}
query = ngx_array_push(dlcf->queries);
if (query == NULL) {
return NGX_CONF_ERROR;
}
ngx_memzero(query, sizeof(ngx_drizzle_mixed_t));
dlcf->methods_set |= methods;
}
if (ngx_http_script_variables_count(&sql)) {
/* complex value */
dd("complex value");
query->key = methods;
query->cv = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
if (query->cv == NULL) {
return NGX_CONF_ERROR;
}
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &sql;
ccv.complex_value = query->cv;
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
} else {
/* simple value */
dd("simple value");
query->key = methods;
query->sv = sql;
}
return NGX_CONF_OK;
}
static char *
ngx_http_drizzle_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_drizzle_loc_conf_t *dlcf = conf;
ngx_http_core_loc_conf_t *clcf;
ngx_str_t *value;
ngx_http_compile_complex_value_t ccv;
ngx_url_t url;
ngx_uint_t n;
if (dlcf->upstream.upstream) {
return "is duplicate";
}
if (ngx_http_drizzle_add_variables(cf) != NGX_OK) {
return NGX_CONF_ERROR;
}
dlcf->tid_var_index = ngx_http_get_variable_index(cf,
&ngx_http_drizzle_tid_var_name);
if (dlcf->tid_var_index == NGX_ERROR) {
return NGX_CONF_ERROR;
}
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_drizzle_handler;
if (clcf->name.data[clcf->name.len - 1] == '/') {
clcf->auto_redirect = 1;
}
value = cf->args->elts;
n = ngx_http_script_variables_count(&value[1]);
if (n) {
dlcf->complex_target = ngx_palloc(cf->pool,
sizeof(ngx_http_complex_value_t));
if (dlcf->complex_target == NULL) {
return NGX_CONF_ERROR;
}
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
ccv.cf = cf;
ccv.value = &value[1];
ccv.complex_value = dlcf->complex_target;
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
dlcf->complex_target = NULL;
ngx_memzero(&url, sizeof(ngx_url_t));
url.url = value[1];
url.no_resolve = 1;
dlcf->upstream.upstream = ngx_http_upstream_add(cf, &url, 0);
if (dlcf->upstream.upstream == NULL) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_drizzle_add_variables(ngx_conf_t *cf)
{
ngx_http_variable_t *var, *v;
for (v = ngx_http_drizzle_variables; v->name.len; v++) {
var = ngx_http_add_variable(cf, &v->name, v->flags);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_drizzle_tid_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->len = 0;
v->data = (u_char *) "";
return NGX_OK;
}
static char *
ngx_http_drizzle_enable_status(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_drizzle_status_handler;
return NGX_CONF_OK;
}
================================================
FILE: src/ngx_http_drizzle_module.h
================================================
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef NGX_HTTP_DRIZZLE_MODULE_H
#define NGX_HTTP_DRIZZLE_MODULE_H
#include <ngx_config.h>
#include <nginx.h>
#include <ngx_http.h>
/* XXX nginx undefines "bool", which breaks the libdrizzle 1.0 API
* which makes use of "bool" */
#if defined(__GNUC__)
# ifndef bool
# define bool _Bool
# endif
#endif
#include <libdrizzle/drizzle_client.h>
#ifdef _WIN32
/* remove the bad macros defined in libdrizzle/drizzle.h */
# undef close
# undef snprintf
#endif
#ifndef NGX_HTTP_GONE
#define NGX_HTTP_GONE 410
#endif
#define ngx_http_drizzle_module_version 1012
#define ngx_http_drizzle_module_version_string "0.1.12"
extern ngx_module_t ngx_http_drizzle_module;
typedef struct {
ngx_uint_t key;
ngx_str_t sv;
ngx_http_complex_value_t *cv;
} ngx_drizzle_mixed_t;
typedef struct {
u_char *name;
uint32_t key;
} ngx_drizzle_http_method_t;
typedef struct {
ngx_http_upstream_conf_t upstream;
/* drizzle database name */
ngx_http_complex_value_t *dbname;
/* SQL query to be executed */
ngx_drizzle_mixed_t *default_query;
ngx_uint_t methods_set;
ngx_array_t *queries;
ngx_msec_t recv_cols_timeout;
ngx_msec_t recv_rows_timeout;
ngx_flag_t enable_module_header;
/* for quoting */
ngx_array_t *vars_to_quote;
/* of ngx_http_drizzle_var_to_quote_t */
ngx_array_t *user_types;
/* of ngx_http_drizzle_var_type_t */
ngx_http_complex_value_t *complex_target;
size_t buf_size;
ngx_int_t tid_var_index; /* thread id variable
index */
} ngx_http_drizzle_loc_conf_t;
typedef struct {
#if defined(nginx_version) && (nginx_version < 8017)
ngx_int_t status;
#endif
/* original drizzle peer data; saved here so handlers can find it
* even when r->upstream->peer.data has been wrapped by another
* module such as the standard upstream keepalive (which became
* implicit for explicit upstreams in nginx 1.29.7) */
void *peer_data;
} ngx_http_drizzle_ctx_t;
/* states for the drizzle client state machine */
typedef enum {
state_db_connect,
state_db_send_query,
state_db_recv_cols,
state_db_recv_rows,
state_db_idle
} ngx_http_drizzle_state_t;
#endif /* NGX_HTTP_DRIZZLE_MODULE_H */
================================================
FILE: src/ngx_http_drizzle_output.c
================================================
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_drizzle_module.h"
#include "ngx_http_drizzle_output.h"
#include "ngx_http_drizzle_processor.h"
#include "ngx_http_drizzle_util.h"
#include "resty_dbd_stream.h"
#include <nginx.h>
#define ngx_http_drizzle_module_header_key "X-Resty-DBD-Module"
#define ngx_http_drizzle_module_header_key_len \
(sizeof(ngx_http_drizzle_module_header_key) - 1)
#define ngx_http_drizzle_module_header_val \
"ngx_drizzle " \
ngx_http_drizzle_module_version_string
#define ngx_http_drizzle_module_header_val_len \
(sizeof(ngx_http_drizzle_module_header_val) - 1)
#define ngx_http_drizzle_module_header_key_len \
(sizeof(ngx_http_drizzle_module_header_key) - 1)
static ngx_int_t ngx_http_drizzle_get_buf(ngx_http_request_t *r,
ngx_http_upstream_drizzle_peer_data_t *dp);
static u_char *ngx_http_drizzle_get_postponed(ngx_http_request_t *r,
ngx_http_upstream_drizzle_peer_data_t *dp, size_t len);
static rds_col_type_t ngx_http_drizzle_std_col_type(
drizzle_column_type_t col_type);
static u_char *ngx_http_drizzle_request_mem(ngx_http_request_t *r,
ngx_http_upstream_drizzle_peer_data_t *dp, size_t len);
static ngx_int_t ngx_http_drizzle_submit_mem(ngx_http_request_t *r,
ngx_http_upstream_drizzle_peer_data_t *dp, size_t len);
ngx_int_t
ngx_http_drizzle_output_result_header(ngx_http_request_t *r,
drizzle_result_st *res)
{
u_char *pos, *last;
ngx_int_t rc;
ngx_http_upstream_t *u = r->upstream;
const char *errstr;
size_t size;
uint16_t errstr_len;
uint16_t col_count;
uint16_t errcode;
ngx_http_upstream_drizzle_peer_data_t *dp = ngx_http_drizzle_get_peer_data(r);
errcode = drizzle_result_error_code(res);
if (dp->enable_charset && ! dp->has_set_names) {
if (errcode != 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"drizzle: FATAL: failed to set names 'utf8' "
"(error %d)", (int) errcode);
return NGX_ERROR;
}
if (dp->drizzle_con && dp->drizzle_res.con) {
dd("before drizzle result free");
dd("%p vs. %p", dp->drizzle_res.con, dp->drizzle_con);
drizzle_result_free(&dp->drizzle_res);
dd("after drizzle result free");
}
/* ngx_http_upstream_drizzle_done(r, u, dp, NGX_OK); */
dd("returning DONE when set names");
return NGX_DONE;
}
errstr = drizzle_result_error(res);
errstr_len = (uint16_t) ngx_strlen(errstr);
col_count = drizzle_result_column_count(res);
size = sizeof(uint8_t) /* endian type */
+ sizeof(uint32_t) /* format version */
+ sizeof(uint8_t) /* result type */
+ sizeof(uint16_t) /* standard error code */
+ sizeof(uint16_t) /* driver-specific error code */
+ sizeof(uint16_t) /* driver-specific errstr len */
+ errstr_len /* driver-specific errstr data */
+ sizeof(uint64_t) /* rows affected */
+ sizeof(uint64_t) /* insert id */
+ sizeof(uint16_t); /* column count */
pos = ngx_http_drizzle_request_mem(r, dp, size);
if (pos == NULL) {
return NGX_ERROR;
}
last = pos;
#if NGX_HAVE_LITTLE_ENDIAN
*last++ = 0;
#else /* big endian */
*last++ = 1;
#endif
/* RDS format version */
*(uint32_t *) last = (uint32_t) resty_dbd_stream_version;
last += sizeof(uint32_t);
/* result type fixed to 0 */
*last++ = 0;
/* standard error code
* FIXME: define the standard error code set and map
* libdrizzle's to it. */
*(uint16_t *) last = errcode;
last += sizeof(uint16_t);
/* driver-specific error code */
*(uint16_t *) last = drizzle_result_error_code(res);
last += sizeof(uint16_t);
/* driver-specific errstr len */
*(uint16_t *) last = errstr_len;
last += sizeof(uint16_t);
/* driver-specific errstr data */
if (errstr_len) {
last = ngx_copy(last, (u_char *) errstr, errstr_len);
}
/* affected rows */
*(uint64_t *) last = drizzle_result_affected_rows(res);
last += sizeof(uint64_t);
/* insert id */
*(uint64_t *) last = drizzle_result_insert_id(res);
last += sizeof(uint64_t);
/* column count */
*(uint16_t *) last = col_count;
last += sizeof(uint16_t);
if ((size_t) (last - pos) != size) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"drizzle: FATAL: output result header buffer error");
return NGX_ERROR;
}
if (col_count == 0) {
dd("Col count is ZERO");
/* we suppress row terminator here when there's no columns */
dp->seen_stream_end = 1;
rc = ngx_http_drizzle_submit_mem(r, dp, size);
if (rc != NGX_OK) {
return NGX_ERROR;
}
dd("about to be done...");
ngx_http_upstream_drizzle_done(r, u, dp, NGX_DONE);
dd("i am returning DONE");
return NGX_DONE;
}
return ngx_http_drizzle_submit_mem(r, dp, size);
}
ngx_int_t
ngx_http_drizzle_output_bufs(ngx_http_request_t *r,
ngx_http_upstream_drizzle_peer_data_t *dp)
{
ngx_http_upstream_t *u = r->upstream;
ngx_int_t rc;
ngx_str_t key, value;
ngx_http_drizzle_loc_conf_t *dlcf;
ngx_chain_t *cl;
dd("enter");
dd_dump_chain_size();
if (dp->seen_stream_end) {
dp->seen_stream_end = 0;
if (dp->avail_out) {
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = dp->out_buf;
cl->next = NULL;
*dp->last_out = cl;
dp->last_out = &cl->next;
dp->avail_out = 0;
}
}
if (!u->header_sent && u->out_bufs) {
ngx_http_clear_content_length(r);
r->headers_out.status = NGX_HTTP_OK;
/* set the Content-Type header */
r->headers_out.content_type.data = (u_char *) rds_content_type;
r->headers_out.content_type.len = rds_content_type_len;
r->headers_out.content_type_len = rds_content_type_len;
r->headers_out.content_type_lowcase = NULL;
dlcf = ngx_http_get_module_loc_conf(r, ngx_http_drizzle_module);
if (dlcf->enable_module_header) {
/* set the X-Resty-DBD-Module header */
key.data = (u_char *) ngx_http_drizzle_module_header_key;
key.len = ngx_http_drizzle_module_header_key_len;
value.data = (u_char *) ngx_http_drizzle_module_header_val;
value.len = ngx_http_drizzle_module_header_val_len;
rc = ngx_http_drizzle_set_header(r, &key, &value);
if (rc != NGX_OK) {
return NGX_ERROR;
}
}
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
u->header_sent = 1;
}
for ( ;; ) {
if (u->out_bufs == NULL) {
return NGX_OK;
}
#if 0
{
int n;
ngx_chain_t *cl;
for (n = 0, cl = u->out_bufs; cl; cl = cl->next, n++) {
}
fprintf(stderr, "XXX chain size: %d\n", n);
}
#endif
rc = ngx_http_output_filter(r, u->out_bufs);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
#if defined(nginx_version) && nginx_version >= 1001004
ngx_chain_update_chains(r->pool, &u->free_bufs, &u->busy_bufs,
&u->out_bufs, u->output.tag);
#else
ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs, &u->out_bufs,
u->output.tag);
#endif
dp->last_out = &u->out_bufs;
}
/* impossible to reach here */
}
ngx_int_t
ngx_http_drizzle_output_col(ngx_http_request_t *r, drizzle_column_st *col)
{
u_char *pos, *last;
drizzle_column_type_t col_type = 0;
uint16_t std_col_type = 0;
const char *col_name = NULL;
uint16_t col_name_len = 0;
size_t size;
ngx_http_upstream_drizzle_peer_data_t *dp = ngx_http_drizzle_get_peer_data(r);
if (col == NULL) {
return NGX_ERROR;
}
col_type = drizzle_column_type(col);
col_name = drizzle_column_name(col);
col_name_len = (uint16_t) strlen(col_name);
size = sizeof(uint16_t) /* std col type */
+ sizeof(uint16_t) /* driver-specific col type */
+ sizeof(uint16_t) /* col name str len */
+ col_name_len; /* col name str len */
pos = ngx_http_drizzle_request_mem(r, dp, size);
if (pos == NULL) {
return NGX_ERROR;
}
last = pos;
/* std column type */
std_col_type = (uint16_t) ngx_http_drizzle_std_col_type(col_type);
#if 0
dd("std col type for %s: %d, %d (%d, %d, %d)",
col_name, std_col_type, rds_col_type_blob,
rds_rough_col_type_str,
rds_rough_col_type_str << 14,
(uint16_t) (19 | (rds_rough_col_type_str << 14)));
#endif
*(uint16_t *) last = std_col_type;
last += sizeof(uint16_t);
/* drizzle column type */
*(uint16_t *) last = col_type;
last += sizeof(uint16_t);
/* column name string length */
*(uint16_t *) last = col_name_len;
last += sizeof(uint16_t);
/* column name string data */
last = ngx_copy(last, col_name, col_name_len);
if ((size_t) (last - pos) != size) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"drizzle: FATAL: output column buffer error");
return NGX_ERROR;
}
return ngx_http_drizzle_submit_mem(r, dp, size);
}
ngx_int_t
ngx_http_drizzle_output_row(ngx_http_request_t *r, uint64_t row)
{
u_char *pos, *last;
size_t size;
ngx_http_upstream_drizzle_peer_data_t *dp = ngx_http_drizzle_get_peer_data(r);
size = sizeof(uint8_t);
pos = ngx_http_drizzle_request_mem(r, dp, size);
if (pos == NULL) {
return NGX_ERROR;
}
last = pos;
*last++ = (row != 0);
if (row == 0) {
dp->seen_stream_end = 1;
}
return ngx_http_drizzle_submit_mem(r, dp, size);
}
ngx_int_t
ngx_http_drizzle_output_field(ngx_http_request_t *r, size_t offset,
size_t len, size_t total, drizzle_field_t field)
{
u_char *pos, *last;
size_t size = 0;
ngx_http_upstream_drizzle_peer_data_t *dp = ngx_http_drizzle_get_peer_data(r);
if (offset == 0) {
if (len == 0 && total != 0) {
return NGX_DONE;
}
size = sizeof(uint32_t); /* field total length */
}
/* (more) field data */
size += (uint32_t) len;
/* request memory */
pos = ngx_http_drizzle_request_mem(r, dp, size);
if (pos == NULL) {
return NGX_ERROR;
}
last = pos;
/* fill in the buffer */
if (offset == 0) {
/* field total length */
if (field == NULL) {
*(uint32_t *) last = (uint32_t) -1;
} else {
*(uint32_t *) last = (uint32_t) total;
}
last += sizeof(uint32_t);
}
/* field data */
if (len && field) {
last = ngx_copy(last, field, (uint32_t) len);
}
if ((size_t) (last - pos) != size) {
dd("offset %d, len %d, size %d", (int) offset,
(int) len, (int) size);
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"drizzle: FATAL: output field buffer error");
return NGX_ERROR;
}
return ngx_http_drizzle_submit_mem(r, dp, size);
}
static rds_col_type_t
ngx_http_drizzle_std_col_type(drizzle_column_type_t col_type)
{
dd("drizzle col type: %d", col_type);
switch (col_type) {
case DRIZZLE_COLUMN_TYPE_DECIMAL:
return rds_col_type_decimal;
case DRIZZLE_COLUMN_TYPE_TINY:
return rds_col_type_smallint;
case DRIZZLE_COLUMN_TYPE_SHORT:
return rds_col_type_smallint;
case DRIZZLE_COLUMN_TYPE_LONG:
return rds_col_type_integer;
case DRIZZLE_COLUMN_TYPE_FLOAT:
return rds_col_type_real;
case DRIZZLE_COLUMN_TYPE_DOUBLE:
return rds_col_type_double;
case DRIZZLE_COLUMN_TYPE_NULL:
return rds_col_type_unknown;
case DRIZZLE_COLUMN_TYPE_TIMESTAMP:
return rds_col_type_timestamp;
case DRIZZLE_COLUMN_TYPE_LONGLONG:
return rds_col_type_bigint;
case DRIZZLE_COLUMN_TYPE_INT24:
return rds_col_type_integer;
case DRIZZLE_COLUMN_TYPE_DATE:
return rds_col_type_timestamp;
case DRIZZLE_COLUMN_TYPE_TIME:
return rds_col_type_time;
case DRIZZLE_COLUMN_TYPE_DATETIME:
return rds_col_type_timestamp;
case DRIZZLE_COLUMN_TYPE_YEAR:
return rds_col_type_smallint;
case DRIZZLE_COLUMN_TYPE_NEWDATE:
return rds_col_type_timestamp;
case DRIZZLE_COLUMN_TYPE_VARCHAR:
return rds_col_type_varchar;
case DRIZZLE_COLUMN_TYPE_BIT:
return rds_col_type_bit;
case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:
return rds_col_type_decimal;
case DRIZZLE_COLUMN_TYPE_ENUM:
return rds_col_type_varchar;
case DRIZZLE_COLUMN_TYPE_SET:
return rds_col_type_varchar;
case DRIZZLE_COLUMN_TYPE_TINY_BLOB:
return rds_col_type_blob;
case DRIZZLE_COLUMN_TYPE_MEDIUM_BLOB:
return rds_col_type_blob;
case DRIZZLE_COLUMN_TYPE_LONG_BLOB:
return rds_col_type_blob;
case DRIZZLE_COLUMN_TYPE_BLOB:
return rds_col_type_blob;
case DRIZZLE_COLUMN_TYPE_VAR_STRING:
return rds_col_type_varchar;
case DRIZZLE_COLUMN_TYPE_STRING:
return rds_col_type_varchar;
case DRIZZLE_COLUMN_TYPE_GEOMETRY:
return rds_col_type_varchar;
default:
return rds_col_type_unknown;
}
/* impossible to reach here */
}
static u_char *
ngx_http_drizzle_request_mem(ngx_http_request_t *r,
ngx_http_upstream_drizzle_peer_data_t *dp, size_t len)
{
ngx_int_t rc;
u_char *p;
rc = ngx_http_drizzle_get_buf(r, dp);
if (rc != NGX_OK) {
return NULL;
}
if (dp->avail_out < len) {
p = ngx_http_drizzle_get_postponed(r, dp, len);
if (p == NULL) {
return NULL;
}
dp->postponed.pos = p;
dp->postponed.last = p + len;
return p;
}
return dp->out_buf->last;
}
static ngx_int_t
ngx_http_drizzle_get_buf(ngx_http_request_t *r,
ngx_http_upstream_drizzle_peer_data_t *dp)
{
ngx_http_drizzle_loc_conf_t *conf = dp->loc_conf;
ngx_http_upstream_t *u = dp->upstream;
dd("MEM enter");
if (dp->avail_out) {
return NGX_OK;
}
if (u->free_bufs) {
dd("MEM reusing temp buf from free_bufs");
dp->out_buf = u->free_bufs->buf;
u->free_bufs = u->free_bufs->next;
} else {
dd("MEM creating temp buf with size: %d", (int) conf->buf_size);
dp->out_buf = ngx_create_temp_buf(r->pool, conf->buf_size);
if (dp->out_buf == NULL) {
return NGX_ERROR;
}
dp->out_buf->tag = (ngx_buf_tag_t) &ngx_http_drizzle_module;
dp->out_buf->recycled = 1;
}
dp->avail_out = conf->buf_size;
return NGX_OK;
}
static u_char *
ngx_http_drizzle_get_postponed(ngx_http_request_t *r,
ngx_http_upstream_drizzle_peer_data_t *dp, size_t len)
{
u_char *p;
dd("MEM enter");
if (dp->cached.start == NULL) {
goto alloc;
}
if ((size_t) (dp->cached.end - dp->cached.start) < len) {
ngx_pfree(r->pool, dp->cached.start);
goto alloc;
}
return dp->cached.start;
alloc:
p = ngx_palloc(r->pool, len);
if (p == NULL) {
return NULL;
}
dp->cached.start = p;
dp->cached.end = p + len;
return p;
}
static ngx_int_t
ngx_http_drizzle_submit_mem(ngx_http_request_t *r,
ngx_http_upstream_drizzle_peer_data_t *dp, size_t len)
{
ngx_chain_t *cl;
ngx_int_t rc;
size_t postponed_len;
ngx_http_drizzle_loc_conf_t *conf = dp->loc_conf;
if (dp->postponed.pos != NULL) {
dd("MEM copy postponed data over to u->out_bufs for len %d", (int) len);
postponed_len = dp->postponed.last - dp->postponed.pos;
if (postponed_len > dp->avail_out) {
/* we should ensure that rds atoms do not get
* splitted into multiple bufs. */
if (dp->out_buf && dp->out_buf->pos != dp->out_buf->last) {
/* save the current dp->out_buf */
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = dp->out_buf;
cl->next = NULL;
*dp->last_out = cl;
dp->last_out = &cl->next;
}
/* create a buf for the postponed buf */
len = postponed_len > conf->buf_size ?
postponed_len : conf->buf_size;
dp->out_buf = ngx_create_temp_buf(r->pool, len);
if (dp->out_buf == NULL) {
return NGX_ERROR;
}
dp->out_buf->tag = (ngx_buf_tag_t) &ngx_http_drizzle_module;
dp->out_buf->recycled = 1;
dp->out_buf->last = ngx_copy(dp->out_buf->last, dp->postponed.pos,
postponed_len);
dp->avail_out = len - postponed_len;
dp->postponed.pos = NULL;
if (dp->avail_out == 0) {
/* save the new big buf */
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = dp->out_buf;
cl->next = NULL;
*dp->last_out = cl;
dp->last_out = &cl->next;
}
return NGX_OK;
}
for ( ;; ) {
len = dp->postponed.last - dp->postponed.pos;
if (len > dp->avail_out) {
len = dp->avail_out;
}
dp->out_buf->last = ngx_copy(dp->out_buf->last, dp->postponed.pos,
len);
dp->avail_out -= len;
dp->postponed.pos += len;
if (dp->postponed.pos == dp->postponed.last) {
dp->postponed.pos = NULL;
}
if (dp->avail_out > 0) {
break;
}
dd("MEM save dp->out_buf");
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = dp->out_buf;
cl->next = NULL;
*dp->last_out = cl;
dp->last_out = &cl->next;
if (dp->postponed.pos == NULL) {
break;
}
rc = ngx_http_drizzle_get_buf(r, dp);
if (rc != NGX_OK) {
return NGX_ERROR;
}
}
return NGX_OK;
}
dd("MEM consuming out_buf for %d", (int) len);
dp->out_buf->last += len;
dp->avail_out -= len;
if (dp->avail_out == 0) {
dd("MEM save dp->out_buf");
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = dp->out_buf;
cl->next = NULL;
*dp->last_out = cl;
dp->last_out = &cl->next;
}
return NGX_OK;
}
================================================
FILE: src/ngx_http_drizzle_output.h
================================================
#ifndef NGX_HTTP_DRIZZLE_OUTPUT_H
#define NGX_HTTP_DRIZZLE_OUTPUT_H
#include "ngx_http_drizzle_upstream.h"
ngx_int_t ngx_http_drizzle_output_result_header(ngx_http_request_t *r,
drizzle_result_st *res);
ngx_int_t ngx_http_drizzle_output_col(ngx_http_request_t *r,
drizzle_column_st *res);
ngx_int_t ngx_http_drizzle_output_row(ngx_http_request_t *r, uint64_t row);
ngx_int_t ngx_http_drizzle_output_field(ngx_http_request_t *r, size_t offset,
size_t len, size_t total, drizzle_field_t field);
ngx_int_t ngx_http_drizzle_output_bufs(ngx_http_request_t *r,
ngx_http_upstream_drizzle_peer_data_t *dp);
#endif /* NGX_HTTP_DRIZZLE_OUTPUT_H */
================================================
FILE: src/ngx_http_drizzle_processor.c
================================================
/*
* Copyright (C) Xiaozhe Wang (chaoslawful)
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_drizzle_processor.h"
#include "ngx_http_drizzle_module.h"
#include "ngx_http_drizzle_util.h"
#include "ngx_http_drizzle_output.h"
#include "ngx_http_drizzle_upstream.h"
#define MYSQL_ER_NO_SUCH_TABLE 1146
static ngx_int_t ngx_http_upstream_drizzle_connect(ngx_http_request_t *r,
ngx_connection_t *c, ngx_http_upstream_drizzle_peer_data_t *dp,
drizzle_con_st *dc);
static ngx_int_t ngx_http_upstream_drizzle_send_query(ngx_http_request_t *r,
ngx_connection_t *c, ngx_http_upstream_drizzle_peer_data_t *dp,
drizzle_con_st *dc);
static ngx_int_t ngx_http_upstream_drizzle_recv_cols(ngx_http_request_t *r,
ngx_connection_t *c, ngx_http_upstream_drizzle_peer_data_t *dp,
drizzle_con_st *dc);
static ngx_int_t ngx_http_upstream_drizzle_recv_rows(ngx_http_request_t *r,
ngx_connection_t *c, ngx_http_upstream_drizzle_peer_data_t *dp,
drizzle_con_st *dc);
ngx_int_t
ngx_http_drizzle_process_events(ngx_http_request_t *r)
{
ngx_http_upstream_t *u;
ngx_connection_t *c;
ngx_http_upstream_drizzle_peer_data_t *dp;
drizzle_con_st *dc;
ngx_int_t rc;
#if 0
drizzle_return_t ret;
#endif
u = r->upstream;
c = u->peer.connection;
dp = ngx_http_drizzle_get_peer_data(r);
if (dp == NULL) {
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"process events: it seems you "
"are using a non-drizzle upstream backend"
);
return NGX_ERROR;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"drizzle process events, state: %d", dp->state);
dc = dp->drizzle_con;
switch (dp->state) {
case state_db_connect:
rc = ngx_http_upstream_drizzle_connect(r, c, dp, dc);
break;
case state_db_idle: /* from connection pool */
c->log->action = "sending query to drizzle upstream";
/* fallthrough */
case state_db_send_query:
rc = ngx_http_upstream_drizzle_send_query(r, c, dp, dc);
break;
case state_db_recv_cols:
rc = ngx_http_upstream_drizzle_recv_cols(r, c, dp, dc);
break;
case state_db_recv_rows:
rc = ngx_http_upstream_drizzle_recv_rows(r, c, dp, dc);
break;
default:
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"unknown state: %d", (int) dp->state);
return NGX_ERROR;
}
dd("rc == %d", (int) rc);
if (rc == NGX_ERROR) {
ngx_http_upstream_drizzle_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
return NGX_ERROR;
}
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
ngx_http_upstream_drizzle_finalize_request(r, u, rc);
return NGX_ERROR;
}
if (rc == NGX_AGAIN) {
#if 0
if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
return NGX_ERROR;
}
#endif
rc = ngx_http_drizzle_output_bufs(r, dp);
if (rc == NGX_ERROR || rc > NGX_OK) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return NGX_ERROR;
}
}
return rc;
}
static ngx_int_t
ngx_http_upstream_drizzle_connect(ngx_http_request_t *r,
ngx_connection_t *c, ngx_http_upstream_drizzle_peer_data_t *dp,
drizzle_con_st *dc)
{
drizzle_return_t ret;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"drizzle connect: user %s, password %s", dc->user,
dc->password);
ret = drizzle_con_connect(dc);
if (ret == DRIZZLE_RETURN_IO_WAIT) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"drizzle libdrizzle returned IO_WAIT while "
"connecting");
#if 0
if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
return NGX_ERROR;
}
#endif
return NGX_AGAIN;
}
if (c->write->timer_set) {
ngx_del_timer(c->write);
}
if (ret != DRIZZLE_RETURN_OK) {
ngx_log_error(NGX_LOG_ERR, c->log, 0, "failed to connect: %d: %s",
(int) ret, drizzle_error(dc->drizzle));
return NGX_ERROR;
}
/* ret == DRIZZLE_RETURN_OK */
c->log->action = "sending query to drizzle upstream";
return ngx_http_upstream_drizzle_send_query(r, c, dp, dc);
}
static ngx_int_t
ngx_http_upstream_drizzle_send_query(ngx_http_request_t *r,
ngx_connection_t *c, ngx_http_upstream_drizzle_peer_data_t *dp,
drizzle_con_st *dc)
{
ngx_http_upstream_t *u = r->upstream;
drizzle_return_t ret;
ngx_int_t rc;
ngx_str_t query;
ngx_flag_t has_set_names = 0;
ngx_flag_t enable_charset = 0;
dd("enable charset: %d", (int) dp->enable_charset);
if (dp->enable_charset && ! dp->has_set_names) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"drizzle enables connection charset setting");
query.len = dp->set_names_query->len;
query.data = dp->set_names_query->data;
} else {
query.data = dp->query.data;
query.len = dp->query.len;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"drizzle sending query \"%V\"", &query);
(void) drizzle_query(dc, &dp->drizzle_res, (const char *) query.data,
query.len, &ret);
if (ret == DRIZZLE_RETURN_IO_WAIT) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"drizzle libdrizzle returned IO_WAIT while sending "
"query");
if (dp->state != state_db_send_query) {
dp->state = state_db_send_query;
if (c->write->timer_set) {
ngx_del_timer(c->write);
}
ngx_add_timer(c->write, u->conf->send_timeout);
}
return NGX_AGAIN;
}
if (c->write->timer_set) {
ngx_del_timer(c->write);
}
if (ret != DRIZZLE_RETURN_OK) {
#if 1
if (ret == DRIZZLE_RETURN_ERROR_CODE) {
if (drizzle_error_code(dc->drizzle) == MYSQL_ER_NO_SUCH_TABLE) {
ngx_log_error(NGX_LOG_NOTICE, c->log, 0,
"failed to send query: %i (%d): %s",
ret, drizzle_error_code(dc->drizzle),
drizzle_error(dc->drizzle));
if (dp->enable_charset && ! dp->has_set_names) {
c->log->action = "sending query to drizzle upstream";
dp->has_set_names = 1;
return ngx_http_upstream_drizzle_send_query(r, c, dp, dc);
}
ngx_http_upstream_drizzle_done(r, u, dp, NGX_HTTP_GONE);
return NGX_DONE;
}
}
#endif
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"failed to send query: %d (%d): %s",
(int) ret, drizzle_error_code(dc->drizzle),
drizzle_error(dc->drizzle));
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* ret == DRIZZLE_RETURN_OK */
dd_drizzle_result(&dp->drizzle_res);
dd("after drizzle restult");
if (dp->enable_charset) {
enable_charset = 1;
}
has_set_names = dp->has_set_names;
rc = ngx_http_drizzle_output_result_header(r, &dp->drizzle_res);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
if (rc == NGX_DONE) {
if (enable_charset && ! has_set_names) {
c->log->action = "sending query to drizzle upstream";
dp->has_set_names = 1;
dp->state = state_db_idle;
return ngx_http_upstream_drizzle_send_query(r, c, dp, dc);
}
/* no data set following the header */
return rc;
}
c->log->action = "receiving result set columns from drizzle upstream";
return ngx_http_upstream_drizzle_recv_cols(r, c, dp, dc);
}
static ngx_int_t
ngx_http_upstream_drizzle_recv_cols(ngx_http_request_t *r,
ngx_connection_t *c, ngx_http_upstream_drizzle_peer_data_t *dp,
drizzle_con_st *dc)
{
drizzle_column_st *col;
ngx_int_t rc;
drizzle_return_t ret;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"drizzle receive resultset columns");
for (;;) {
col = drizzle_column_read(&dp->drizzle_res, &dp->drizzle_col, &ret);
if (ret == DRIZZLE_RETURN_IO_WAIT) {
if (dp->state != state_db_recv_cols) {
dp->state = state_db_recv_cols;
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
ngx_add_timer(c->read, dp->loc_conf->recv_cols_timeout);
}
#if 0
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
return NGX_ERROR;
}
#endif
return NGX_AGAIN;
}
if (ret != DRIZZLE_RETURN_OK) {
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"failed to recv cols: %d: %s",
(int) ret,
drizzle_error(dc->drizzle));
return NGX_ERROR;
}
/* ret == DRIZZLE_RETURN_OK */
if (col) {
rc = ngx_http_drizzle_output_col(r, col);
drizzle_column_free(col);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
} else { /* after the last column */
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
c->log->action = "receiving result set rows from drizzle "
"upstream";
return ngx_http_upstream_drizzle_recv_rows(r, c, dp, dc);
}
dd_drizzle_column(col);
}
/* impossible to reach here */
}
static ngx_int_t
ngx_http_upstream_drizzle_recv_rows(ngx_http_request_t *r,
ngx_connection_t *c, ngx_http_upstream_drizzle_peer_data_t *dp,
drizzle_con_st *dc)
{
ngx_http_upstream_t *u = r->upstream;
ngx_int_t rc;
drizzle_return_t ret;
size_t offset;
size_t len;
size_t total;
drizzle_field_t field;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"drizzle receive resultset rows");
for (;;) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"drizzle receive resultset row %uL", dp->drizzle_row);
if (dp->drizzle_row == 0) {
dp->drizzle_row = drizzle_row_read(&dp->drizzle_res, &ret);
if (ret == DRIZZLE_RETURN_IO_WAIT) {
dp->drizzle_row = 0;
goto io_wait;
}
if (ret != DRIZZLE_RETURN_OK) {
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"failed to read row: %d: %s",
(int) ret,
drizzle_error(dc->drizzle));
return NGX_ERROR;
}
/* ret == DRIZZLE_RETURN_OK */
rc = ngx_http_drizzle_output_row(r, dp->drizzle_row);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
drizzle_result_free(&dp->drizzle_res);
return rc;
}
if (dp->drizzle_row == 0) {
/* after last row */
drizzle_result_free(&dp->drizzle_res);
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
if (dp->enable_charset && ! dp->has_set_names) {
c->log->action = "sending query to drizzle upstream";
dp->has_set_names = 1;
return ngx_http_upstream_drizzle_send_query(r, c, dp, dc);
}
ngx_http_upstream_drizzle_done(r, u, dp, NGX_DONE);
return NGX_DONE;
}
}
/* dp->drizzle_row != 0 */
for (;;) {
field = drizzle_field_read(&dp->drizzle_res, &offset, &len,
&total, &ret);
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
"drizzle field read: %p (offset %z, len %z)",
field, offset, len);
if (ret == DRIZZLE_RETURN_IO_WAIT) {
goto io_wait;
}
if (ret == DRIZZLE_RETURN_ROW_END) {
/* reached the end of the current row */
break;
}
if (ret != DRIZZLE_RETURN_OK) {
drizzle_result_free(&dp->drizzle_res);
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"failed to read row field: %d: %s",
(int) ret,
drizzle_error(dc->drizzle));
return NGX_ERROR;
}
/* ret == DRIZZLE_RETURN_OK */
rc = ngx_http_drizzle_output_field(r, offset, len, total, field);
if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
drizzle_result_free(&dp->drizzle_res);
return rc;
}
if (field) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"drizzle field value read: %*s", len, field);
}
}
dp->drizzle_row = 0;
}
/* impossible to reach here */
io_wait:
if (dp->state != state_db_recv_rows) {
dp->state = state_db_recv_rows;
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
ngx_add_timer(c->read, dp->loc_conf->recv_rows_timeout);
}
#if 0
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
return NGX_ERROR;
}
#endif
return NGX_AGAIN;
}
void
ngx_http_upstream_drizzle_done(ngx_http_request_t *r,
ngx_http_upstream_t *u, ngx_http_upstream_drizzle_peer_data_t *dp,
ngx_int_t rc)
{
ngx_connection_t *c;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"drizzle upstream done");
(void) ngx_http_drizzle_output_bufs(r, dp);
/* to persuade Maxim Dounin's ngx_http_upstream_keepalive
* module to cache the current connection */
u->length = 0;
if (rc == NGX_DONE) {
u->header_sent = 1;
u->headers_in.status_n = NGX_HTTP_OK;
rc = NGX_OK;
} else {
r->headers_out.status = rc;
u->headers_in.status_n = rc;
}
c = u->peer.connection;
c->log->action = "being idle";
/* reset the state machine */
dp->state = state_db_idle;
dd("about to finalize request...");
ngx_http_upstream_drizzle_finalize_request(r, u, rc);
dd("after finalize request...");
}
================================================
FILE: src/ngx_http_drizzle_processor.h
================================================
#ifndef NGX_HTTP_DRIZZLE_PROCESSOR_H
#define NGX_HTTP_DRIZZLE_PROCESSOR_H
#include "ngx_http_drizzle_module.h"
#include "ngx_http_drizzle_upstream.h"
#include <ngx_http.h>
#include <ngx_core.h>
ngx_int_t ngx_http_drizzle_process_events(ngx_http_request_t *r);
void ngx_http_upstream_drizzle_done(ngx_http_request_t *r,
ngx_http_upstream_t *u, ngx_http_upstream_drizzle_peer_data_t *dp,
ngx_int_t rc);
#endif /* NGX_HTTP_DRIZZLE_PROCESSOR_H */
================================================
FILE: src/ngx_http_drizzle_quoting.c
================================================
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_drizzle_quoting.h"
#include "ngx_http_drizzle_checker.h"
#include <ngx_core.h>
#include <ngx_http.h>
/* static */ ngx_http_drizzle_var_type_t ngx_http_drizzle_builtin_types[] = {
{ ngx_string("string"),
NULL,
NULL,
quotes_type_single
},
{ ngx_string("int"),
ngx_http_drizzle_check_int,
NULL,
quotes_type_none
},
{ ngx_string("bool"),
ngx_http_drizzle_check_bool,
NULL,
quotes_type_none
},
{ ngx_string("float"),
ngx_http_drizzle_check_float,
NULL,
quotes_type_none
},
{ ngx_string("column"),
ngx_http_drizzle_check_col,
NULL,
quotes_type_none
},
{ ngx_string("table"),
ngx_http_drizzle_check_table,
NULL,
quotes_type_none
},
{ ngx_string("keyword"),
ngx_http_drizzle_check_keyword,
NULL,
quotes_type_none
}
};
================================================
FILE: src/ngx_http_drizzle_quoting.h
================================================
#ifndef NGX_HTTP_DRIZZLE_QUOTING_H
#define NGX_HTTP_DRIZZLE_QUOTING_H
#include "ngx_http_drizzle_module.h"
typedef ngx_int_t (* ngx_http_drizzle_type_checker_pt) (ngx_str_t *value,
void *data);
typedef enum {
quotes_type_none,
quotes_type_single,
quotes_type_double,
quotes_type_back
} ngx_http_drizzle_quotes_type_t;
typedef struct {
ngx_str_t name;
ngx_http_drizzle_type_checker_pt checker;
void *checker_data;
ngx_http_drizzle_quotes_type_t quotes;
} ngx_http_drizzle_var_type_t;
typedef struct {
ngx_int_t src_var;
ngx_int_t dest_var;
ngx_http_drizzle_var_type_t *type;
ngx_str_t errstr;
} ngx_http_drizzle_var_to_quote_t;
#endif /* NGX_HTTP_DRIZZLE_QUOTING_H */
================================================
FILE: src/ngx_http_drizzle_upstream.c
================================================
/* Copyright (C) agentzh */
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_drizzle_module.h"
#include "ngx_http_drizzle_upstream.h"
#include "ngx_http_drizzle_keepalive.h"
#include "ngx_http_drizzle_processor.h"
#include "ngx_http_drizzle_util.h"
enum {
ngx_http_drizzle_default_port = 3306
};
static void ngx_http_upstream_drizzle_cleanup(void *data);
static ngx_int_t ngx_http_upstream_drizzle_init(ngx_conf_t *cf,
ngx_http_upstream_srv_conf_t *uscf);
static ngx_int_t ngx_http_upstream_drizzle_init_peer(ngx_http_request_t *r,
ngx_http_upstream_srv_conf_t *uscf);
static ngx_int_t ngx_http_upstream_drizzle_get_peer(ngx_peer_connection_t *pc,
void *data);
static void ngx_http_upstream_drizzle_free_peer(ngx_peer_connection_t *pc,
void *data, ngx_uint_t state);
/* just a work-around to override the default u->output_filter */
static ngx_int_t ngx_http_drizzle_output_filter(void *data, ngx_chain_t *in);
void *
ngx_http_upstream_drizzle_create_srv_conf(ngx_conf_t *cf)
{
ngx_pool_cleanup_t *cln;
ngx_http_upstream_drizzle_srv_conf_t *conf;
dd("drizzle create srv conf");
conf = ngx_pcalloc(cf->pool,
sizeof(ngx_http_upstream_drizzle_srv_conf_t));
if (conf == NULL) {
return NULL;
}
/* set by ngx_pcalloc:
* conf->peers = NULL
* conf->current = 0
* conf->servers = NULL
* conf->single = 0
* conf->max_cached = 0
* conf->overflow = 0 (drizzle_keepalive_overflow_ignore)
*/
conf->pool = cf->pool;
cln = ngx_pool_cleanup_add(cf->pool, 0);
if (cln == NULL) {
return NULL;
}
(void) drizzle_create(&conf->drizzle);
cln->handler = ngx_http_upstream_drizzle_cleanup;
cln->data = &conf->drizzle;
drizzle_add_options(&conf->drizzle, DRIZZLE_NON_BLOCKING);
/* we use 0 timeout for the underlying poll event model
* used by libdrizzle itself. */
drizzle_set_timeout(&conf->drizzle, 0);
return conf;
}
/* mostly based on ngx_http_upstream_server in
* ngx_http_upstream.c of nginx 0.8.30.
* Copyright (C) Igor Sysoev */
char *
ngx_http_upstream_drizzle_server(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
ngx_http_upstream_drizzle_srv_conf_t *dscf = conf;
ngx_http_upstream_drizzle_server_t *ds;
ngx_str_t *value;
ngx_url_t u;
ngx_uint_t i, j;
ngx_http_upstream_srv_conf_t *uscf;
ngx_str_t protocol;
ngx_str_t charset;
u_char *p;
size_t len;
dd("entered drizzle_server directive handler...");
uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
if (dscf->servers == NULL) {
dscf->servers = ngx_array_create(cf->pool, 4,
sizeof(ngx_http_upstream_drizzle_server_t));
if (dscf->servers == NULL) {
return NGX_CONF_ERROR;
}
uscf->servers = dscf->servers;
}
ds = ngx_array_push(dscf->servers);
if (ds == NULL) {
return NGX_CONF_ERROR;
}
ngx_memzero(ds, sizeof(ngx_http_upstream_drizzle_server_t));
value = cf->args->elts;
/* parse the first name:port argument */
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[1];
u.default_port = ngx_http_drizzle_default_port;
if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
if (u.err) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"drizzle: %s in upstream \"%V\"", u.err, &u.url);
}
return NGX_CONF_ERROR;
}
ds->addrs = u.addrs;
ds->naddrs = u.naddrs;
ds->port = u.port;
ds->protocol = ngx_http_drizzle_protocol;
/* parse various options */
for (i = 2; i < cf->args->nelts; i++) {
if (ngx_strncmp(value[i].data, "dbname=", sizeof("dbname=") - 1)
== 0)
{
ds->dbname.len = value[i].len - (sizeof("dbname=") - 1);
if (ds->dbname.len >= DRIZZLE_MAX_DB_SIZE) {
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"drizzle: \"dbname\" value too large in upstream \"%V\""
" (at most %d bytes)",
dscf->peers->name,
(int) DRIZZLE_MAX_DB_SIZE);
return NGX_CONF_ERROR;
}
ds->dbname.data = &value[i].data[sizeof("dbname=") - 1];
continue;
}
if (ngx_strncmp(value[i].data, "user=", sizeof("user=") - 1)
== 0)
{
ds->user.len = value[i].len - (sizeof("user=") - 1);
if (ds->user.len >= DRIZZLE_MAX_USER_SIZE) {
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"drizzle: \"user\" value too large in upstream \"%V\""
" (at most %d bytes)",
dscf->peers->name,
(int) DRIZZLE_MAX_USER_SIZE);
return NGX_CONF_ERROR;
}
ds->user.data = &value[i].data[sizeof("user=") - 1];
continue;
}
if (ngx_strncmp(value[i].data, "password=", sizeof("password=") - 1)
== 0)
{
ds->password.len = value[i].len - (sizeof("password=") - 1);
if (ds->password.len >= DRIZZLE_MAX_PASSWORD_SIZE) {
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"drizzle: \"password\" value too large in upstream "
"\"%V\" (at most %d bytes)",
dscf->peers->name,
(int) DRIZZLE_MAX_PASSWORD_SIZE);
return NGX_CONF_ERROR;
}
ds->password.data = &value[i].data[sizeof("password=") - 1];
continue;
}
if (ngx_strncmp(value[i].data, "protocol=", sizeof("protocol=") - 1)
== 0)
{
protocol.len = value[i].len - (sizeof("protocol=") - 1);
protocol.data = &value[i].data[sizeof("protocol=") - 1];
switch (protocol.len) {
case 5:
if (ngx_http_drizzle_strcmp_const(protocol.data, "mysql") == 0)
{
ds->protocol = ngx_http_mysql_protocol;
} else {
continue;
}
break;
case 7:
if (ngx_http_drizzle_strcmp_const(protocol.data,
"drizzle") != 0)
{
continue;
}
break;
default:
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"drizzle: invalid protocol \"%V\""
" in drizzle_server", &protocol);
return NGX_CONF_ERROR;
}
continue;
}
if (ngx_strncmp(value[i].data, "charset=", sizeof("charset=") - 1)
== 0)
{
charset.len = value[i].len - (sizeof("charset=") - 1);
charset.data = &value[i].data[sizeof("charset=") - 1];
dd("charset: %.*s", (int) charset.len, charset.data);
if (charset.len == 0) {
continue;
}
for (j = 0; j < charset.len; j++) {
if (charset.data[j] == '\'') {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"bad charste value \"%V\" in"
" drizzle_server", &charset);
return NGX_CONF_ERROR;
}
}
len = sizeof("set names ''") - 1 + charset.len;
p = ngx_palloc(cf->pool, len);
if (p == NULL) {
return NGX_CONF_ERROR;
}
ds->set_names_query.data = p;
ds->set_names_query.len = len;
dd("charset query len: %d", (int) len);
p = ngx_copy(p, "set names '", sizeof("set names '") - 1);
p = ngx_copy(p, charset.data, charset.len);
*p = '\'';
continue;
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter \"%V\" in"
" drizzle_server", &value[i]);
return NGX_CONF_ERROR;
}
dd("reset init_upstream...");
uscf->peer.init_upstream = ngx_http_upstream_drizzle_init;
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_upstream_drizzle_init(ngx_conf_t *cf,
ngx_http_upstream_srv_conf_t *uscf)
{
ngx_uint_t i, j, n;
ngx_http_upstream_drizzle_srv_conf_t *dscf;
ngx_http_upstream_drizzle_server_t *server;
ngx_http_upstream_drizzle_peers_t *peers;
size_t len;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
"drizzle upstream init");
uscf->peer.init = ngx_http_upstream_drizzle_init_peer;
dscf = ngx_http_conf_upstream_srv_conf(uscf, ngx_http_drizzle_module);
if (dscf->servers == NULL || dscf->servers->nelts == 0) {
/* XXX an upstream implicitly defined by drizzle_pass, etc.,
* is not allowed for now */
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
"drizzle: no drizzle_server defined in upstream \"%V\""
" in %s:%ui",
&uscf->host, uscf->file_name, uscf->line);
return NGX_ERROR;
}
/* dscf->servers != NULL */
server = uscf->servers->elts;
n = 0;
for (i = 0; i < uscf->servers->nelts; i++) {
n += server[i].naddrs;
}
peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_drizzle_peers_t)
+ sizeof(ngx_http_upstream_drizzle_peer_t) * (n - 1));
if (peers == NULL) {
return NGX_ERROR;
}
peers->single = (n == 1);
peers->number = n;
peers->name = &uscf->host;
n = 0;
for (i = 0; i < uscf->servers->nelts; i++) {
for (j = 0; j < server[i].naddrs; j++) {
peers->peer[n].sockaddr = server[i].addrs[j].sockaddr;
peers->peer[n].socklen = server[i].addrs[j].socklen;
peers->peer[n].name = server[i].addrs[j].name;
peers->peer[n].port = server[i].port;
peers->peer[n].user = server[i].user;
peers->peer[n].password = server[i].password;
peers->peer[n].dbname = server[i].dbname;
peers->peer[n].protocol = server[i].protocol;
peers->peer[n].set_names_query = &server[i].set_names_query;
len = NGX_SOCKADDR_STRLEN + 1 /* for '\0' */;
peers->peer[n].host = ngx_palloc(cf->pool, len);
if (peers->peer[n].host == NULL) {
return NGX_ERROR;
}
len = ngx_sock_ntop(peers->peer[n].sockaddr,
#if defined(nginx_version) && (nginx_version >= 1005003)
peers->peer[n].socklen,
#endif
peers->peer[n].host, len - 1, 0 /* no port */);
peers->peer[n].host[len] = '\0';
n++;
}
}
dscf->peers = peers;
dscf->active_conns = 0;
if (dscf->max_cached) {
return ngx_http_drizzle_keepalive_init(cf->pool, dscf);
}
return NGX_OK;
}
static ngx_int_t
ngx_http_upstream_drizzle_init_peer(ngx_http_request_t *r,
ngx_http_upstream_srv_conf_t *uscf)
{
ngx_http_upstream_drizzle_peer_data_t *dp;
ngx_http_upstream_drizzle_srv_conf_t *dscf;
ngx_http_upstream_t *u;
ngx_http_core_loc_conf_t *clcf;
ngx_http_drizzle_loc_conf_t *dlcf;
ngx_http_drizzle_ctx_t *dctx;
ngx_drizzle_mixed_t *query;
ngx_str_t dbname, sql;
ngx_uint_t i;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"drizzle init peer");
dp = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_drizzle_peer_data_t));
if (dp == NULL) {
goto failed;
}
u = r->upstream;
dp->upstream = u;
dp->request = r;
dp->last_out = &u->out_bufs;
dscf = ngx_http_conf_upstream_srv_conf(uscf, ngx_http_drizzle_module);
dp->srv_conf = dscf;
dlcf = ngx_http_get_module_loc_conf(r, ngx_http_drizzle_module);
dp->loc_conf = dlcf;
dp->query.len = 0;
dp->dbname.len = 0;
/* to force ngx_output_chain not to use ngx_chain_writer */
u->output.output_filter = ngx_http_drizzle_output_filter;
u->output.filter_ctx = r;
u->output.in = NULL;
u->output.busy = NULL;
u->peer.data = dp;
u->peer.get = ngx_http_upstream_drizzle_get_peer;
u->peer.free = ngx_http_upstream_drizzle_free_peer;
/* Stash dp in the module ctx so handlers can recover it even if
* another module (e.g. the standard upstream keepalive, which is
* implicit in nginx 1.29.7+) wraps u->peer.data afterwards. */
dctx = ngx_http_get_module_ctx(r, ngx_http_drizzle_module);
if (dctx != NULL) {
dctx->peer_data = dp;
}
/* prepare dbname */
dp->dbname.len = 0;
if (dlcf->dbname) {
/* check if dbname requires overriding at request time */
if (ngx_http_complex_value(r, dlcf->dbname, &dbname) != NGX_OK) {
goto failed;
}
if (dbname.len) {
if (dbname.len >= DRIZZLE_MAX_DB_SIZE) {
ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0,
"drizzle: \"dbname\" value too large in upstream \"%V\"",
dscf->peers->name);
goto failed;
}
dp->dbname = dbname;
}
}
/* prepare SQL query */
if (dlcf->methods_set & r->method) {
/* method-specific query */
dd("using method-specific query");
query = dlcf->queries->elts;
for (i = 0; i < dlcf->queries->nelts; i++) {
if (query[i].key & r->method) {
query = &query[i];
break;
}
}
if (i == dlcf->queries->nelts) {
goto failed;
}
} else {
/* default query */
dd("using default query");
query = dlcf->default_query;
}
if (query->cv) {
/* complex value */
dd("using complex value");
if (ngx_http_complex_value(r, query->cv, &sql) != NGX_OK) {
goto failed;
}
if (sql.len == 0) {
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"drizzle: empty \"drizzle_query\" (was: \"%V\")"
" in location \"%V\"", &query->cv->value,
&clcf->name);
goto failed;
}
dp->query = sql;
return NGX_OK;
}
/* simple value */
dd("using simple value");
dp->query = query->sv;
return NGX_OK;
failed:
#if defined(nginx_version) && (nginx_version >= 8017)
return NGX_ERROR;
#else
r->upstream->peer.data = NULL;
return NGX_OK;
#endif
}
static ngx_int_t
ngx_http_upstream_drizzle_get_peer(ngx_peer_connection_t *pc, void *data)
{
ngx_http_upstream_drizzle_peer_data_t *dp = data;
ngx_http_upstream_drizzle_srv_conf_t *dscf;
ngx_http_upstream_drizzle_peers_t *peers;
ngx_http_upstream_drizzle_peer_t *peer;
#if defined(nginx_version) && (nginx_version < 8017)
ngx_http_drizzle_ctx_t *dctx;
#endif
ngx_connection_t *c = NULL;
drizzle_con_st *dc = NULL;
ngx_str_t dbname;
drizzle_return_t ret;
ngx_socket_t fd;
ngx_event_t *rev, *wev;
ngx_int_t rc;
ngx_int_t event;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, dp->request->connection->log, 0,
"drizzle get peer");
#if defined(nginx_version) && (nginx_version < 8017)
if (data == NULL) {
goto failed;
}
dctx = ngx_http_get_module_ctx(dp->request, ngx_http_drizzle_module);
#endif
dscf = dp->srv_conf;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, dp->request->connection->log, 0,
"active drizzle connections %ui", dscf->active_conns);
dp->failed = 0;
/* try to get an idle connection from our single-mode
* keep-alive pool */
if (dscf->max_cached && dscf->single) {
rc = ngx_http_drizzle_keepalive_get_peer_single(pc, dp, dscf);
if (rc != NGX_DECLINED) {
return rc;
}
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, dp->request->connection->log, 0,
"drizzle get peer using simple round robin");
peers = dscf->peers;
if (dscf->current > peers->number - 1) {
dscf->current = 0;
}
peer = &peers->peer[dscf->current++];
dp->name.data = peer->name.data;
dp->name.len = peer->name.len;
dp->sockaddr = *peer->sockaddr;
dp->enable_charset = (peer->set_names_query->len > 0);
dp->set_names_query = peer->set_names_query;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"drizzle set connection charset query \"%V\"",
dp->set_names_query);
pc->name = &dp->name;
pc->sockaddr = &dp->sockaddr;
pc->socklen = peer->socklen;
pc->cached = 0;
if (dscf->max_cached && ! dscf->single) {
rc = ngx_http_drizzle_keepalive_get_peer_multi(pc, dp, dscf);
if (rc != NGX_DECLINED) {
return rc;
}
}
if (dscf->overflow == drizzle_keepalive_overflow_reject
&& dscf->active_conns >= dscf->max_cached)
{
ngx_log_error(NGX_LOG_INFO, pc->log, 0,
"drizzle: connection pool full, rejecting request "
"to upstream \"%V\"",
&peer->name);
/* a bit hack-ish way to return error response (setup part) */
pc->connection = ngx_get_connection(0, pc->log);
#if defined(nginx_version) && (nginx_version < 8017)
dctx->status = NGX_HTTP_SERVICE_UNAVAILABLE;
#endif
return NGX_AGAIN;
}
/* set up the peer's drizzle connection */
dc = ngx_pcalloc(dscf->pool, sizeof(drizzle_con_st));
if (dc == NULL) {
#if defined(nginx_version) && (nginx_version >= 8017)
return NGX_ERROR;
#else
goto failed;
#endif
}
dp->drizzle_con = dc;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"drizzle creating connection");
(void) drizzle_con_create(&dscf->drizzle, dc);
/* set protocol for the drizzle connection */
if (peer->protocol == ngx_http_mysql_protocol) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"drizzle using mysql protocol");
drizzle_con_add_options(dc, DRIZZLE_CON_MYSQL);
}
/* set dbname for the drizzle connection */
if (dp->dbname.len) {
dbname = dp->dbname;
} else {
dbname = peer->dbname;
}
ngx_memcpy(dc->db, dbname.data, dbname.len);
dc->db[dbname.len] = '\0';
/* set user for the drizzle connection */
ngx_memcpy(dc->user, peer->user.data, peer->user.len);
dc->user[peer->user.len] = '\0';
/* set password for the drizzle connection */
ngx_memcpy(dc->password, peer->password.data, peer->password.len);
dc->password[peer->password.len] = '\0';
dd("user %s, password %s", dc->user, dc->password);
/* TODO add support for uds (unix domain socket) */
/* set host and port for the drizzle connection */
drizzle_con_set_tcp(dc, (char *) peer->host, peer->port);
/* ask drizzle to connect to the remote */
ngx_log_debug7(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"drizzle connecting: host %s, port %d, dbname \"%V\", "
"user \"%V\", pass \"%V\", dc pass \"%s\", "
"protocol %d", peer->host, (int) peer->port, &dbname,
&peer->user, &peer->password, dc->password,
(int) peer->protocol);
ret = drizzle_con_connect(dc);
if (ret != DRIZZLE_RETURN_OK && ret != DRIZZLE_RETURN_IO_WAIT) {
ngx_log_error(NGX_LOG_EMERG, pc->log, 0,
"drizzle: failed to connect: %d: %s in upstream \"%V\"",
(int) ret,
drizzle_error(&dscf->drizzle),
&peer->name);
drizzle_con_free(dc);
ngx_pfree(dscf->pool, dc);
#if defined(nginx_version) && (nginx_version >= 8017)
return NGX_DECLINED;
#else
dctx->status = NGX_HTTP_BAD_GATEWAY;
goto failed;
#endif
}
dscf->active_conns++;
/* add the file descriptor (fd) into an nginx connection structure */
fd = drizzle_con_fd(dc);
if (fd == -1) {
ngx_log_error(NGX_LOG_ERR, pc->log, 0,
"drizzle: failed to get the drizzle connection fd");
goto invalid;
}
c = ngx_get_connection(fd, pc->log);
if (c == NULL) {
ngx_log_error(NGX_LOG_ERR, pc->log, 0,
"drizzle: failed to get a free nginx connection");
goto invalid;
}
c->log = pc->log;
c->log_error = pc->log_error;
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
rev = c->read;
wev = c->write;
rev->log = pc->log;
wev->log = pc->log;
pc->connection = c;
/* register the connection with the drizzle fd into the
* nginx event model */
#if 0
if (ngx_nonblocking(fd) == -1) {
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
ngx_nonblocking_n " failed");
goto invalid;
}
#endif
if (ret == DRIZZLE_RETURN_IO_WAIT) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"drizzle get peer: still connecting to remote");
dp->state = state_db_connect;
c->log->action = "connecting to drizzle upstream";
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"drizzle get peer: already connected to remote");
/* to ensure send_query sets corresponding timers */
dp->state = state_db_idle;
}
if (ngx_add_conn) {
dd("Found ngx_add_conn");
if (ngx_add_conn(c) == NGX_ERROR) {
goto invalid;
}
if (ret == DRIZZLE_RETURN_IO_WAIT) {
dd("returned NGX_AGAIN!!!");
return NGX_AGAIN;
}
/* ret == DRIZZLE_RETURN_OK */
wev->ready = 1;
return NGX_DONE;
}
if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
/* kqueue */
event = NGX_CLEAR_EVENT;
} else {
/* select, poll, /dev/poll */
event = NGX_LEVEL_EVENT;
}
if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, pc->log, 0,
"drizzle: failed to add connection into nginx event model");
goto invalid;
}
if (ret == DRIZZLE_RETURN_IO_WAIT) {
if (ngx_add_event(wev, NGX_WRITE_EVENT, event) != NGX_OK) {
goto invalid;
}
return NGX_AGAIN;
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, pc->log, 0, "drizzle connected");
wev->ready = 1;
/* ret == DRIZZLE_RETURN_OK */
return NGX_DONE;
invalid:
ngx_http_upstream_drizzle_free_connection(pc->log, pc->connection,
dc, dscf);
#if defined(nginx_version) && (nginx_version >= 8017)
return NGX_ERROR;
#else
failed:
/* a bit hack-ish way to return error response (setup part) */
pc->connection = ngx_get_connection(0, pc->log);
return NGX_AGAIN;
#endif
}
static void
ngx_http_upstream_drizzle_free_peer(ngx_peer_connection_t *pc,
void *data, ngx_uint_t state)
{
ngx_http_upstream_drizzle_peer_data_t *dp = data;
ngx_http_upstream_drizzle_srv_conf_t *dscf;
#if defined(nginx_version) && (nginx_version < 8017)
if (data == NULL) {
return;
}
#endif
if (pc && pc->log) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"drizzle free peer");
}
dscf = dp->srv_conf;
if (dp->drizzle_con && dp->drizzle_res.con) {
dd("before drizzle result free");
dd("%p vs. %p", dp->drizzle_res.con, dp->drizzle_con);
drizzle_result_free(&dp->drizzle_res);
dd("after drizzle result free");
}
if (pc != NULL) {
if (dscf->max_cached) {
ngx_http_drizzle_keepalive_free_peer(pc, dp, dscf, state);
}
if (pc->connection) {
dd("actually free the drizzle connection");
ngx_http_upstream_drizzle_free_connection(pc->log, pc->connection,
dp->drizzle_con, dscf);
dp->drizzle_con = NULL;
pc->connection = NULL;
}
}
}
static ngx_int_t
ngx_http_drizzle_output_filter(void *data, ngx_chain_t *in)
{
ngx_http_request_t *r = data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"drizzle output filter");
/* just to ensure u->reinit_request always gets called for
* upstream_next */
r->upstream->request_sent = 1;
(void) ngx_http_drizzle_process_events(r);
/* discard the ret val from process events because
* we can only return NGX_AGAIN here to prevent
* ngx_http_upstream_process_header from being called
* and avoid u->write_event_handler to be set to
* ngx_http_upstream_dummy. */
return NGX_AGAIN;
}
ngx_flag_t
ngx_http_upstream_drizzle_is_my_peer(const ngx_peer_connection_t *peer)
{
return (peer->get == ngx_http_upstream_drizzle_get_peer);
}
void
ngx_http_upstream_drizzle_free_connection(ngx_log_t *log,
ngx_connection_t *c, drizzle_con_st *dc,
ngx_http_upstream_drizzle_srv_conf_t *dscf)
{
ngx_event_t *rev, *wev;
dd("drizzle free peer connection");
dscf->active_conns--;
if (dc) {
dd("before con free");
drizzle_con_free(dc);
dd("after con free");
ngx_pfree(dscf->pool, dc);
}
if (c) {
/* dd("c pool: %p", c->pool); */
rev = c->read;
wev = c->write;
if (rev->timer_set) {
ngx_del_timer(rev);
}
if (wev->timer_set) {
ngx_del_timer(wev);
}
if (ngx_del_conn) {
ngx_del_conn(c, NGX_CLOSE_EVENT);
} else {
if (rev->active || rev->disabled) {
ngx_del_event(rev, NGX_READ_EVENT, NGX_CLOSE_EVENT);
}
if (wev->active || wev->disabled) {
ngx_del_event(wev, NGX_WRITE_EVENT, NGX_CLOSE_EVENT);
}
}
#if defined(nginx_version) && nginx_version >= 1007005
if (rev->posted) {
#else
if (rev->prev) {
#endif
ngx_delete_posted_event(rev);
}
#if defined(nginx_version) && nginx_version >= 1007005
if (wev->posted) {
#else
if (wev->prev) {
#endif
ngx_delete_posted_event(wev);
}
rev->closed = 1;
wev->closed = 1;
ngx_free_connection(c);
c->fd = (ngx_socket_t) -1;
}
}
ngx_http_upstream_srv_conf_t *
ngx_http_upstream_drizzle_add(ngx_http_request_t *r, ngx_url_t *url)
{
ngx_http_upstream_main_conf_t *umcf;
ngx_http_upstream_srv_conf_t **uscfp;
ngx_uint_t i;
umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
uscfp = umcf->upstreams.elts;
for (i = 0; i < umcf->upstreams.nelts; i++) {
if (uscfp[i]->host.len != url->host.len
|| ngx_strncasecmp(uscfp[i]->host.data, url->host.data,
url->host.len) != 0)
{
dd("upstream_add: host not match");
continue;
}
if (uscfp[i]->port != url->port) {
dd("upstream_add: port not match: %d != %d",
(int) uscfp[i]->port, (int) url->port);
continue;
}
#if !defined(nginx_version) || nginx_version < 1011006
if (uscfp[i]->default_port && url->default_port
&& uscfp[i]->default_port != url->default_port)
{
dd("upstream_add: default_port not match");
continue;
}
#endif
return uscfp[i];
}
dd("No upstream found: %.*s", (int) url->host.len, url->host.data);
return NULL;
}
static void
ngx_http_upstream_drizzle_cleanup(void *data)
{
drizzle_st *drizzle = data;
drizzle_free(drizzle);
}
================================================
FILE: src/ngx_http_drizzle_upstream.h
================================================
#ifndef NGX_HTTP_DRIZZLE_UPSTREAM_H
#define NGX_HTTP_DRIZZLE_UPSTREAM_H
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
#include "ngx_http_drizzle_module.h"
typedef enum {
drizzle_keepalive_overflow_ignore = 0,
drizzle_keepalive_overflow_reject
} ngx_http_drizzle_keepalive_overflow_t;
typedef enum {
ngx_http_drizzle_protocol = 0,
ngx_http_mysql_protocol
} ngx_http_upstream_drizzle_protocol_t;
typedef struct {
#if defined(nginx_version) && nginx_version >= 8022
ngx_addr_t *addrs;
#else
ngx_peer_addr_t *addrs;
#endif
ngx_uint_t naddrs;
in_port_t port;
ngx_str_t user;
ngx_str_t password;
ngx_str_t dbname;
ngx_str_t set_names_query;
ngx_http_upstream_drizzle_protocol_t protocol;
} ngx_http_upstream_drizzle_server_t;
typedef struct {
struct sockaddr *sockaddr;
socklen_t socklen;
ngx_str_t name;
in_port_t port;
ngx_str_t user;
ngx_str_t password;
ngx_str_t dbname;
ngx_str_t *set_names_query;
u_char *host;
ngx_http_upstream_drizzle_protocol_t protocol;
} ngx_http_upstream_drizzle_peer_t;
typedef struct {
unsigned single;
ngx_uint_t number;
ngx_str_t *name;
ngx_http_upstream_drizzle_peer_t peer[1];
} ngx_http_upstream_drizzle_peers_t;
typedef struct {
ngx_http_upstream_drizzle_peers_t *peers;
/* TODO: we might need "tried" from round robin peer data */
ngx_uint_t current;
/* of ngx_http_upstream_drizzle_server_t */
ngx_array_t *servers;
drizzle_st drizzle;
ngx_pool_t *pool;
/* keepalive related fields */
unsigned single;
ngx_queue_t free;
ngx_queue_t cache;
ngx_uint_t active_conns;
ngx_uint_t max_cached;
ngx_http_drizzle_keepalive_overflow_t overflow;
} ngx_http_upstream_drizzle_srv_conf_t;
typedef struct {
ngx_http_drizzle_loc_conf_t *loc_conf;
ngx_http_upstream_drizzle_srv_conf_t *srv_conf;
ngx_http_upstream_t *upstream;
ngx_http_request_t *request;
ngx_str_t dbname;
ngx_str_t query;
ngx_http_drizzle_state_t state;
drizzle_con_st *drizzle_con;
drizzle_result_st drizzle_res;
drizzle_column_st drizzle_col;
uint64_t drizzle_row;
ngx_str_t name;
ngx_chain_t **last_out;
ngx_buf_t *out_buf;
ngx_buf_t cached;
ngx_buf_t postponed;
size_t avail_out;
ngx_str_t charset;
ngx_str_t *set_names_query;
struct sockaddr sockaddr;
/* how many times this connection has been successfully used */
ngx_uint_t used;
unsigned failed:1;
unsigned seen_stream_end:1;
unsigned has_set_names:1;
unsigned enable_charset:1;
} ngx_http_upstream_drizzle_peer_data_t;
char *ngx_http_upstream_drizzle_server(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
void *ngx_http_upstream_drizzle_create_srv_conf(ngx_conf_t *cf);
ngx_flag_t ngx_http_upstream_drizzle_is_my_peer(
const ngx_peer_connection_t *peer);
void ngx_http_upstream_drizzle_free_connection(ngx_log_t *log,
ngx_connection_t *c, drizzle_con_st *dc,
ngx_http_upstream_drizzle_srv_conf_t *dscf);
ngx_http_upstream_srv_conf_t *ngx_http_upstream_drizzle_add(
ngx_http_request_t *r, ngx_url_t *url);
#endif /* NGX_HTTP_DRIZZLE_UPSTREAM_H */
================================================
FILE: src/ngx_http_drizzle_util.c
================================================
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_drizzle_module.h"
#include "ngx_http_drizzle_util.h"
#include "ngx_http_drizzle_handler.h"
#include "ngx_http_drizzle_processor.h"
#include <ngx_core.h>
#include <ngx_http.h>
static ngx_int_t ngx_http_upstream_dbd_reinit(ngx_http_request_t *r,
ngx_http_upstream_t *u);
static void ngx_http_upstream_dbd_handler(ngx_event_t *ev);
static void ngx_http_upstream_dbd_connect(ngx_http_request_t *r,
ngx_http_upstream_t *u);
static void ngx_http_upstream_dbd_cleanup(void *data);
static void ngx_http_upstream_dbd_wr_check_broken_connection(
ngx_http_request_t *r);
static void ngx_http_upstream_dbd_rd_check_broken_connection(
ngx_http_request_t *r);
static void ngx_http_upstream_dbd_check_broken_connection(ngx_http_request_t *r,
ngx_event_t *ev);
void *
ngx_http_drizzle_get_peer_data(ngx_http_request_t *r)
{
ngx_http_drizzle_ctx_t *dctx;
/* Prefer the ctx-saved pointer because r->upstream->peer.data may have
* been wrapped by the standard upstream keepalive (implicit since
* nginx 1.29.7) or by another balancer module. Fall back to peer.data
* for the rare case where the ctx is missing. */
dctx = ngx_http_get_module_ctx(r, ngx_http_drizzle_module);
if (dctx != NULL && dctx->peer_data != NULL) {
return dctx->peer_data;
}
return r->upstream ? r->upstream->peer.data : NULL;
}
ngx_int_t
ngx_http_drizzle_set_header(ngx_http_request_t *r, ngx_str_t *key,
ngx_str_t *value)
{
ngx_uint_t i;
ngx_table_elt_t *h;
ngx_list_part_t *part;
dd("entered set_header");
part = &r->headers_out.headers.part;
h = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
h = part->elts;
i = 0;
}
if (h[i].key.len == key->len
&& ngx_strncasecmp(h[i].key.data, key->data, h[i].key.len) == 0)
{
if (value->len == 0) {
h[i].hash = 0;
}
h[i].value = *value;
return NGX_OK;
}
}
if (value->len == 0) {
return NGX_OK;
}
h = ngx_list_push(&r->headers_out.headers);
if (h == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
h->hash = 1;
h->key = *key;
h->value = *value;
return NGX_OK;
}
/* the following functions are copied directly from
ngx_http_upstream.c in nginx 0.8.30, just because
they're static. sigh. */
void
ngx_http_upstream_drizzle_finalize_request(ngx_http_request_t *r,
ngx_http_upstream_t *u, ngx_int_t rc)
{
#if !defined(nginx_version) || nginx_version < 1009001
ngx_time_t *tp;
#endif
dd("enter");
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"finalize http upstream request: %i", rc);
if (u->cleanup) {
*u->cleanup = NULL;
}
if (u->resolved && u->resolved->ctx) {
ngx_resolve_name_done(u->resolved->ctx);
u->resolved->ctx = NULL;
}
#if defined(nginx_version) && nginx_version >= 1009001
if (u->state && u->state->response_time) {
u->state->response_time = ngx_current_msec - u->state->response_time;
#else
if (u->state && u->state->response_sec) {
tp = ngx_timeofday();
u->state->response_sec = tp->sec - u->state->response_sec;
u->state->response_msec = tp->msec - u->state->response_msec;
#endif
if (u->pipe) {
u->state->response_length = u->pipe->read_length;
}
}
if (u->finalize_request) {
u->finalize_request(r, rc);
}
if (u->peer.free) {
dd("before free peer");
u->peer.free(&u->peer, u->peer.data, 0);
dd("after free peer");
}
dd("about to free peer 2, c: %p, r pool: %p", u->peer.connection, r->pool);
if (u->peer.connection) {
#if 0 /* libdrizzle doesn't support SSL, was: (NGX_HTTP_SSL) */
/* TODO: do not shutdown persistent connection */
if (u->peer.connection->ssl) {
/*
* We send the "close notify" shutdown alert to the upstream only
* and do not wait its "close notify" shutdown alert.
* It is acceptable according to the TLS standard.
*/
u->peer.connection->ssl->no_wait_shutdown = 1;
(void) ngx_ssl_shutdown(u->peer.connection);
}
#endif
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"drizzle close http upstream connection: %d",
u->peer.connection->fd);
dd("r pool: %p, c pool: %p", r->pool, u->peer.connection->pool);
ngx_close_connection(u->peer.connection);
}
u->peer.connection = NULL;
if (u->pipe && u->pipe->temp_file) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http upstream temp fd: %d",
u->pipe->temp_file->file.fd);
}
if (u->header_sent
&& (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))
{
rc = 0;
}
if (rc == NGX_DECLINED) {
return;
}
r->connection->log->action = "sending to client";
if (rc == 0) {
rc = ngx_http_send_special(r, NGX_HTTP_LAST);
}
ngx_http_finalize_request(r, rc);
}
void
ngx_http_upstream_drizzle_next(ngx_http_request_t *r,
ngx_http_upstream_t *u, ngx_int_t ft_type)
{
ngx_uint_t status, state;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http next upstream, %xi", ft_type);
#if 0
ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);
#endif
if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) {
state = NGX_PEER_NEXT;
} else {
state = NGX_PEER_FAILED;
}
if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) {
u->peer.free(&u->peer, u->peer.data, state);
}
if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,
"upstream timed out");
}
if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {
status = 0;
} else {
switch (ft_type) {
case NGX_HTTP_UPSTREAM_FT_TIMEOUT:
status = NGX_HTTP_GATEWAY_TIME_OUT;
break;
case NGX_HTTP_UPSTREAM_FT_HTTP_500:
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
break;
case NGX_HTTP_UPSTREAM_FT_HTTP_404:
status = NGX_HTTP_NOT_FOUND;
break;
/*
* NGX_HTTP_UPSTREAM_FT_BUSY_LOCK and NGX_HTTP_UPSTREAM_FT_MAX_WAITING
* never reach here
*/
default:
status = NGX_HTTP_BAD_GATEWAY;
}
}
if (r->connection->error) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_CLIENT_CLOSED_REQUEST);
return;
}
if (status) {
u->state->status = status;
if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) {
ngx_http_upstream_drizzle_finalize_request(r, u, status);
return;
}
}
if (u->peer.connection) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"close http upstream connection: %d",
u->peer.connection->fd);
#if 0 /* libdrizzle doesn't support SSL, was: (NGX_HTTP_SSL) */
if (u->peer.connection->ssl) {
u->peer.connection->ssl->no_wait_shutdown = 1;
u->peer.connection->ssl->no_send_shutdown = 1;
(void) ngx_ssl_shutdown(u->peer.connection);
}
#endif
dd("r pool: %p, c pool: %p", r->pool, u->peer.connection->pool);
ngx_close_connection(u->peer.connection);
}
#if 0
if (u->conf->busy_lock && !u->busy_locked) {
ngx_http_upstream_busy_lock(p);
return;
}
#endif
/* TODO: ngx_http_upstream_connect(r, u); */
if (status == 0) {
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_http_upstream_drizzle_finalize_request(r, u, status);
}
ngx_int_t
ngx_http_upstream_drizzle_test_connect(ngx_connection_t *c)
{
int err;
socklen_t len;
#if (NGX_HAVE_KQUEUE)
if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
if (c->write->pending_eof) {
c->log->action = "connecting to upstream";
(void) ngx_connection_error(c, c->write->kq_errno,
"kevent() reported that connect() "
"failed");
return NGX_ERROR;
}
} else
#endif
{
err = 0;
len = sizeof(int);
/*
* BSDs and Linux return 0 and set a pending error in err
* Solaris returns -1 and sets errno
*/
if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len)
== -1)
{
err = ngx_errno;
}
if (err) {
c->log->action = "connecting to upstream";
(void) ngx_connection_error(c, err, "connect() failed");
return NGX_ERROR;
}
}
return NGX_OK;
}
void
ngx_http_upstream_dbd_init(ngx_http_request_t *r)
{
ngx_connection_t *c;
c = r->connection;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http init upstream, client timer: %d", c->read->timer_set);
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
if (!c->write->active) {
if (ngx_add_event(c->write, NGX_WRITE_EVENT, NGX_CLEAR_EVENT)
== NGX_ERROR)
{
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
}
}
ngx_http_upstream_dbd_init_request(r);
}
void
ngx_http_upstream_dbd_init_request(ngx_http_request_t *r)
{
ngx_str_t *host;
ngx_uint_t i;
ngx_resolver_ctx_t *ctx, temp;
ngx_http_cleanup_t *cln;
ngx_http_upstream_t *u;
ngx_http_core_loc_conf_t *clcf;
ngx_http_upstream_srv_conf_t *uscf, **uscfp;
ngx_http_upstream_main_conf_t *umcf;
#if defined(nginx_version) && nginx_version >= 8011
if (r->aio) {
return;
}
#endif
u = r->upstream;
#if 0 && (NGX_HTTP_CACHE)
if (u->conf->cache) {
ngx_int_t rc;
rc = ngx_http_upstream_cache(r, u);
if (rc == NGX_BUSY) {
r->write_event_handler = ngx_http_upstream_init_request;
return;
}
r->write_event_handler = ngx_http_request_empty_handler;
if (rc == NGX_DONE) {
return;
}
if (rc != NGX_DECLINED) {
ngx_http_finalize_request(r, rc);
return;
}
}
#endif
u->store = (u->conf->store || u->conf->store_lengths);
if (!u->store && !r->post_action && !u->conf->ignore_client_abort) {
r->read_event_handler =
ngx_http_upstream_dbd_rd_check_broken_connection;
r->write_event_handler =
ngx_http_upstream_dbd_wr_check_broken_connection;
}
if (r->request_body) {
u->request_bufs = r->request_body->bufs;
}
if (u->create_request(r) != NGX_OK) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
#if 0
#if defined(nginx_version) && nginx_version >= 8022
u->peer.local = u->conf->local;
#endif
#endif
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
#if defined(nginx_version) && nginx_version >= 8011
u->output.alignment = clcf->directio_alignment;
#endif
u->output.pool = r->pool;
u->output.bufs.num = 1;
u->output.bufs.size = clcf->client_body_buffer_size;
u->output.output_filter = ngx_chain_writer;
u->output.filter_ctx = &u->writer;
u->writer.pool = r->pool;
if (r->upstream_states == NULL) {
r->upstream_states = ngx_array_create(r->pool, 1,
sizeof(ngx_http_upstream_state_t));
if (r->upstream_states == NULL) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
} else {
u->state = ngx_array_push(r->upstream_states);
if (u->state == NULL) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
}
cln = ngx_http_cleanup_add(r, 0);
if (cln == NULL) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
cln->handler = ngx_http_upstream_dbd_cleanup;
cln->data = r;
u->cleanup = &cln->handler;
if (u->resolved == NULL) {
uscf = u->conf->upstream;
} else {
if (u->resolved->sockaddr) {
if (ngx_http_upstream_create_round_robin_peer(r, u->resolved)
!= NGX_OK)
{
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
ngx_http_upstream_dbd_connect(r, u);
return;
}
host = &u->resolved->host;
umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
uscfp = umcf->upstreams.elts;
for (i = 0; i < umcf->upstreams.nelts; i++) {
uscf = uscfp[i];
if (uscf->host.len == host->len
&& ((uscf->port == 0 && u->resolved->no_port)
|| uscf->port == u->resolved->port)
&& ngx_memcmp(uscf->host.data, host->data, host->len) == 0)
{
goto found;
}
}
temp.name = *host;
ctx = ngx_resolve_start(clcf->resolver, &temp);
if (ctx == NULL) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
if (ctx == NGX_NO_RESOLVER) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"no resolver defined to resolve %V", host);
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_BAD_GATEWAY);
return;
}
#if 0
ctx->name = *host;
ctx->type = NGX_RESOLVE_A;
ctx->handler = ngx_http_upstream_resolve_handler;
ctx->data = r;
ctx->timeout = clcf->resolver_timeout;
u->resolved->ctx = ctx;
if (ngx_resolve_name(ctx) != NGX_OK) {
u->resolved->ctx = NULL;
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
#endif
return;
}
found:
if (uscf->peer.init(r, uscf) != NGX_OK) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
ngx_http_upstream_dbd_connect(r, u);
}
static void
ngx_http_upstream_dbd_connect(ngx_http_request_t *r, ngx_http_upstream_t *u)
{
ngx_int_t rc;
#if !defined(nginx_version) || nginx_version < 1009001
ngx_time_t *tp;
#endif
ngx_connection_t *c;
r->connection->log->action = "connecting to upstream";
#if defined(nginx_version) && nginx_version >= 1009001
if (u->state && u->state->response_time) {
u->state->response_time = ngx_current_msec - u->state->response_time;
}
#else
if (u->state && u->state->response_sec) {
tp = ngx_timeofday();
u->state->response_sec = tp->sec - u->state->response_sec;
u->state->response_msec = tp->msec - u->state->response_msec;
}
#endif
u->state = ngx_array_push(r->upstream_states);
if (u->state == NULL) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
#if defined(nginx_version) && nginx_version >= 1009001
u->state->response_time = ngx_current_msec;
u->state->connect_time = (ngx_msec_t) -1;
u->state->header_time = (ngx_msec_t) -1;
#else
tp = ngx_timeofday();
u->state->response_sec = tp->sec;
u->state->response_msec = tp->msec;
#endif
rc = ngx_event_connect_peer(&u->peer);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http upstream connect: %i", rc);
if (rc == NGX_ERROR) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
u->state->peer = u->peer.name;
if (rc == NGX_BUSY) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams");
ngx_http_upstream_drizzle_next(r, u, NGX_HTTP_UPSTREAM_FT_NOLIVE);
return;
}
if (rc == NGX_DECLINED) {
ngx_http_upstream_drizzle_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
return;
}
/* rc == NGX_OK || rc == NGX_AGAIN */
c = u->peer.connection;
c->data = r;
c->write->handler = ngx_http_upstream_dbd_handler;
c->read->handler = ngx_http_upstream_dbd_handler;
u->write_event_handler = ngx_http_drizzle_wev_handler;
u->read_event_handler = ngx_http_drizzle_rev_handler;
c->sendfile &= r->connection->sendfile;
u->output.sendfile = c->sendfile;
c->pool = r->pool;
c->log = r->connection->log;
c->read->log = c->log;
c->write->log = c->log;
/* init or reinit the ngx_output_chain() and ngx_chain_writer() contexts */
u->writer.out = NULL;
u->writer.last = &u->writer.out;
u->writer.connection = c;
u->writer.limit = 0;
if (u->request_sent) {
if (ngx_http_upstream_dbd_reinit(r, u) != NGX_OK) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
}
if (r->request_body
&& r->request_body->buf
&& r->request_body->temp_file
&& r == r->main)
{
/*
* the r->request_body->buf can be reused for one request only,
* the subrequests should allocate their own temporay bufs
*/
u->output.free = ngx_alloc_chain_link(r->pool);
if (u->output.free == NULL) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
u->output.free->buf = r->request_body->buf;
u->output.free->next = NULL;
u->output.allocated = 1;
r->request_body->buf->pos = r->request_body->buf->start;
r->request_body->buf->last = r->request_body->buf->start;
r->request_body->buf->tag = u->output.tag;
}
u->request_sent = 0;
if (rc == NGX_AGAIN) {
ngx_add_timer(c->write, u->conf->connect_timeout);
return;
}
#if 0 /* libdrizzle doesn't support SSL, was: (NGX_HTTP_SSL) */
if (u->ssl && c->ssl == NULL) {
ngx_http_upstream_ssl_init_connection(r, u, c);
return;
}
#endif
#if 0
ngx_http_upstream_send_request(r, u);
#endif
dd("connection error: %d", c->error);
ngx_http_drizzle_set_libdrizzle_ready(r);
(void) ngx_http_drizzle_process_events(r);
}
static void
ngx_http_upstream_dbd_rd_check_broken_connection(ngx_http_request_t *r)
{
ngx_http_upstream_dbd_check_broken_connection(r, r->connection->read);
}
static void
ngx_http_upstream_dbd_wr_check_broken_connection(ngx_http_request_t *r)
{
ngx_http_upstream_dbd_check_broken_connection(r, r->connection->write);
}
static void
ngx_http_upstream_dbd_check_broken_connection(ngx_http_request_t *r,
ngx_event_t *ev)
{
int n;
char buf[1];
ngx_err_t err;
ngx_int_t event;
ngx_connection_t *c;
ngx_http_upstream_t *u;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ev->log, 0,
"http upstream check client, write event:%d, \"%V\"",
ev->write, &r->uri);
c = r->connection;
u = r->upstream;
if (c->error) {
if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && ev->active) {
event = ev->write ? NGX_WRITE_EVENT : NGX_READ_EVENT;
if (ngx_del_event(ev, event, 0) != NGX_OK) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
}
if (!u->cacheable) {
ngx_http_upstream_drizzle_finalize_request(r, u,
NGX_HTTP_CLIENT_CLOSED_REQUEST);
}
return;
}
#if (NGX_HAVE_KQUEUE)
if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
if (!ev->pending_eof) {
return;
}
ev->eof = 1;
gitextract_0fqk8n_f/ ├── .gitattributes ├── .github/ │ └── workflows/ │ └── coverity.yml ├── .gitignore ├── .travis.yml ├── Changes ├── README.markdown ├── config ├── doc/ │ └── design ├── src/ │ ├── ddebug.h │ ├── ngx_http_drizzle_checker.c │ ├── ngx_http_drizzle_checker.h │ ├── ngx_http_drizzle_handler.c │ ├── ngx_http_drizzle_handler.h │ ├── ngx_http_drizzle_keepalive.c │ ├── ngx_http_drizzle_keepalive.h │ ├── ngx_http_drizzle_module.c │ ├── ngx_http_drizzle_module.h │ ├── ngx_http_drizzle_output.c │ ├── ngx_http_drizzle_output.h │ ├── ngx_http_drizzle_processor.c │ ├── ngx_http_drizzle_processor.h │ ├── ngx_http_drizzle_quoting.c │ ├── ngx_http_drizzle_quoting.h │ ├── ngx_http_drizzle_upstream.c │ ├── ngx_http_drizzle_upstream.h │ ├── ngx_http_drizzle_util.c │ ├── ngx_http_drizzle_util.h │ └── resty_dbd_stream.h ├── t/ │ ├── 000_init.t │ ├── bugs.t │ ├── charset/ │ │ ├── 000_init.t │ │ ├── errors.t │ │ ├── keepalive.t │ │ ├── methods.t │ │ ├── sanity.t │ │ └── timeout.t │ ├── errors.t │ ├── keepalive.t │ ├── methods.t │ ├── sanity.t │ ├── status.t │ └── timeout.t ├── util/ │ ├── build.sh │ ├── ngx-links │ ├── update-readme.sh │ └── wiki2pod.pl └── valgrind.suppress
SYMBOL INDEX (86 symbols across 14 files)
FILE: src/ddebug.h
function ngx_inline (line 32) | static ngx_inline void
function ngx_inline (line 50) | static ngx_inline void
FILE: src/ngx_http_drizzle_checker.c
function ngx_int_t (line 18) | ngx_int_t
function ngx_int_t (line 26) | ngx_int_t
function ngx_int_t (line 34) | ngx_int_t
function ngx_int_t (line 42) | ngx_int_t
function ngx_int_t (line 50) | ngx_int_t
function ngx_int_t (line 58) | ngx_int_t
FILE: src/ngx_http_drizzle_handler.c
function ngx_int_t (line 43) | ngx_int_t
function ngx_http_drizzle_wev_handler (line 210) | void
function ngx_http_drizzle_rev_handler (line 245) | void
function ngx_int_t (line 279) | static ngx_int_t
function ngx_int_t (line 288) | static ngx_int_t
function ngx_http_drizzle_abort_request (line 303) | static void
function ngx_http_drizzle_finalize_request (line 309) | static void
function ngx_int_t (line 316) | static ngx_int_t
function ngx_int_t (line 327) | static ngx_int_t
function ngx_int_t (line 340) | static ngx_int_t
function ngx_http_drizzle_set_libdrizzle_ready (line 353) | void
function ngx_int_t (line 392) | ngx_int_t
FILE: src/ngx_http_drizzle_keepalive.c
function ngx_int_t (line 132) | ngx_int_t
function ngx_int_t (line 160) | ngx_int_t
function ngx_int_t (line 206) | ngx_int_t
function ngx_http_drizzle_keepalive_free_peer (line 259) | void
function ngx_http_drizzle_keepalive_dummy_handler (line 356) | static void
function ngx_http_drizzle_keepalive_close_handler (line 364) | static void
FILE: src/ngx_http_drizzle_keepalive.h
type ngx_http_drizzle_keepalive_cache_t (line 11) | typedef struct {
FILE: src/ngx_http_drizzle_module.c
function ngx_int_t (line 526) | static ngx_int_t
function ngx_int_t (line 545) | static ngx_int_t
FILE: src/ngx_http_drizzle_module.h
type ngx_drizzle_mixed_t (line 45) | typedef struct {
type ngx_drizzle_http_method_t (line 52) | typedef struct {
type ngx_http_drizzle_loc_conf_t (line 58) | typedef struct {
type ngx_http_drizzle_ctx_t (line 90) | typedef struct {
type ngx_http_drizzle_state_t (line 103) | typedef enum {
FILE: src/ngx_http_drizzle_output.c
function ngx_int_t (line 49) | ngx_int_t
function ngx_int_t (line 188) | ngx_int_t
function ngx_int_t (line 296) | ngx_int_t
function ngx_int_t (line 364) | ngx_int_t
function ngx_int_t (line 390) | ngx_int_t
function rds_col_type_t (line 452) | static rds_col_type_t
function u_char (line 547) | static u_char *
function ngx_int_t (line 576) | static ngx_int_t
function u_char (line 613) | static u_char *
function ngx_int_t (line 646) | static ngx_int_t
FILE: src/ngx_http_drizzle_processor.c
function ngx_int_t (line 37) | ngx_int_t
function ngx_int_t (line 130) | static ngx_int_t
function ngx_int_t (line 176) | static ngx_int_t
function ngx_int_t (line 299) | static ngx_int_t
function ngx_int_t (line 374) | static ngx_int_t
function ngx_http_upstream_drizzle_done (line 516) | void
FILE: src/ngx_http_drizzle_quoting.h
type ngx_int_t (line 6) | typedef ngx_int_t (* ngx_http_drizzle_type_checker_pt) (ngx_str_t *value,
type ngx_http_drizzle_quotes_type_t (line 9) | typedef enum {
type ngx_http_drizzle_var_type_t (line 18) | typedef struct {
type ngx_http_drizzle_var_to_quote_t (line 27) | typedef struct {
FILE: src/ngx_http_drizzle_upstream.c
function ngx_int_t (line 302) | static ngx_int_t
function ngx_int_t (line 398) | static ngx_int_t
function ngx_int_t (line 548) | static ngx_int_t
function ngx_http_upstream_drizzle_free_peer (line 872) | static void
function ngx_int_t (line 920) | static ngx_int_t
function ngx_flag_t (line 944) | ngx_flag_t
function ngx_http_upstream_srv_conf_t (line 1021) | ngx_http_upstream_srv_conf_t *
function ngx_http_upstream_drizzle_cleanup (line 1066) | static void
FILE: src/ngx_http_drizzle_upstream.h
type ngx_http_drizzle_keepalive_overflow_t (line 11) | typedef enum {
type ngx_http_upstream_drizzle_protocol_t (line 18) | typedef enum {
type ngx_http_upstream_drizzle_server_t (line 25) | typedef struct {
type ngx_http_upstream_drizzle_peer_t (line 45) | typedef struct {
type ngx_http_upstream_drizzle_peers_t (line 61) | typedef struct {
type ngx_http_upstream_drizzle_srv_conf_t (line 71) | typedef struct {
type ngx_http_upstream_drizzle_peer_data_t (line 96) | typedef struct {
FILE: src/ngx_http_drizzle_util.c
function ngx_int_t (line 55) | ngx_int_t
function ngx_http_upstream_drizzle_next (line 225) | void
function ngx_int_t (line 331) | ngx_int_t
function ngx_http_upstream_dbd_init (line 376) | void
function ngx_http_upstream_dbd_init_request (line 406) | void
function ngx_http_upstream_dbd_connect (line 611) | static void
function ngx_http_upstream_dbd_rd_check_broken_connection (line 775) | static void
function ngx_http_upstream_dbd_wr_check_broken_connection (line 782) | static void
function ngx_http_upstream_dbd_check_broken_connection (line 789) | static void
function ngx_http_upstream_dbd_cleanup (line 924) | static void
function ngx_http_upstream_dbd_handler (line 945) | static void
function ngx_int_t (line 978) | static ngx_int_t
function ngx_int_t (line 1042) | ngx_int_t
function ngx_http_drizzle_get_num_size (line 1102) | size_t
function ngx_uint_t (line 1116) | ngx_uint_t
FILE: src/resty_dbd_stream.h
type rds_rough_col_type_t (line 15) | typedef enum {
type rds_col_type_t (line 32) | typedef enum {
Condensed preview — 47 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (332K chars).
[
{
"path": ".gitattributes",
"chars": 27,
"preview": "*.t linguist-language=Text\n"
},
{
"path": ".github/workflows/coverity.yml",
"chars": 2957,
"preview": "name: Coverity\n\non:\n schedule:\n - cron: \"0 0 * * *\"\n\njobs:\n scan:\n runs-on: ubuntu-18.04\n if: ${{ github.reposi"
},
{
"path": ".gitignore",
"chars": 620,
"preview": "*.mobi\ngenmobi.sh\n.libs\n*.swp\n*.slo\n*.la\n*.swo\n*.lo\n*~\n*.o\nprint.txt\n.rsync\n*.tar.gz\ndist\nbuild[789]\nbuild\ntags\nupdate-r"
},
{
"path": ".travis.yml",
"chars": 3429,
"preview": "sudo: required\ndist: focal \n\nbranches:\n only:\n - \"master\"\n\nos: linux\n\nlanguage: c\n\ncache:\n apt: true\n directories:"
},
{
"path": "Changes",
"chars": 1588,
"preview": "v0.1.0 - 5 July 2011\n* now we require at least libdrizzle 1.0, which supports official mysql 5.5+ and is much more stabl"
},
{
"path": "README.markdown",
"chars": 32803,
"preview": "Name\n====\n\ndrizzle-nginx-module - Upstream module for talking to MySQL and Drizzle directly\n\n*This module is not distrib"
},
{
"path": "config",
"chars": 3608,
"preview": "ngx_feature_name=\nngx_feature_run=no\nngx_feature_incs=\"#include <libdrizzle/drizzle_client.h>\"\nngx_feature_test=\"drizzle"
},
{
"path": "doc/design",
"chars": 765,
"preview": "main request to location \"/drizzle\" (handled by mod_drizzle)\n\t-> mod_drizzle content handler (is main request)\n\t\t-> emit"
},
{
"path": "src/ddebug.h",
"chars": 3136,
"preview": "#ifndef DDEBUG_H\n#define DDEBUG_H\n\n#include <ngx_config.h>\n#include <ngx_core.h>\n\n#if defined(DDEBUG) && (DDEBUG)\n\n# d"
},
{
"path": "src/ngx_http_drizzle_checker.c",
"chars": 844,
"preview": "\n/*\n * Copyright (C) Yichun Zhang (agentzh)\n */\n\n\n#ifndef DDEBUG\n#define DDEBUG 0\n#endif\n#include \"ddebug.h\"\n\n\n#include "
},
{
"path": "src/ngx_http_drizzle_checker.h",
"chars": 573,
"preview": "#ifndef NGX_HTTP_DRIZZLE_CHECKER_H\n#define NGX_HTTP_DRIZZLE_CHECKER_H\n\n#include \"ngx_http_drizzle_module.h\"\n\nngx_int_t n"
},
{
"path": "src/ngx_http_drizzle_handler.c",
"chars": 19325,
"preview": "\n/*\n * Copyright (C) Yichun Zhang (agentzh)\n */\n\n\n#ifndef DDEBUG\n#define DDEBUG 0\n#endif\n#include \"ddebug.h\"\n\n\n#include "
},
{
"path": "src/ngx_http_drizzle_handler.h",
"chars": 535,
"preview": "#ifndef NGX_HTTP_DRIZZLE_HANDLER_H\n#define NGX_HTTP_DRIZZLE_HANDLER_H\n\n#include <ngx_core.h>\n#include <ngx_http.h>\n\n\nvoi"
},
{
"path": "src/ngx_http_drizzle_keepalive.c",
"chars": 11363,
"preview": "\n/*\n * Copyright (C) Xiaozhe Wang (chaoslawful)\n * Copyright (C) Yichun Zhang (agentzh)\n */\n\n\n#ifndef DDEBUG\n#define DDE"
},
{
"path": "src/ngx_http_drizzle_keepalive.h",
"chars": 1599,
"preview": "#ifndef NGX_HTTP_DRIZZLE_KEEPALIVE_H\n#define NGX_HTTP_DRIZZLE_KEEPALIVE_H\n\n\n#include \"ngx_http_drizzle_upstream.h\"\n#incl"
},
{
"path": "src/ngx_http_drizzle_module.c",
"chars": 16328,
"preview": "\n/*\n * Copyright (C) Xiaozhe Wang (chaoslawful)\n * Copyright (C) Yichun Zhang (agentzh)\n */\n\n\n#ifndef DDEBUG\n#define DDE"
},
{
"path": "src/ngx_http_drizzle_module.h",
"chars": 2831,
"preview": "\n/*\n * Copyright (C) Yichun Zhang (agentzh)\n */\n\n\n#ifndef NGX_HTTP_DRIZZLE_MODULE_H\n#define NGX_HTTP_DRIZZLE_MODULE_H\n\n\n"
},
{
"path": "src/ngx_http_drizzle_output.c",
"chars": 20484,
"preview": "\n/*\n * Copyright (C) Yichun Zhang (agentzh)\n */\n\n\n#ifndef DDEBUG\n#define DDEBUG 0\n#endif\n#include \"ddebug.h\"\n\n\n#include "
},
{
"path": "src/ngx_http_drizzle_output.h",
"chars": 678,
"preview": "#ifndef NGX_HTTP_DRIZZLE_OUTPUT_H\n#define NGX_HTTP_DRIZZLE_OUTPUT_H\n\n#include \"ngx_http_drizzle_upstream.h\"\n\nngx_int_t n"
},
{
"path": "src/ngx_http_drizzle_processor.c",
"chars": 15321,
"preview": "\n/*\n * Copyright (C) Xiaozhe Wang (chaoslawful)\n * Copyright (C) Yichun Zhang (agentzh)\n */\n\n\n#ifndef DDEBUG\n#define DDE"
},
{
"path": "src/ngx_http_drizzle_processor.h",
"chars": 465,
"preview": "#ifndef NGX_HTTP_DRIZZLE_PROCESSOR_H\n#define NGX_HTTP_DRIZZLE_PROCESSOR_H\n\n#include \"ngx_http_drizzle_module.h\"\n#include"
},
{
"path": "src/ngx_http_drizzle_quoting.c",
"chars": 1025,
"preview": "\n/*\n * Copyright (C) Yichun Zhang (agentzh)\n */\n\n\n#ifndef DDEBUG\n#define DDEBUG 0\n#endif\n#include \"ddebug.h\"\n\n\n#include "
},
{
"path": "src/ngx_http_drizzle_quoting.h",
"chars": 904,
"preview": "#ifndef NGX_HTTP_DRIZZLE_QUOTING_H\n#define NGX_HTTP_DRIZZLE_QUOTING_H\n\n#include \"ngx_http_drizzle_module.h\"\n\ntypedef ngx"
},
{
"path": "src/ngx_http_drizzle_upstream.c",
"chars": 29153,
"preview": "/* Copyright (C) agentzh */\n\n#ifndef DDEBUG\n#define DDEBUG 0\n#endif\n#include \"ddebug.h\"\n\n#include \"ngx_http_drizzle_modu"
},
{
"path": "src/ngx_http_drizzle_upstream.h",
"chars": 4597,
"preview": "#ifndef NGX_HTTP_DRIZZLE_UPSTREAM_H\n#define NGX_HTTP_DRIZZLE_UPSTREAM_H\n\n\n#include <ngx_core.h>\n#include <ngx_http.h>\n#i"
},
{
"path": "src/ngx_http_drizzle_util.c",
"chars": 28271,
"preview": "\n/*\n * Copyright (C) Igor Sysoev\n * Copyright (C) Yichun Zhang (agentzh)\n */\n\n\n#ifndef DDEBUG\n#define DDEBUG 0\n#endif\n#i"
},
{
"path": "src/ngx_http_drizzle_util.h",
"chars": 1419,
"preview": "#ifndef NGX_HTTP_DRIZZLE_UTIL_H\n#define NGX_HTTP_DRIZZLE_UTIL_H\n\n#include \"ddebug.h\"\n\n#include <nginx.h>\n#include <ngx_c"
},
{
"path": "src/resty_dbd_stream.h",
"chars": 2022,
"preview": "#ifndef RESTY_DBD_STREAME_H\n#define RESTY_DBD_STREAME_H\n\n#define resty_dbd_stream_version 3\n#define resty_dbd_stream_ver"
},
{
"path": "t/000_init.t",
"chars": 1575,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(1);\n\nplan tests => repeat_each() * blocks();\n\n$ENV{"
},
{
"path": "t/bugs.t",
"chars": 1062,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(2);\n\nplan tests => repeat_each() * (blocks() + 1);\n"
},
{
"path": "t/charset/000_init.t",
"chars": 1591,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(1);\n\nplan tests => repeat_each() * blocks();\n\n$ENV{"
},
{
"path": "t/charset/errors.t",
"chars": 3863,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(2);\n\nplan tests => repeat_each() * (blocks() + 2);\n"
},
{
"path": "t/charset/keepalive.t",
"chars": 11769,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(2);\n\nplan tests => repeat_each() * 2 * blocks() + 2"
},
{
"path": "t/charset/methods.t",
"chars": 10349,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(2);\n\nplan tests => repeat_each() * (blocks() * 3 - "
},
{
"path": "t/charset/sanity.t",
"chars": 11787,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(2);\n#repeat_each(1);\n\nplan tests => repeat_each() *"
},
{
"path": "t/charset/timeout.t",
"chars": 2415,
"preview": "# vi:ft=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\n#repeat_each(2);\nrepeat_each(1);\n\nplan tests => repeat_each() * block"
},
{
"path": "t/errors.t",
"chars": 3562,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(2);\n\nplan tests => repeat_each() * (blocks() + 1);\n"
},
{
"path": "t/keepalive.t",
"chars": 12144,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(2);\n\nplan tests => repeat_each() * (4 * blocks() + "
},
{
"path": "t/methods.t",
"chars": 10293,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(2);\n\nplan tests => repeat_each() * 26;\n\n$ENV{TEST_N"
},
{
"path": "t/sanity.t",
"chars": 11769,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(2);\n\nplan tests => repeat_each() * 2 * blocks() + 2"
},
{
"path": "t/status.t",
"chars": 7061,
"preview": "# vi:filetype=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\n#repeat_each(2);\n\nplan tests => repeat_each() * 2 * blocks();\n\n"
},
{
"path": "t/timeout.t",
"chars": 3761,
"preview": "# vi:ft=\n\nuse lib 'lib';\nuse Test::Nginx::Socket;\n\nrepeat_each(2);\n#repeat_each(1);\n\nplan tests => repeat_each() * (bloc"
},
{
"path": "util/build.sh",
"chars": 1373,
"preview": "#!/bin/bash\n\n# this file is mostly meant to be used by the author himself.\n\nroot=`pwd`\nversion=$1\nhome=~\nforce=$2\n\n\nngx-"
},
{
"path": "util/ngx-links",
"chars": 1298,
"preview": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\n\nuse Cwd qw( cwd );\nuse Getopt::Std;\n\nmy %opts;\ngetopts('f', \\%opts) or\n "
},
{
"path": "util/update-readme.sh",
"chars": 98,
"preview": "#!/bin/bash\n\nperl util/wiki2pod.pl doc/manpage.wiki > /tmp/a.pod && pod2text /tmp/a.pod > README\n\n"
},
{
"path": "util/wiki2pod.pl",
"chars": 3124,
"preview": "#!/usr/bin/env perl\n\nuse strict;\nuse warnings;\nuse bytes;\n\nmy @nl_counts;\nmy $last_nl_count_level;\n\nmy @bl_counts;\nmy $l"
},
{
"path": "valgrind.suppress",
"chars": 5776,
"preview": "{\n <insert_a_suppression_name_here>\n Memcheck:Param\n epoll_ctl(event)\n fun:epoll_ctl\n}\n{\n <insert_a_suppressio"
}
]
About this extraction
This page contains the full source code of the openresty/drizzle-nginx-module GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 47 files (304.0 KB), approximately 90.2k tokens, and a symbol index with 86 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.