curl / Mailing Lists / curl-library / Single Mail

curl-library

Problems w/ pausing & unpausing HTTP connections

From: Philip Prindeville <philipp_subx_at_redfish-solutions.com>
Date: Mon, 12 Mar 2018 20:06:37 -0600

Hi.

I’m using libcurl-7.55.1 and libevent-2.0.22 together, and a dispatcher loosely copied from the hiperfifo.c example.

I’ve also added an additional timer which ticks at about 24 HZ.

My WRITEFUNCTION callback looks like:

static size_t
write_cb(void* ptr, size_t size, size_t nmemb, void* userdata)
{
        session_t *sess = (session_t *)userdata;
        buffer_t *bp = &sess->buffer;
        size_t totalBytes = size * nmemb;

        /* pause until we're drained */
        if (buffer_ishigh(bp) || buffer_remaining(bp) < totalBytes)
                return CURL_WRITEFUNC_PAUSE;

        buffer_put(bp, ptr, totalBytes);

        return totalBytes;
}

so we get called back with data, and if there’s room, we append it to our FIFO buffer.

The 2nd (periodic) timer callback for libevent2 looks like:

/* Called by libevent when our timeout expires */
static void timer2_cb(int fd _Unused, short kind _Unused, void *userp _Unused)
{
        CURLcode rc;
        session_t *sess = (session_t *)userp;
        unsigned bytes = random() % 128 * 1024 + 1;
        unsigned avail;
        void *data;

        buffer_peek(&sess->buffer, &data, &avail);

        if (bytes > avail)
                bytes = avail;

        if (bytes == 0)
                return;

        fwrite(data, 1, bytes, fp);
        buffer_discard(&sess->buffer, bytes);

        /* if we’ve just emptied the buffer completely, resume streaming */
        if (buffer_isempty(&sess->buffer)) {
                rc = curl_easy_pause(sess->curl, CURLPAUSE_CONT);
        }
}

and exists to pace the draining of data as it’s arriving from the network, and to smooth out any burstiness.

Problem is that weird things seem to happen if you’re able to unpause your Curl handle in the timer callback, then that causes the write callback to fire immediately (before the timer callback has even completed).

I’m not using threading.

What am I missing?

Thanks,

-Philip

-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2018-03-13