Commit 98654160 authored by zhujiashun's avatar zhujiashun

move grpc timeout related code to grpc.cpp

parent 0f9d7878
......@@ -166,4 +166,49 @@ void PercentDecode(const std::string& str, std::string* str_out) {
}
}
int64_t ConvertGrpcTimeoutToUS(const std::string* grpc_timeout) {
if (!grpc_timeout || grpc_timeout->empty()) {
return -1;
}
const char timeout_unit = grpc_timeout->back();
char* endptr = NULL;
int64_t timeout_value = (int64_t)strtol(grpc_timeout->data(), &endptr, 10);
// Only the format that the digit number is equal to (timeout header size - 1)
// is valid. Otherwise the format is not valid and is treated as no deadline.
// For example:
// "1H", "2993S", "82m" is valid.
// "30A" is also valid, but the following switch would fall into default
// case and return -1 since 'A' is not a valid time unit.
// "123ASH" is not vaid since the digit number is 3, while the size is 6.
// "HHH" is not valid since the dight number is 0, while the size is 3.
if ((size_t)(endptr - grpc_timeout->data()) != grpc_timeout->size() - 1) {
return -1;
}
switch (timeout_unit) {
case 'H':
timeout_value *= (3600 * 1000000L);
break;
case 'M':
timeout_value *= (60 * 1000000L);
break;
case 'S':
timeout_value *= 1000000L;
break;
case 'm':
timeout_value *= 1000L;
break;
case 'u':
break;
case 'n':
timeout_value = (timeout_value + 500) / 1000;
if (timeout_value == 0) {
timeout_value = 1;
}
break;
default:
return -1;
}
return timeout_value;
}
} // namespace brpc
......@@ -50,6 +50,8 @@ int is_failed_after_queries(const http_parser* parser);
int is_failed_after_http_version(const http_parser* parser);
DECLARE_bool(http_verbose);
DECLARE_int32(http_verbose_max_body_length);
// Defined in grpc.cpp
int64_t ConvertGrpcTimeoutToUS(const std::string* grpc_timeout);
namespace policy {
......@@ -1194,35 +1196,6 @@ void EndRunningCallMethodInPool(
::google::protobuf::Message* response,
::google::protobuf::Closure* done);
static int64_t ConvertGrpcTimeoutToUS(int64_t timeout_value, const char timeout_unit) {
switch (timeout_unit) {
case 'H':
timeout_value *= (3600 * 1000000L);
break;
case 'M':
timeout_value *= (60 * 1000000L);
break;
case 'S':
timeout_value *= 1000000L;
break;
case 'm':
timeout_value *= 1000L;
break;
case 'u':
break;
case 'n':
timeout_value = (timeout_value + 500) / 1000;
if (timeout_value == 0) {
timeout_value = 1;
}
break;
default:
return -1;
}
return timeout_value;
}
void ProcessHttpRequest(InputMessageBase *msg) {
const int64_t start_parse_us = butil::cpuwide_time_us();
DestroyingPtr<HttpContext> imsg_guard(static_cast<HttpContext*>(msg));
......@@ -1451,27 +1424,11 @@ void ProcessHttpRequest(InputMessageBase *msg) {
return;
}
}
const std::string* grpc_timeout = req_header.GetHeader(common->GRPC_TIMEOUT);
if (grpc_timeout && !grpc_timeout->empty()) {
const char timeout_unit = grpc_timeout->back();
char* endptr = NULL;
int64_t timeout_value = (int64_t)strtol(grpc_timeout->data(), &endptr, 10);
// Only the format that the digit number is equal to (timeout header size - 1) is valid.
// Otherwise the format is not valid and is treated as no deadline.
// For example:
// 1H, 2993S, 82m is valid.
// 30A is also valid, but the following ConvertGrpcTimeoutToUS would return -1 since 'A'
// is not a valid time unit.
// 123ASH is not vaid since the digit number is 3, while the size is 6.
// HHH is not valid since the dight number is 0, while the size is 3.
if ((size_t)(endptr - grpc_timeout->data()) == grpc_timeout->size() - 1) {
int64_t timeout_value_us =
ConvertGrpcTimeoutToUS(timeout_value, timeout_unit);
if (timeout_value_us >= 0) {
accessor.set_deadline_us(
butil::gettimeofday_us() + timeout_value_us);
}
}
int64_t timeout_value_us =
ConvertGrpcTimeoutToUS(req_header.GetHeader(common->GRPC_TIMEOUT));
if (timeout_value_us >= 0) {
accessor.set_deadline_us(
butil::gettimeofday_us() + timeout_value_us);
}
}
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment