Hello,
I want to send data continuously in a long loop, and I don't want to block the worker's primary event loop with my own loop logic. How can I do it?
This is my handler function:
/* Handler function */
static ngx_int_t
ngx_http_mymodue_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_buf_t *b;
r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char *) "text/html";
r->headers_out.status = NGX_HTTP_OK;
r->chunked = 1;
r->connection->tcp_nodelay = 1;
rc = ngx_http_send_header(r);
if(rc != NGX_OK || r->header_only)
{
return rc;
}
int i = 0;
u_char txt[64];
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
while(i++<1000000)
{
ngx_sprintf(txt,"hello,this is line %d <br>",i);
ngx_chain_t out;
out.buf = b;
out.next = NULL;
b->pos = txt;
b->last = txt + ngx_strlen(txt);
b->memory = 1;
b->last_buf = 0;
b->flush = 1;
b->sync = 1;
if(ngx_http_output_filter(r, &out)!=NGX_OK)
{
printf("error");
break;
}
usleep(100);
}
return NGX_OK;
}
Thanks!
I want to send data continuously in a long loop, and I don't want to block the worker's primary event loop with my own loop logic. How can I do it?
This is my handler function:
/* Handler function */
static ngx_int_t
ngx_http_mymodue_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_buf_t *b;
r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char *) "text/html";
r->headers_out.status = NGX_HTTP_OK;
r->chunked = 1;
r->connection->tcp_nodelay = 1;
rc = ngx_http_send_header(r);
if(rc != NGX_OK || r->header_only)
{
return rc;
}
int i = 0;
u_char txt[64];
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
while(i++<1000000)
{
ngx_sprintf(txt,"hello,this is line %d <br>",i);
ngx_chain_t out;
out.buf = b;
out.next = NULL;
b->pos = txt;
b->last = txt + ngx_strlen(txt);
b->memory = 1;
b->last_buf = 0;
b->flush = 1;
b->sync = 1;
if(ngx_http_output_filter(r, &out)!=NGX_OK)
{
printf("error");
break;
}
usleep(100);
}
return NGX_OK;
}
Thanks!