EulerianTechnologies / eredis

Fast and light Redis C client library built over Hiredis, thread-safe, write replication, auto-reconnect, sync pool, async libev.
BSD 3-Clause "New" or "Revised" License
81 stars 38 forks source link

publish/subscribe #7

Closed CurtTilmes closed 7 years ago

CurtTilmes commented 7 years ago

What is the right way to implement publish/subscribe on top of eredis?

I tried sending a 'subscribe' command, and it works fine, but I can't call eredis_r_reply() in a blocking manner to wait for a response that hasn't come yet.

CurtTilmes commented 7 years ago

I added this little sub and it seems to work for pub/sub, but I'm not sure I'm doing everything right..

/**
 * @brief eredis reader reply, blocking for pub/sub
 *
 * @param r     eredis reader
 *
 * @return reply (redisReply)
 */
  eredis_reply_t *
eredis_r_reply_blocking( eredis_reader_t *r )
{
    redisContext *c;
    eredis_reply_t *reply;
    int err;

    reply = NULL;

    if (!(c = _eredis_r_ctx(r, 0)))
        return NULL;

    err = redisGetReply( c, (void**)&reply );

    if (err == EREDIS_OK) {
      /* Good */
      /* Previous to clean? */
      _eredis_r_free_reply( r );
      /* New reply */
      r->reply = reply;
    } else {
      /* Bad */
      if (reply)
        freeReplyObject( reply );

      err = c->err;
    }

    return reply;
}
guillaumef commented 7 years ago

Moved to pull request #8