Ngx_echo v0.29: major core refactoring and more robust sequential subrequests

Hi, list!

I’m happy to announce the v0.29 release of the ngx_echo module:

http://github.com/agentzh/echo-nginx-module/tarball/v0.29

As mentioned in some other threads on the nginx mailing list, I’ve
completed the big refactoring of the ngx_echo module’s core in this
version to reflect my latest understanding (hopefully being quite
complete and correct already) of the nginx internals.

Now the implementation of echo_subrequest, echo_location, echo_sleep,
and echo_read_request_body directives has been massively rewritten.
I’m trying to set up a design pattern for nginx content handlers that
require to do various kinds of non-blocking I/O during its lifetime
(similar to upstream modules but for different tasks like subrequests
and others).

For sequential subrequests issued by the echo module’s content
handler, we now use a totally different approach.

Instead of issuing subrequests directly in our post_subrequest
callback (as fed into the ngx_http_subrequest call), we now postpone
firing the subrequests in a custom write event handler which will be
called automatically once the current subrequest in question gets
completely finalized (that post_subrequest callback is called by
ngx_http_finalize_request of the subrequest in question).

This works because the parent request will always be waken up once its
subrequest completes. (This is done by ngx_http_finalize_request via
posting the parent request into the “posted requests” queue and the
posted requests will be always called at the end of the top-level
read/write event handler, i.e., ngx_http_request_handler).

This solves a lot of issues like mangled r->main->count and the
following alert message when nginx is configured with the --with-debug
option, for instance,

2010/05/05 16:46:14 [alert] 23853#0: *1 http finalize non-active

request: “/main?” …

We’ll apply this trick to the upcoming ngx_lua and ngx_srcache modules
soon :slight_smile:

I’ve Cc’d the nginx-devel mailing list because other nginx developers
might find this useful.

Stay tuned!
-agentzh

P.S. See the ngx_echo module’s wiki page for more information:
http://wiki.nginx.org/NginxHttpEchoModule

On Wed, May 05, 2010 at 05:15:32PM +0800, agentzh wrote:

Hi, list!

I’m happy to announce the v0.29 release of the ngx_echo module:

http://github.com/agentzh/echo-nginx-module/tarball/v0.29

% wget http://github.com/agentzh/echo-nginx-module/tarball/v0.29
–2010-05-05 11:20:07–
http://github.com/agentzh/echo-nginx-module/tarball/v0.29
Resolving github.com… 207.97.227.239
Connecting to github.com|207.97.227.239|:80… connected.
HTTP request sent, awaiting response… 302 Moved Temporarily
Location:
http://waitdownload.github.com/agentzh-echo-nginx-module-v0.29-0-gd87e269.tar.gz
[following]
–2010-05-05 11:20:07–
http://waitdownload.github.com/agentzh-echo-nginx-module-v0.29-0-gd87e269.tar.gz
Resolving waitdownload.github.com… 207.97.227.241
Connecting to waitdownload.github.com|207.97.227.241|:80… connected.
HTTP request sent, awaiting response… 302 Moved Temporarily
Location:
http://download.github.com/agentzh-echo-nginx-module-v0.29-0-gd87e269.tar.gz
[following]
–2010-05-05 11:20:08–
http://download.github.com/agentzh-echo-nginx-module-v0.29-0-gd87e269.tar.gz
Resolving download.github.com… 207.97.227.240
Connecting to download.github.com|207.97.227.240|:80… connected.
HTTP request sent, awaiting response… 404 Not Found
2010-05-05 11:20:08 ERROR 404: Not Found.


Sergey A. Osokin,
[email protected]

On Wed, May 5, 2010 at 5:33 PM, Sergey A. Osokin [email protected]
wrote:


[following]
–2010-05-05 11:20:08–
http://download.github.com/agentzh-echo-nginx-module-v0.29-0-gd87e269.tar.gz
Resolving download.github.com… 207.97.227.240
Connecting to download.github.com|207.97.227.240|:80… connected.
HTTP request sent, awaiting response… 404 Not Found
2010-05-05 11:20:08 ERROR 404: Not Found.

Weird. That GitHub download link works for me. Maybe an issue on that
mirror.

Here’s the link on my own server:

http://agentzh.org/misc/nginx/agentzh-echo-nginx-module-v0.29-0-gd87e269.tar.gz

Enjoy!
-agentzh

On Wed, May 5, 2010 at 5:15 PM, agentzh [email protected] wrote:

Hi, list!

I’m happy to announce the v0.29 release of the ngx_echo module:

http://github.com/agentzh/echo-nginx-module/tarball/v0.29

ngx_echo v0.30 is just out:

http://github.com/agentzh/echo-nginx-module/tarball/v0.30

I didn’t get the r->main->count right for the echo_exec directive in
the previous version. Use of the echo_exec directive in v0.29 may
result in random server hang for nginx >= 0.8.11.

Please consider upgrading to v0.30 immediately if you’re using
ngx_echo. And please feel free to report any issues on your side.

Thanks!
-agentzh

On Fri, May 7, 2010 at 1:56 AM, Matthieu T.
[email protected] wrote:

The new version of the echo module looks great, and it’s a very good source
of inspiration for writing my own module.

Glad to hear that :slight_smile:

I think that would be amazing if the echo module could be used to “pipe”
echo directives to an upstream, in order to create completely custom queries
for example, instead of echoing data back to the user.

This is a very intriguing idea and I can see the benefits of debugging
custom TCP upstream modules :slight_smile:

Just to confirm my understanding, I’d ask first…are you suggesting a
telnet module for nginx upstreams? grin

Do you think that would be feasible ?

Given the flexibility of the nginx core, I’d say “yes” :slight_smile:

To be honest, I happen to be thinking about mudding with the
ngx_http_upstream infrastructure a bit more these days :wink:

Cheers,
-agentzh

On Thu, May 6, 2010 at 6:54 PM, agentzh [email protected] wrote:

queries
Given the flexibility of the nginx core, I’d say “yes” :slight_smile:

To be honest, I happen to be thinking about mudding with the
ngx_http_upstream infrastructure a bit more these days :wink:

Cheers,
-agentzh

Yes, I guess that could be done with a telnet upstream module,
then I could just do something like :

echo_subrequest /telnet_upstream -b “GET / HTTP/1.0\n …” to completely
craft queries.

That could also be useful to me, for example to let an untouched query
go
through,
using something like :

echo_read_request_body;
echo_subrequest $echo_request_method ‘/telnet_upstream’ -b
$echo_client_request_headers$echo_request_body;

On Sat, May 8, 2010 at 12:40 AM, Matthieu T.
[email protected] wrote:

echo_subrequest $echo_request_method ‘/telnet_upstream’ -b
$echo_client_request_headers$echo_request_body;

Oh, that’s sweet :slight_smile:

Will you work on that ngx_telnet upstream module :wink: I’m currently
quite busy with ngx_srcache and ngx_lua development :stuck_out_tongue:

Cheers,
-agentzh

On Thu, May 6, 2010 at 4:42 AM, agentzh [email protected] wrote:

http://github.com/agentzh/echo-nginx-module/tarball/v0.30

Hi Agentzh,

The new version of the echo module looks great, and it’s a very good
source
of inspiration for writing my own module.

I think that would be amazing if the echo module could be used to “pipe”
echo directives to an upstream, in order to create completely custom
queries
for example, instead of echoing data back to the user.
Do you think that would be feasible ?

Thank you,

Matthieu.

thnx agentzh,

can’t wait to get into the source. i’ve been snooping around the echo
module source to familiarize myself with subrequests as it provides
great examples. question, is it possible to determine a content-length
when you utilize subrequests or will that prohibit chunking?

peter

Posted at Nginx Forum:

On Sun, May 9, 2010 at 8:44 PM, agentzh [email protected] wrote:

using something like :
-agentzh

I might consider doing it if I have a real need for it, but
unfortunately I
have higher priority projects currently.

I’ll let you know if I start an implementation,

Matthieu.

On Tue, May 11, 2010 at 4:12 AM, peterjanovsky [email protected]
wrote:

can’t wait to get into the source. i’ve been snooping around the echo
module source to familiarize myself with subrequests as it provides great
examples.

Yay!

question, is it possible to determine a content-length when you utilize
subrequests or will that prohibit chunking?

Yes, it’s possible :slight_smile:

you can inspect all your subrequest’s response bufs in your own output
filter and accumulate the overall buf size (i.e., the total length of
your subrequest’s response) along the way (but not buffer the bufs
themselves).

It will not prohibit chunking as long as you don’t want to send a
Content-Length header before sending the response bufs.

Cheers,
-agentzh