# Ngx\_echo v0.29: major core refactoring and more robust sequential subrequests

**URL:** <https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073>\
**Category:** NGINX\
**Created:** [May 5, 2010, 11:16am UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073 "2010-05-05T11:16:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 5, 2010, 11:16am UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/1 "2010-05-05T11:16:22Z")

</div>

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 🙂

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](http://wiki.nginx.org/NginxHttpEchoModule)

---

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 5, 2010, 11:33am UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/2 "2010-05-05T11:33:56Z")

</div>

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](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](http://github.com/agentzh/echo-nginx-module/tarball/v0.29)  
Resolving [github.com](http://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](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](http://waitdownload.github.com/agentzh-echo-nginx-module-v0.29-0-gd87e269.tar.gz)  
Resolving [waitdownload.github.com](http://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](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](http://download.github.com/agentzh-echo-nginx-module-v0.29-0-gd87e269.tar.gz)  
Resolving [download.github.com](http://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,  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 5, 2010, 11:52am UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/3 "2010-05-05T11:52:40Z")

</div>

On Wed, May 5, 2010 at 5:33 PM, Sergey A. Osokin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> …  
> [following]  
> –2010-05-05 11:20:08–  
> [http://download.github.com/agentzh-echo-nginx-module-v0.29-0-gd87e269.tar.gz](http://download.github.com/agentzh-echo-nginx-module-v0.29-0-gd87e269.tar.gz)  
> Resolving [download.github.com](http://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

---

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 6, 2010, 1:43pm UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/4 "2010-05-06T13:43:23Z")

</div>

On Wed, May 5, 2010 at 5:15 PM, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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

---

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 7, 2010, 3:55am UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/5 "2010-05-07T03:55:10Z")

</div>

On Fri, May 7, 2010 at 1:56 AM, Matthieu T.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂

> 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 🙂

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” 🙂

To be honest, I happen to be thinking about mudding with the  
ngx\_http\_upstream infrastructure a bit more these days 😉

Cheers,  
-agentzh

---

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 7, 2010, 6:41pm UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/6 "2010-05-07T18:41:21Z")

</div>

On Thu, May 6, 2010 at 6:54 PM, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> queries  
> Given the flexibility of the nginx core, I’d say “yes” 🙂
> 
> To be honest, I happen to be thinking about mudding with the  
> ngx\_http\_upstream infrastructure a bit more these days 😉
> 
> 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;

---

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 10, 2010, 5:44am UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/7 "2010-05-10T05:44:38Z")

</div>

On Sat, May 8, 2010 at 12:40 AM, Matthieu T.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> echo\_subrequest $echo\_request\_method ‘/telnet\_upstream’ -b  
> $echo\_client\_request\_headers$echo\_request\_body;

Oh, that’s sweet 🙂

Will you work on that ngx\_telnet upstream module 😉 I’m currently  
quite busy with ngx\_srcache and ngx\_lua development 😛

Cheers,  
-agentzh

---

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 6, 2010, 7:57pm UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/8 "2010-05-06T19:57:32Z")

</div>

On Thu, May 6, 2010 at 4:42 AM, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://github.com/agentzh/echo-nginx-module/tarball/v0.30](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.

---

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 10, 2010, 10:13pm UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/9 "2010-05-10T22:13:25Z")

</div>

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:

> **[Re: \[ANNOUNCE\] ngx\_echo v0.29: major core refactoring and more robust...](https://forum.nginx.org/read.php?2,82808,85050#msg-85050)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 10, 2010, 9:09pm UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/10 "2010-05-10T21:09:02Z")

</div>

On Sun, May 9, 2010 at 8:44 PM, agentzh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![agentzh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@agentzh](https://www.ruby-forum.com/u/agentzh)\
**Post date:** [May 11, 2010, 5:10am UTC](https://www.ruby-forum.com/t/ngx-echo-v0-29-major-core-refactoring-and-more-robust-sequential-subrequests/188073/11 "2010-05-11T05:10:15Z")

</div>

On Tue, May 11, 2010 at 4:12 AM, peterjanovsky [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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 🙂

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
