# Postponing responses to requests

**URL:** <https://www.ruby-forum.com/t/postponing-responses-to-requests/185182>\
**Category:** NGINX\
**Created:** [March 15, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/postponing-responses-to-requests/185182 "2010-03-15T17:23:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/postponing-responses-to-requests/185182/1 "2010-03-15T17:23:54Z")

</div>

I’m trying to figure out how to get some code working in an NGINX module  
we  
are writing. The basic idea is that requests come in to our module, and  
we  
make callouts from the module to both some external code and to an  
upstream  
HTTP interface. We then want to send responses to the requests.

The bit that we are a little confused about is how to manage our  
responses  
correctly. Our code looks a bit like this.

- Get request in our request handler
  - Do some work that will trigger an async callback later (from a  
library)
  - Return NGX\_DONE to say “leave us alone for now”

- Get async callback from our handler
  - Issue upstream request (using upstream module)

- Get upstream response
  - Do some more processing before sending response (potentially much  
later after more async work)

The problem occurs in that last step; we can do our async processing and  
manage the upstream requests OK, but as soon as we get our response from  
the upstream module, we find that finalize\_request is called and a  
response is sent. However, we want to hold off until later sending the  
response.

We’d like to be able to just say “don’t send a response on this request  
just now, even though the upstream request has returned”. I think  
perhaps the underlying issue is that we are just issuing an upstream  
request not a subrequest, but I’m now quite confused, and it would be  
very helpful if somebody could give me some helpful hints, or else point  
me at some documentation of how to do this, or just at some sample code  
we could read through for ideas.

Thanks,

Pete

Posted at Nginx Forum:

> **[Postponing responses to requests](https://forum.nginx.org/read.php?2,64052,64052#msg-64052)**
>
> I'm trying to figure out how to get some code working in an NGINX module we are writing. The basic idea is that requests come in to our module, and we make callouts from the module to both some external code and to an upstream HTTP interface. We then...

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 15, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/postponing-responses-to-requests/185182/2 "2010-03-15T19:09:18Z")

</div>

Hello!

On Mon, Mar 15, 2010 at 12:23:37PM -0400, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> correctly. Our code looks a bit like this.  
> much later after more async work)  
> request just now, even though the upstream request has  
> returned". I think perhaps the underlying issue is that we are  
> just issuing an upstream request not a subrequest, but I’m now  
> quite confused, and it would be very helpful if somebody could  
> give me some helpful hints, or else point me at some  
> documentation of how to do this, or just at some sample code we  
> could read through for ideas.

Basically you have two options:

1. Use subrequest with SUBREQUEST\_WAITED and SUBREQUEST\_IN\_MEMORY  
flags. It works for small responses and only when appropriate  
protocol module supports it (proxy and memcached do).

2. Install output filter and do anything you want with response.  
This works always, but it’s up to you to correctly implement it.

Maxim D.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 17, 2010, 10:39am UTC](https://www.ruby-forum.com/t/postponing-responses-to-requests/185182/3 "2010-03-17T10:39:40Z")

</div>

Some fiddling around later, it seems that using a subrequest is much  
simpler than what we were trying, and as an added bonus is working  
(though with limited testing so far).

Thanks for the pointers.

Pete

Posted at Nginx Forum:

> **[Re: Postponing responses to requests](https://forum.nginx.org/read.php?2,64052,64673#msg-64673)**
>
> I'm trying to figure out how to get some code working in an NGINX module we are writing. The basic idea is that requests come in to our module, and we make callouts from the module to both some external code and to an upstream HTTP interface. We then...
