<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Greetings nginx developers, <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I work at Acision, and we make use of nginx, especially its mail module, which we have added considerable code to.  I'm currently experiencing issues related to non-blocking sockets and ngx_unix_recv() returning NGX_AGAIN which I can't
 make sense of, and I was wondering if anyone could help.  Forgive me if these questions are answered somewhere in this list, or elsewhere online -- if so, I cannot find them.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So, my chief goal is to know the proper way to simply create a new ngx_connection_t (specifically, with non-blocking sockets and in the mail module), which gets properly scheduled via the "nginx event queue" -- that is, to create an ngx_connection_t
 such that, when data arrives on the socket, the event engine calls my read handler, and when data is to be written, the event engine calls my write handler.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">To achieve that goal (to not only create an ngx_connection_t, but also have it "scheduled" in the "proper ngnix style"), I have thus far used ngx_event_connect_peer(), passing it the address of a local ngx_peer_connection_t variable, like
 so:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">  ngx_peer_connection_t peer;<o:p></o:p></p>
<p class="MsoNormal">  ngx_str_t peer_name = {13, (u_char*)"MyName"};<o:p></o:p></p>
<p class="MsoNormal">   ...<o:p></o:p></p>
<p class="MsoNormal">  /* build peer */<o:p></o:p></p>
<p class="MsoNormal">  peer.sockaddr = (struct sockaddr *) saddr;  <o:p></o:p></p>
<p class="MsoNormal">  peer.socklen = sizeof(struct sockaddr_in);<o:p></o:p></p>
<p class="MsoNormal">  peer.name = &peer_name;<o:p></o:p></p>
<p class="MsoNormal">  peer.get = ngx_event_get_peer;<o:p></o:p></p>
<p class="MsoNormal">  peer.log = s->connection->log;<o:p></o:p></p>
<p class="MsoNormal">  peer.log_error = NGX_ERROR;<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">  rc = ngx_event_connect_peer(&peer);<o:p></o:p></p>
<p class="MsoNormal">  if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED)<o:p></o:p></p>
<p class="MsoNormal">     /* error */<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">  peer.connection->data = MyData;<o:p></o:p></p>
<p class="MsoNormal">  peer.connection->pool = s->connection->pool;<o:p></o:p></p>
<p class="MsoNormal">  peer.connection->read->handler = my_read_handler;<o:p></o:p></p>
<p class="MsoNormal">  peer.connection->write->handler = my_write_handler;<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Using that approach, ngx_event_connect_peer() creates the ngx_connection_t as peer.connection, and by using ngx_post_event() on peer.connection->read and peer.connection->write, I've been able to force my handlers to hit, and also, the
 appropriate handler (read or write) seems to be called when data is to be sent or received.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">That approach seemed ok at first, but I’ve been noticing strange behavior on the non-blocking sockets within my_read_handler().  In particular, I call ngx_unix_recv() in my_read_handler() to actually receive data, but in any given invocation
 of my_read_handler(), the first call to ngx_unix_recv() never ends up reading more than 128 bytes, and the second call always returns NGX_AGAIN, as if no more data were available at that time.  However, I know more data is available!  For example, if I continue
 to call ngx_unix_recv() is a loop, ngx_unix_recv() will return NGX_AGAIN forever, even though more data will definitely be available!  It's not until a separate invocation my_read_handler() in the future occurs that ngx_unix_recv() doesn't return NGX_AGAIN,
 but again, the first call returns at most 128 bytes and the second call returns NGX_AGAIN.  And, this pattern continues.  The result is that for very large transfers, the data transfer is very slow!<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Is there something I can do to avoid that behavior?  What am I doing wrong?  Do I really want to be using ngx_event_connect_peer() and an ngx_peer_connection_t to achieve my goal of creating a new nginx connection, which gets scheduled
 via the event queue?  <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks a bunch,<o:p></o:p></p>
<p class="MsoNormal">Drew Abbot, Acision<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<br>
<hr>
<font face="Arial" color="Gray" size="1">This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed
 to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you for understanding.<br>
</font>
<br clear=all> 
</body>
</html>