EV-Redis

 view release on metacpan or  search on metacpan

src/EV__Redis.xs  view on Meta::CPAN

    if (NULL == handler || NULL == reply) return;
    SvREFCNT_inc_simple_void_NN(handler);

    self->callback_depth++;

    {
        dSP;

        ENTER;
        SAVETMPS;
        save_scalar(PL_errgv);

        int truncated = 0;
        SV* decoded = sv_2mortal(EV__redis_decode_reply(reply, &truncated));

        if (truncated) {
            /* on_push takes the message only, so a dropped one errors here */
            emit_error_str(self, "reply exceeds maximum nesting depth");
        }
        else {
            PUSHMARK(SP);
            XPUSHs(decoded);
            PUTBACK;

            call_sv(handler, G_DISCARD | G_EVAL);
            if (SvTRUE(ERRSV)) {
                warn_exception("push handler", ERRSV);
            }
        }

        FREETMPS;
        LEAVE;
    }

    SvREFCNT_dec(handler);
    redisLibevResumeParked(ac);
    self->callback_depth--;
    check_destroyed(self);
}

static void pre_connect_common(EV__Redis self, redisOptions* opts) {
    self->connection_gen++;
    if (NULL != self->connect_timeout) {
        opts->connect_timeout = self->connect_timeout;
    }
    if (NULL != self->command_timeout) {
        opts->command_timeout = self->command_timeout;
    }
    if (self->prefer_ipv4) {
        opts->options |= REDIS_OPT_PREFER_IPV4;
    }
    else if (self->prefer_ipv6) {
        opts->options |= REDIS_OPT_PREFER_IPV6;
    }
    /* not REDIS_OPT_SET_SOCK_CLOEXEC: its c->flags bit is REDIS_REUSEADDR, so
     * each would turn on the other; post_connect_setup sets FD_CLOEXEC */
    if (self->reuseaddr) {
        opts->options |= REDIS_OPT_REUSEADDR;
    }
    if (NULL != self->source_addr && NULL == self->path) {
        opts->endpoint.tcp.source_addr = self->source_addr;
    }
}

static int setup_failed(EV__Redis self, SV* err) {
    /* own copy: on_error's @_ aliases err, so the handler can change it */
    SV* wait_err = sv_2mortal(newSVsv(err));
    ngx_queue_t doomed;
    redisAsyncFree(self->ac);
    self->ac = NULL;
    ngx_queue_init(&doomed);
    if (!self->reconnect || !self->resume_waiting_on_reconnect
            || self->intentional_disconnect) {
        take_wait_queue(self, &doomed);
    }
    rederive_span(self);
    emit_error(self, err);
    fail_wait_list(self, &doomed, wait_err);
    return REDIS_ERR;
}

/* On failure: frees and nulls self->ac, then emits the error. */
static int post_connect_setup(EV__Redis self, const char* err_prefix) {
    self->ac->data = (void*)self;
    self->in_multi = 0;
    self->pending_multi = 0;
    (void)sync_barrier(self);
    if (self->ac->c.fd != REDIS_INVALID_FD) {
        fcntl(self->ac->c.fd, F_SETFD, self->cloexec ? FD_CLOEXEC : 0);
    }

    /* failed inside the connect call: fd may be -1, never hand it to libev */
    if (self->ac->err) {
        return setup_failed(self, sv_2mortal(newSVpvf("%s: %s",
            err_prefix, self->ac->errstr)));
    }

#ifdef EV_REDIS_SSL
    if (NULL != self->ssl_ctx
            && REDIS_OK != redisInitiateSSLWithContext(&self->ac->c, self->ssl_ctx)) {
        return setup_failed(self, sv_2mortal(newSVpvf("%s: SSL initiation failed: %s",
            err_prefix, self->ac->errstr[0] ? self->ac->errstr : "unknown error")));
    }
#endif

    /* on failure it sets c.err, which breaks the first write */
    if (self->keepalive > 0 && self->ac->c.connection_type == REDIS_CONN_TCP
            && REDIS_OK != redisEnableKeepAliveWithInterval(&self->ac->c, self->keepalive)) {
        return setup_failed(self, sv_2mortal(newSVpvf("%s: %s",
            err_prefix, self->ac->c.errstr)));
    }
    /* hiredis closes the fd when this fails */
    if (self->tcp_user_timeout > 0 && self->ac->c.connection_type == REDIS_CONN_TCP
            && REDIS_OK != redisSetTcpUserTimeout(&self->ac->c, self->tcp_user_timeout)) {
        return setup_failed(self, sv_2mortal(newSVpvf("%s: %s",
            err_prefix, self->ac->c.errstr)));
    }

    if (REDIS_OK != redisLibevAttach(self->loop, self->ac)) {
        return setup_failed(self, sv_2mortal(newSVpvf("%s: cannot attach libev",
            err_prefix)));



( run in 0.704 second using v1.01-cache-2.11-cpan-eb134014566 )