# How to bind variable with connection in a customized module?

**URL:** <https://www.ruby-forum.com/t/how-to-bind-variable-with-connection-in-a-customized-module/229627>\
**Category:** NGINX\
**Created:** [January 22, 2013, 1:17pm UTC](https://www.ruby-forum.com/t/how-to-bind-variable-with-connection-in-a-customized-module/229627 "2013-01-22T13:17:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![landlord01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8baadc/32.png) [@landlord01](https://www.ruby-forum.com/u/landlord01)\
**Post date:** [January 22, 2013, 1:17pm UTC](https://www.ruby-forum.com/t/how-to-bind-variable-with-connection-in-a-customized-module/229627/1 "2013-01-22T13:17:11Z")

</div>

hi all,

I want save state during a long connection (keep-alive) for performance  
optimization in my own HTTP handler module, thus I can peek up the saved  
state when handling requests, to speed up response. Actually it’s quite  
like session concept. I saw request struct has a member ctx, but what I  
want is a ctx on the connection. It seems no way to save any customized  
variable to the ngx\_connection\_t structure. What’s the suggested way to  
make this if I don’t want the client hold something like session\_id?

---

<div class="post-metadata">

**Author:** ![landlord01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8baadc/32.png) [@landlord01](https://www.ruby-forum.com/u/landlord01)\
**Post date:** [January 22, 2013, 1:31pm UTC](https://www.ruby-forum.com/t/how-to-bind-variable-with-connection-in-a-customized-module/229627/2 "2013-01-22T13:31:47Z")

</div>

Hello!

On Tue, Jan 22, 2013 at 08:16:36PM +0800, Liu H. wrote:

> hi all,
> 
> I want save state during a long connection (keep-alive) for  
> performance optimization in my own HTTP handler module, thus I  
> can peek up the saved state when handling requests, to speed up  
> response. Actually it’s quite like session concept. I saw  
> request struct has a member ctx, but what I want is a ctx on the  
> connection. It seems no way to save any customized variable to  
> the ngx\_connection\_t structure. What’s the suggested way to make  
> this if I don’t want the client hold something like session\_id?

This was recently discussed on nginx-devel@ mailing list, and  
probably the best way currently available is to install connection  
pool cleanup handler with custom data and then iterate over  
connection pool cleanup handlers to find your data. It is  
relatively costly, but allows to keep memory footprint from  
keepalive connections low and still allows modules to keep their  
per-connection data in rare cases when they really need to.

See here:

> **[Connection API?](http://mailman.nginx.org/pipermail/nginx-devel/2012-December/003049.html)**

Note well though, that HTTP is stateless protocol, and the fact  
that request came from the same connection means mostly nothing:  
it might be a request from a completely different user.

–  
Maxim D.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.
