void
ngx_http_upstream_init(ngx_http_request_t *r)
{
ngx_connection_t *c;
c = r->connection;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http init upstream, client timer: %d", c->read->timer_set);
//检查请求对于客户端的连接,如果此连接上的读事件在定时器中,就从定时器中删除。因为一旦启动upstream,就不应该对客户端的读操作带有超时时间的处理,请求的主要触发事件将以与上游服务器的连接为主
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
//如果nginx是边缘触发,并且写事件非活跃,那么以边缘触发的形式来添加写事件
if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
if (!c->write->active) {
if (ngx_add_event(c->write, NGX_WRITE_EVENT, NGX_CLEAR_EVENT)
== NGX_ERROR)
{
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
}
}
//主要的启动流程在这里实现
ngx_http_upstream_init_request(r);
}
static void
ngx_http_upstream_init_request(ngx_http_request_t *r)
{
//这是用户自己实现的回调函数,主要是构造发往上游服务器的请求,请求的内容是放到u->request_bufs缓冲区链表中的。注意,包括请求头和请求体。
if (u->create_request(r) != NGX_OK) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
//注意这里,后面会有解释
u->output.output_filter = ngx_chain_writer;
u->output.filter_ctx = &u->writer; ngx_http_upstream_connect(r, u); }