Skip to content

Commit 70ca010

Browse files
committed
initial checkin: methods get_upstreams() and get_servers() now work.
0 parents  commit 70ca010

9 files changed

+956
-0
lines changed

.gitignore

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
*.mobi
2+
genmobi.sh
3+
.libs
4+
*.swp
5+
*.slo
6+
*.la
7+
*.swo
8+
*.lo
9+
*~
10+
*.o
11+
print.txt
12+
.rsync
13+
*.tar.gz
14+
dist
15+
build[78]
16+
build
17+
tags
18+
update-readme
19+
*.tmp
20+
test/Makefile
21+
test/blib
22+
test.sh
23+
t/t.sh
24+
test/t/servroot/
25+
releng
26+
reset
27+
*.t_
28+
src/handler.h
29+
src/util.c
30+
src/module.h
31+
src/module.c
32+
src/drizzle.c
33+
src/processor.h
34+
src/handler.c
35+
src/util.h
36+
src/drizzle.h
37+
src/processor.c
38+
src/output.c
39+
src/output.h
40+
libdrizzle
41+
ctags
42+
src/stream.h
43+
nginx
44+
keepalive
45+
reindex
46+
util/bench
47+
*.html
48+
trace.out*
49+
try.sh
50+
build9
51+
src/module.[ch]
52+
t/servroot/
53+
headers.[ch]
54+
buildroot/
55+
build1[0-9]
56+
go
57+
all
58+
analyze
59+
addr2line
60+
*.plist
61+
Makefile

README.md

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
Name
2+
====
3+
4+
ngx_lua_upstream - Nginx C module to expose Lua API to ngx_lua for Nginx upstreams
5+
6+
Table of Contents
7+
=================
8+
9+
* [Name](#name)
10+
* [Status](#status)
11+
* [Synopsis](#synopsis)
12+
* [Installation](#installation)
13+
* [Author](#author)
14+
* [Copyright and License](#copyright-and-license)
15+
* [See Also](#see-also)
16+
17+
Status
18+
======
19+
20+
This module is still under early development.
21+
22+
Synopsis
23+
========
24+
25+
```nginx
26+
```
27+
28+
upstream foo.com:1234 {
29+
server 127.0.0.1 fail_timeout=53 weight=4 max_fails=100;
30+
server agentzh.org:81;
31+
}
32+
33+
upstream bar {
34+
server 127.0.0.2;
35+
}
36+
37+
server {
38+
listen 8080;
39+
40+
# sample output for the following /upstream interface:
41+
# upstream foo.com:
42+
# addr = 127.0.0.1:80, weight = 4, fail_timeout = 53, max_fails = 100
43+
# addr = 106.187.41.147:81, weight = 1, fail_timeout = 10, max_fails = 1
44+
# upstream bar:
45+
# addr = 127.0.0.2:80, weight = 1, fail_timeout = 10, max_fails = 1
46+
47+
location = /upstreams {
48+
default_type text/plain;
49+
content_by_lua '
50+
local upstream = require "ngx.upstream"
51+
local get_servers = upstream.get_servers
52+
local get_upstreams = upstream.get_upstreams
53+
54+
local us = get_upstreams()
55+
for _, u in ipairs(us) do
56+
ngx.say("upstream ", u, ":")
57+
local srvs, err = get_servers(u)
58+
if not srvs then
59+
ngx.say("failed to get servers in upstream ", u)
60+
else
61+
for _, srv in ipairs(srvs) do
62+
local first = true
63+
for k, v in pairs(srv) do
64+
if first then
65+
first = false
66+
ngx.print(" ")
67+
else
68+
ngx.print(", ")
69+
end
70+
ngx.print(k, " = ", v)
71+
end
72+
ngx.print("\\n")
73+
end
74+
end
75+
end
76+
';
77+
}
78+
}
79+
80+
Compatibility
81+
=============
82+
83+
The following versions of Nginx should work with this module:
84+
85+
* **1.5.x** (last tested: 1.5.8)
86+
87+
Installation
88+
============
89+
90+
1. Grab the nginx source code from [nginx.org](http://nginx.org/), for example,
91+
the version 1.5.8 (see [nginx compatibility](#compatibility)),
92+
2. then grab the source code of the [ngx_lua](https://github.com/chaoslawful/lua-nginx-module#installation) as well as its dependencies like [LuaJIT](http://luajit.org/download.html).
93+
3. and finally build the source with this module:
94+
95+
```bash
96+
$ wget 'http://nginx.org/download/nginx-1.5.8.tar.gz'
97+
$ tar -xzvf nginx-1.5.8.tar.gz
98+
$ cd nginx-1.5.8/
99+
100+
# assuming your luajit is installed to /opt/luajit:
101+
export LUAJIT_LIB=/opt/luajit/lib
102+
103+
# assuming you are using LuaJIT 2.0.x:
104+
export LUAJIT_INC=/opt/luajit/include/luajit-2.0
105+
106+
# Here we assume you would install you nginx under /opt/nginx/.
107+
$ ./configure --prefix=/opt/nginx \
108+
--with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \
109+
--add-module=/path/to/lua-nginx-module \
110+
--add-module=/path/to/lua-upstream-nginx-module
111+
112+
$ make -j2
113+
$ make install
114+
```
115+
116+
[Back to TOC](#table-of-contents)
117+
118+
Author
119+
======
120+
121+
Yichun "agentzh" Zhang (章亦春) <[email protected]>, CloudFlare Inc.
122+
123+
[Back to TOC](#table-of-contents)
124+
125+
Copyright and License
126+
=====================
127+
128+
This module is licensed under the BSD license.
129+
130+
Copyright (C) 2014, by Yichun "agentzh" Zhang, CloudFlare Inc.
131+
132+
All rights reserved.
133+
134+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
135+
136+
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
137+
138+
* 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.
139+
140+
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.
141+
142+
[Back to TOC](#table-of-contents)
143+
144+
See Also
145+
========
146+
* the ngx_lua module: http://github.com/chaoslawful/lua-nginx-module#readme
147+
148+
[Back to TOC](#table-of-contents)
149+

config

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ngx_addon_name=ngx_http_lua_upstream_module
2+
HTTP_MODULES="$HTTP_MODULES ngx_http_lua_upstream_module"
3+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_lua_upstream_module.c"

src/ddebug.h

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
/*
3+
* Copyright (C) Yichun Zhang (agentzh)
4+
*/
5+
6+
7+
#ifndef _DDEBUG_H_INCLUDED_
8+
#define _DDEBUG_H_INCLUDED_
9+
10+
11+
#include <nginx.h>
12+
#include <ngx_core.h>
13+
14+
15+
#if defined(DDEBUG) && (DDEBUG)
16+
17+
# if (NGX_HAVE_VARIADIC_MACROS)
18+
19+
# define dd(...) fprintf(stderr, "lua-upstream *** %s: ", __func__); \
20+
fprintf(stderr, __VA_ARGS__); \
21+
fprintf(stderr, " at %s line %d.\n", __FILE__, __LINE__)
22+
23+
# else
24+
25+
#include <stdarg.h>
26+
#include <stdio.h>
27+
28+
#include <stdarg.h>
29+
30+
static void dd(const char *fmt, ...) {
31+
}
32+
33+
# endif
34+
35+
#else
36+
37+
# if (NGX_HAVE_VARIADIC_MACROS)
38+
39+
# define dd(...)
40+
41+
# else
42+
43+
#include <stdarg.h>
44+
45+
static void dd(const char *fmt, ...) {
46+
}
47+
48+
# endif
49+
50+
#endif
51+
52+
#if defined(DDEBUG) && (DDEBUG)
53+
54+
#define dd_check_read_event_handler(r) \
55+
dd("r->read_event_handler = %s", \
56+
r->read_event_handler == ngx_http_block_reading ? \
57+
"ngx_http_block_reading" : \
58+
r->read_event_handler == ngx_http_test_reading ? \
59+
"ngx_http_test_reading" : \
60+
r->read_event_handler == ngx_http_request_empty_handler ? \
61+
"ngx_http_request_empty_handler" : "UNKNOWN")
62+
63+
#define dd_check_write_event_handler(r) \
64+
dd("r->write_event_handler = %s", \
65+
r->write_event_handler == ngx_http_handler ? \
66+
"ngx_http_handler" : \
67+
r->write_event_handler == ngx_http_core_run_phases ? \
68+
"ngx_http_core_run_phases" : \
69+
r->write_event_handler == ngx_http_request_empty_handler ? \
70+
"ngx_http_request_empty_handler" : "UNKNOWN")
71+
72+
#else
73+
74+
#define dd_check_read_event_handler(r)
75+
#define dd_check_write_event_handler(r)
76+
77+
#endif
78+
79+
80+
#endif /* _DDEBUG_H_INCLUDED_ */
81+
82+
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

0 commit comments

Comments
 (0)