void 
ngx_tcp_lua_finalize_light_session(ngx_tcp_session_t* s)
{
    ngx_connection_t         *c;
    ngx_tcp_lua_ctx_t        *ctx;
    
    c = s->connection;
    ctx = s->ctx;
    //ngx_log_debug0(NGX_LOG_DEBUG_TCP, c->log, 0,
    //               "lua req calling wait_next_request() method");
   
#if (NGX_STAT_STUB)

    if (s->stat_reading) {
        (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
        s->stat_reading = 0;
    }

    if (s->stat_writing) {
        (void) ngx_atomic_fetch_add(ngx_stat_writing, -1);
        s->stat_writing = 0;
    }

#endif

    ngx_tcp_lua_log_session(s);
    
    ngx_reset_pool(s->pool);

    ctx->buf_in = NULL;
    ctx->buf_out = NULL;
}
Beispiel #2
0
static ngx_int_t
ngx_nats_connect_loop(ngx_nats_data_t *nd)
{
    ngx_nats_core_conf_t   *nccf;
    ngx_nats_connection_t  *nc;
    ngx_nats_server_t      *ns;
    ngx_addr_t             *a;
    ngx_int_t               rc, n;
    ngx_connection_t       *c = NULL;

    nccf = nd->nccf;

    n = nccf->servers->nelts;

    if (nd->curr_index < 0) {

        /* new reconnect loop */
        nd->curr_index = nd->last_index;
        nd->nconnects++;

    } else {

        if (++nd->curr_index >= n) {
            nd->curr_index = 0;
        }

        if (nd->curr_index == nd->last_index) {

            /*
             * means we tried each server and could not connect
             * to any of them, now sleep and then repeat.
             */

            if (!ngx_nats_conn_err_reported) {

                ngx_nats_conn_err_reported = 1;

                ngx_log_error(NGX_LOG_ERR, nd->log, 0,
                    "cannot connect to NATS server%s, "
                    "will try every %d milliseconds",
                    (u_char*)(n > 1 ? "s" : ""),
                    (int)nccf->reconnect_interval);
            }

            ngx_nats_add_reconnect_timer(nd);

            return NGX_DECLINED;
        }
    }

    ngx_reset_pool(nd->nc_pool);
    ngx_nats_buf_reset(nd->nc_read_buf);
    ngx_nats_buf_reset(nd->nc_write_buf);

    nc = ngx_pcalloc(nd->nc_pool, sizeof(ngx_nats_connection_t));
    if (nc == NULL) {
        return NGX_ERROR;
    }

    nd->nc = nc;
    nc->nd = nd;

    ns = (ngx_nats_server_t *) nccf->servers->elts;
    ns = ns + nd->curr_index;

    a = ns->addrs;  /* TODO: handle multiple addrs? */

    nc->pc.data     = nd;
    nc->pool        = nd->nc_pool;
    nc->read_buf    = nd->nc_read_buf;
    nc->write_buf   = nd->nc_write_buf;
    nc->pc.log      = nccf->log;
#if (NGX_HAVE_KQUEUE)
    /* This prevents Nginx printing:
     * kevent() reported about an closed connection (61: Connection refused)
     * every time connection to NATS fails, it prints only if the error
     * log level is set to INFO which we don't.
     */
    nc->pc.log_error = NGX_ERROR_INFO;
#endif
    nc->pc.sockaddr = a->sockaddr;
    nc->pc.socklen  = a->socklen;
    nc->pc.name     = &ns->url;
    nc->pc.tries    = 1;
    nc->pc.get      = ngx_nats_get_peer;
    nc->pc.free     = ngx_nats_free_peer;

    nc->server = ns;

    rc = ngx_event_connect_peer(&nc->pc);

    if (rc == NGX_BUSY || rc == NGX_ERROR || rc == NGX_DECLINED) {
        return NGX_AGAIN;
    }

    if (rc == NGX_OK || rc == NGX_AGAIN) {

        c = nc->pc.connection;

        c->data = nc;

        c->write->handler   = ngx_nats_connection_handler;
        c->read->handler    = ngx_nats_connection_handler;

        c->log              = nd->log;

        c->read->log        = c->log;
        c->write->log       = c->log;
        /* TODO: do I need SSL? c->pool is for SSL only. */
        if (c->pool != NULL)
            c->pool->log = c->log;
    }

    if (rc == NGX_AGAIN) {
        ngx_add_timer(c->write, 5000);  /* TODO: configurable? */
        return NGX_OK;
    }

    if (rc == NGX_OK) {
        /* Connected right here. */
        ngx_nats_connection_init(nc);
        ngx_nats_flush(nc);
        return NGX_OK;
    }

    return NGX_OK;
}