Skip to content

Commit

Permalink
Remove unnecessary lambda usage
Browse files Browse the repository at this point in the history
  • Loading branch information
hamidr committed Jan 12, 2017
1 parent 3e6b3f1 commit b0bc663
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 47 deletions.
1 change: 0 additions & 1 deletion includes/async_redis/sentinel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ namespace async_redis {
static std::vector<std::string> parse_watch_master_change(const parser_t& event);

private:
bool if_connected_do(std::function<bool ()>&& fn);
void connect_all(const string& ip, int port, const connect_cb_t& connector);
void check_connected(const connect_cb_t& connector, bool res);
bool send(std::list<string>&& words, connection::reply_cb_t&& reply);
Expand Down
1 change: 0 additions & 1 deletion src/monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ void monitor::stream_received(ssize_t len)
}
}

// if (!(watchers_.size() || pwatchers_.size()))
if (!watchers_.size() && !pwatchers_.size()) {
is_watching_ = false;
return;
Expand Down
77 changes: 32 additions & 45 deletions src/sentinel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,64 +31,60 @@ void sentinel::disconnect() {

bool sentinel::failover(const string& clustername, connection::reply_cb_t&& reply)
{
return if_connected_do(
[&]() -> bool {
return send({std::string("sentinel failover ") + clustername}, std::move(reply));
}
);
if (!is_connected())
return false;

return send({std::string("sentinel failover ") + clustername}, std::move(reply));
}

bool sentinel::ping(connection::reply_cb_t&& reply)
{
return if_connected_do(
[&]() -> bool {
return send({"ping"}, std::move(reply));
}
);
if (!is_connected())
return false;

return send({"ping"}, std::move(reply));
}

bool sentinel::watch_master_change(cb_watch_master_change_t&& fn)
{
return if_connected_do(
[&]()-> bool {
using State = monitor::EventState;

return stream_.subscribe({"+switch-master"},
[this, fn = std::move(fn)](const string& channel, parser_t event, State state) -> void
{
switch(state)
{
case State::Stream:
return fn(parse_watch_master_change(event), SentinelState::Watching);
if (!is_connected())
return false;

case State::Disconnected:
this->disconnect();
return fn({}, SentinelState::Disconnected);
break;
using State = monitor::EventState;

default:
break;
}
}
);
return stream_.subscribe({"+switch-master"},
[this, fn{std::move(fn)}](const string& channel, parser_t event, State state) -> void
{
switch(state)
{
case State::Stream:
return fn(parse_watch_master_change(event), SentinelState::Watching);

case State::Disconnected:
this->disconnect();
return fn({}, SentinelState::Disconnected);
break;

default:
break;
}
}
);
}


bool sentinel::master_addr_by_name(const string& cluster_name, cb_addr_by_name_t&& cb)
{
return if_connected_do(
[&]() -> bool {
return send_master_addr_by_name(cluster_name, std::move(cb));
}
);
if (!is_connected())
return false;

return send_master_addr_by_name(cluster_name, std::move(cb));
}

bool sentinel::send_master_addr_by_name(const string& cluster_name, cb_addr_by_name_t&& cb)
{
return this->send({string("SENTINEL get-master-addr-by-name ") + cluster_name + "\r\n"},
[cb = std::move(cb)](parser_t parsed_value)
[cb{std::move(cb)}](parser_t parsed_value)
{
using ::async_redis::parser::RespType;

Expand Down Expand Up @@ -122,7 +118,6 @@ bool sentinel::send_master_addr_by_name(const string& cluster_name, cb_addr_by_n
);
}

// static
std::vector<std::string> sentinel::parse_watch_master_change(const parser_t& event)
{
std::vector<string> words;
Expand All @@ -135,14 +130,6 @@ std::vector<std::string> sentinel::parse_watch_master_change(const parser_t& eve
return words;
}

bool sentinel::if_connected_do(std::function<bool ()>&& fn)
{
if (!is_connected())
return false;

return fn();
}

void sentinel::connect_all(const string& ip, int port, const connect_cb_t& connector)
{
auto cb = std::bind(&sentinel::check_connected, this, connector, std::placeholders::_1);
Expand Down

0 comments on commit b0bc663

Please sign in to comment.