EV-Etcd

 view release on metacpan or  search on metacpan

Etcd.xs  view on Meta::CPAN

            SV **ep = av_fetch(endpoints_av, i, 0);
            STRLEN len;
            const char *str;
            int is_https, skip;
            if (ep)
                SvGETMAGIC(*ep);
            if (!ep || !SvOK(*ep))
                croak("EV::Etcd: endpoints element %d is undefined", i);
            str = SvPV_nomg(*ep, len);
            VALIDATE_URL_SIZE(len);
            VALIDATE_NO_NUL(str, len, "endpoint");
            skip = endpoint_scheme_len(str, &is_https);
            if (len <= (STRLEN)skip)
                croak("EV::Etcd: endpoints element %d is empty", i);
            av_push(checked, newSVpvn(str + skip, len - skip));
            if (!skip)
                continue;
            if (is_https)
                tls = 1;
            else
                saw_http = 1;
        }
    }

    if (tls_ca_file || tls_cert_file || tls_key_file || tls_server_name)
        tls = 1;
    if (tls && saw_http)
        croak("EV::Etcd: http:// endpoint on a TLS client");
    if (!tls_cert_file != !tls_key_file)
        croak("EV::Etcd: tls_cert_file and tls_key_file must be given together");
    if (tls_ca_file) ca_pem = slurp_pem(aTHX_ "tls_ca_file", tls_ca_file);
    if (tls_cert_file) cert_pem = slurp_pem(aTHX_ "tls_cert_file", tls_cert_file);
    if (tls_key_file) key_pem = slurp_pem(aTHX_ "tls_key_file", tls_key_file);

    /* Its fork-safety croak must come before anything is allocated */
    grpc_acquire(aTHX);
    Newxz(client, 1, ev_etcd_t);

    if (checked) {
        int count = av_len(checked) + 1;
        Newx(client->endpoints, count, char *);
        client->endpoint_count = count;
        for (i = 0; i < count; i++) {
            SV *ep = *av_fetch(checked, i, 0);
            client->endpoints[i] = savepvn(SvPVX(ep), SvCUR(ep));
        }
    } else {
        Newx(client->endpoints, 1, char *);
        client->endpoints[0] = savepv("127.0.0.1:2379");
        client->endpoint_count = 1;
    }
    client->current_endpoint = 0;
    client->keepalive_ms = seconds_to_ms(keepalive_time);
    client->keepalive_timeout_ms = seconds_to_ms(keepalive_timeout);
    if (!client->keepalive_timeout_ms)
        client->keepalive_timeout_ms = 10000;

    if (tls) {
        grpc_ssl_pem_key_cert_pair pair;
        if (cert_pem) {
            pair.private_key = SvPV_nolen(key_pem);
            pair.cert_chain = SvPV_nolen(cert_pem);
        }
        client->creds = grpc_ssl_credentials_create(
            ca_pem ? SvPV_nolen(ca_pem) : NULL, cert_pem ? &pair : NULL, NULL, NULL);
        if (!client->creds) {
            for (int j = 0; j < client->endpoint_count; j++) {
                Safefree(client->endpoints[j]);
            }
            Safefree(client->endpoints);
            Safefree(client);
            grpc_release();
            croak("EV::Etcd: failed to create TLS credentials");
        }
        if (key_pem)
            memset(SvPVX(key_pem), 0, SvCUR(key_pem));
        if (tls_server_name)
            client->tls_server_name = savepv(tls_server_name);
    }

    client->channel_ref = etcd_create_channel(client, client->endpoints[0]);

    if (!client->channel_ref) {
        for (int j = 0; j < client->endpoint_count; j++) {
            Safefree(client->endpoints[j]);
        }
        Safefree(client->endpoints);
        if (client->creds) grpc_channel_credentials_release(client->creds);
        Safefree(client->tls_server_name);
        Safefree(client);
        grpc_release();
        croak("Failed to create gRPC channel");
    }
    client->channel = client->channel_ref->channel;

    client->cq = grpc_completion_queue_create_for_next(NULL);

    pthread_mutex_init(&client->queue_mutex, NULL);

    ev_async_init(&client->cq_async, cq_async_callback);
    ev_async_start(EV_DEFAULT, &client->cq_async);

    int thread_error;
    WITH_SIGNALS_BLOCKED(thread_error = pthread_create(&client->cq_thread, NULL, cq_thread_func, client));
    if (thread_error) {
        ev_async_stop(EV_DEFAULT, &client->cq_async);
        pthread_mutex_destroy(&client->queue_mutex);
        grpc_completion_queue_shutdown(client->cq);
        while (grpc_completion_queue_next(client->cq,
               gpr_inf_past(GPR_CLOCK_REALTIME), NULL).type != GRPC_QUEUE_SHUTDOWN)
            ;
        grpc_completion_queue_destroy(client->cq);
        etcd_channel_release(client->channel_ref, 1);
        for (int j = 0; j < client->endpoint_count; j++) {
            Safefree(client->endpoints[j]);
        }
        Safefree(client->endpoints);
        if (client->creds) grpc_channel_credentials_release(client->creds);
        Safefree(client->tls_server_name);
        Safefree(client);
        grpc_release();



( run in 0.620 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )