Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add available() function to subscriber #587

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/sw/redis++/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <cassert>
#include <tuple>
#include <algorithm>
#include <poll.h>
#include "sw/redis++/reply.h"
#include "sw/redis++/command.h"
#include "sw/redis++/command_args.h"
Expand Down Expand Up @@ -253,6 +254,28 @@ ReplyUPtr Connection::recv(bool handle_error_reply) {
return reply;
}

bool Connection::avail() {
auto *ctx = _context();

assert(ctx != nullptr);

auto fd = ctx->fd;

struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN; // Check for readability

int result = poll(&pfd, 1, 0); // Timeout set to 0 milliseconds
if (result > 0 && (pfd.revents & POLLIN)) {
return true;
} else if (result == 0) {
return false;
} else {
// An error occurred
throw_error(*ctx, strerror(errno));
}
}

#ifdef REDIS_PLUS_PLUS_RESP_VERSION_3

void Connection::set_push_callback(redisPushFn *push_func) {
Expand Down
2 changes: 2 additions & 0 deletions src/sw/redis++/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ class Connection {

ReplyUPtr recv(bool handle_error_reply = true);

bool avail();

const ConnectionOptions& options() const {
return _opts;
}
Expand Down
6 changes: 6 additions & 0 deletions src/sw/redis++/subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ void Subscriber::consume() {
}
}

bool Subscriber::available() {
_check_connection();

return _connection.avail();
}

Subscriber::MsgType Subscriber::_msg_type(redisReply *reply) const {
if (reply == nullptr) {
throw ProtoError("Null type reply.");
Expand Down
2 changes: 2 additions & 0 deletions src/sw/redis++/subscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ class Subscriber {

void consume();

bool available();

private:
friend class Redis;

Expand Down