Skip to content

Commit

Permalink
Parse the timeout parameter as double in the blocking command (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yangsx-1 authored Jun 27, 2023
1 parent 66c1e89 commit 2abf0ad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
12 changes: 7 additions & 5 deletions src/commands/cmd_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ class CommandBPop : public Commander,
~CommandBPop() override = default;

Status Parse(const std::vector<std::string> &args) override {
auto parse_result = ParseInt<int>(args[args.size() - 1], 10);
auto parse_result = ParseFloat(args[args.size() - 1]);
if (!parse_result) {
return {Status::RedisParseErr, "timeout is not an integer or out of range"};
return {Status::RedisParseErr, errTimeoutIsNotFloat};
}

if (*parse_result < 0) {
return {Status::RedisParseErr, "timeout should not be negative"};
}

timeout_ = *parse_result;
timeout_ = static_cast<int64_t>(*parse_result * 1000 * 1000);

keys_ = std::vector<std::string>(args.begin() + 1, args.end() - 1);
return Commander::Parse(args);
Expand Down Expand Up @@ -203,7 +203,9 @@ class CommandBPop : public Commander,

if (timeout_) {
timer_.reset(NewTimer(bufferevent_get_base(bev)));
timeval tm = {timeout_, 0};
int64_t timeout_second = timeout_ / 1000 / 1000;
int64_t timeout_microsecond = timeout_ % (1000 * 1000);
timeval tm = {timeout_second, static_cast<int>(timeout_microsecond)};
evtimer_add(timer_.get(), &tm);
}

Expand Down Expand Up @@ -284,7 +286,7 @@ class CommandBPop : public Commander,

private:
bool left_ = false;
int timeout_ = 0; // seconds
int64_t timeout_ = 0; // microseconds
std::vector<std::string> keys_;
Server *svr_ = nullptr;
Connection *conn_ = nullptr;
Expand Down
20 changes: 12 additions & 8 deletions src/commands/cmd_zset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,14 @@ class CommandBZPop : public Commander,
explicit CommandBZPop(bool min) : min_(min) {}

Status Parse(const std::vector<std::string> &args) override {
auto parse_result = ParseInt<int>(args[args.size() - 1], 10);
auto parse_result = ParseFloat(args[args.size() - 1]);
if (!parse_result) {
return {Status::RedisParseErr, "timeout is not an integer or out of range"};
return {Status::RedisParseErr, errTimeoutIsNotFloat};
}
if (*parse_result < 0) {
return {Status::RedisParseErr, errTimeoutIsNegative};
}
timeout_ = *parse_result;
timeout_ = static_cast<int64_t>(*parse_result * 1000 * 1000);

keys_ = std::vector<std::string>(args.begin() + 1, args.end() - 1);
return Commander::Parse(args);
Expand Down Expand Up @@ -346,7 +346,9 @@ class CommandBZPop : public Commander,

if (timeout_) {
timer_.reset(NewTimer(bufferevent_get_base(bev)));
timeval tm = {timeout_, 0};
int64_t timeout_second = timeout_ / 1000 / 1000;
int64_t timeout_microsecond = timeout_ % (1000 * 1000);
timeval tm = {timeout_second, static_cast<int>(timeout_microsecond)};
evtimer_add(timer_.get(), &tm);
}

Expand Down Expand Up @@ -420,7 +422,7 @@ class CommandBZPop : public Commander,

private:
bool min_;
int timeout_;
int64_t timeout_ = 0; // microseconds
std::vector<std::string> keys_;
Server *svr_ = nullptr;
Connection *conn_ = nullptr;
Expand Down Expand Up @@ -522,7 +524,7 @@ class CommandBZMPop : public Commander,
Status Parse(const std::vector<std::string> &args) override {
CommandParser parser(args, 1);

timeout_ = GET_OR_RET(parser.TakeInt<int>(NumericRange<int>{0, std::numeric_limits<int>::max()}));
timeout_ = static_cast<int64_t>(GET_OR_RET(parser.TakeFloat<double>()) * 1000 * 1000);
if (timeout_ < 0) {
return {Status::RedisParseErr, errTimeoutIsNegative};
}
Expand Down Expand Up @@ -584,7 +586,9 @@ class CommandBZMPop : public Commander,

if (timeout_) {
timer_.reset(NewTimer(bufferevent_get_base(bev)));
timeval tm = {timeout_, 0};
int64_t timeout_second = timeout_ / 1000 / 1000;
int64_t timeout_microsecond = timeout_ % (1000 * 1000);
timeval tm = {timeout_second, static_cast<int>(timeout_microsecond)};
evtimer_add(timer_.get(), &tm);
}

Expand Down Expand Up @@ -651,7 +655,7 @@ class CommandBZMPop : public Commander,
}

private:
int timeout_ = 0; // seconds
int64_t timeout_ = 0; // microseconds
int num_keys_;
std::vector<std::string> keys_;
enum { ZSET_MIN, ZSET_MAX, ZSET_NONE } flag_ = ZSET_NONE;
Expand Down
1 change: 1 addition & 0 deletions src/commands/error_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ inline constexpr const char *errNoSuchKey = "no such key";
inline constexpr const char *errUnbalancedStreamList =
"Unbalanced XREAD list of streams: for each stream key an ID or '$' must be specified.";
inline constexpr const char *errTimeoutIsNegative = "timeout is negative";
inline constexpr const char *errTimeoutIsNotFloat = "timeout is not a float or out of range";
inline constexpr const char *errLimitIsNegative = "LIMIT can't be negative";
inline constexpr const char *errLimitOptionNotAllowed =
"syntax error, LIMIT cannot be used without the special ~ option";
Expand Down

0 comments on commit 2abf0ad

Please sign in to comment.