void 
ngx_tcp_lua_req_resume(ngx_tcp_session_t *s) 
{
    int                                  nret = 0;
    ngx_int_t                            rc;
    ngx_connection_t                    *c;
    ngx_tcp_lua_ctx_t                   *ctx;
    ngx_tcp_lua_main_conf_t             *lmcf;

    c = s->connection;

    ctx = s->ctx;

    if (ctx->prepare_retvals) {
    
        nret = ctx->prepare_retvals(s,ctx->co);
        ctx->prepare_retvals = NULL;
        
    }
    
    lmcf = ngx_tcp_get_module_main_conf(s, ngx_tcp_lua_module);

    dd("about to run thread for %p ", s);

    rc = ngx_tcp_lua_run_thread(lmcf->lua, s, ctx, nret);

    ngx_log_debug1(NGX_LOG_DEBUG_TCP, c->log, 0,
            "lua run thread returned %d", rc);

    if (rc == NGX_ERROR || rc == NGX_OK) {
        ngx_tcp_lua_close_session(s);
        return;
    }
    
    //NGX_AGAIN
    return;
}
ngx_int_t
ngx_tcp_lua_process_by_chunk(lua_State *L, ngx_tcp_session_t *s)
{
    int                      cc_ref;
    lua_State               *cc;
    ngx_tcp_lua_ctx_t      *ctx;
    ngx_tcp_cleanup_t      *cln;

    dd("content by chunk");

    ctx = ngx_tcp_get_module_ctx(s, ngx_tcp_lua_module);

    if (ctx == NULL) {
        ctx = ngx_pcalloc(s->pool, sizeof(ngx_tcp_lua_ctx_t));
        if (ctx == NULL) {
            return NGX_ERROR;
        }

        dd("setting new ctx, ctx = %p", ctx);

        ctx->cc_ref  = LUA_NOREF;
        ctx->ctx_ref = LUA_NOREF;
		ctx->exited  = 0;

        ngx_tcp_set_ctx(s, ctx, ngx_tcp_lua_module);

    } else {
        dd("reset ctx");
        ngx_tcp_lua_reset_ctx(s, L, ctx);
    }

    /*  {{{ new coroutine to handle request */
    cc = ngx_tcp_lua_new_thread(s, L, &cc_ref);

    if (cc == NULL) {
        ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
                "(lua-content-by-chunk) failed to create new coroutine "
                "to handle request");

        return NGX_ERROR;
    }

    /*  move code closure to new coroutine */
    lua_xmove(L, cc, 1);

    /*  set closure's env table to new coroutine's globals table */
    lua_pushvalue(cc, LUA_GLOBALSINDEX);
    lua_setfenv(cc, -2);

    /*  save nginx request in coroutine globals table */
    lua_pushlightuserdata(cc, &ngx_tcp_lua_request_key);
    lua_pushlightuserdata(cc, s);
    lua_rawset(cc, LUA_GLOBALSINDEX);
    /*  }}} */

    ctx->co = cc;
    ctx->cc_ref = cc_ref;

    /*  {{{ register request cleanup hooks */
    if (ctx->cleanup == NULL) {
        cln = ngx_tcp_cleanup_add(s, 0);
        if (cln == NULL) {
            return NGX_ERROR;
        }

        cln->handler = ngx_tcp_lua_request_cleanup;
        cln->data = s;
        ctx->cleanup = &cln->handler;
    }
    /*  }}} */

    return ngx_tcp_lua_run_thread(L, s, ctx, 0);

}