ParallelChannel并发HTTP请求,但是响应里没有body数据
- Dominant language
- C++
- Stars
- 17.6k
- Forks
- 4.1k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 69
Description
ParallelChannel并发HTTP请求,有如下三个问题请教:
1.请求响应是403,但是代码中设置了path部分(cntl.http_request().uri() = "http://127.0.0.1:8080/article";),而服务端的access日志收到的确是http://127.0.0.1:8080,是哪里没设置对吗?
2.”test-protocol“也是为0,但是前面已经设置了HTTP协议的,不应该为0.
3.如果换请求其它url,虽然!cntl.Failed()为真,但是body内容却打印不来,如代码中:test-body部分。
代码如下:
DEFINE_string(server, "127.0.0.1:8080", "IP Address of server");
static void* sender(void* arg) {
auto channel = static_cast(arg);
brpc::Controller cntl;
example::HttpRequest request;
example::HttpResponse response;
cntl.set_log_id(100);
cntl.http_request().uri() = "http://127.0.0.1:8080/article";
cntl.http_request().SetHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:73.0) Gecko/20100101 Firefox/73.1");
cntl.http_request().uri().SetQuery("cid", "20");
cntl.http_request().uri().SetQuery("tag", "xxx");
cntl.http_request().set_content_type("text/plain");
channel->CallMethod(NULL, &cntl, &request, &response, NULL);
std::cout << "test-protocol:" << cntl.request_protocol() << std::endl;
butil::IOBuf& buf = cntl.request_attachment();
std::string str = cntl.request_attachment().to_string(); // 有拷贝
std::cout << "test-body:" << str << std::endl;
if (!cntl.Failed()) {
std::cout << "success." << std::endl;
std::cout << "test-body2:" << cntl.response_attachment() << std::endl;
} else {
std::cout << cntl.response_attachment() << std::endl;
bthread_usleep(50000);
}
return NULL;
}
int main(int argc, char* argv[]) {
GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
brpc::ParallelChannel channel;
brpc::ParallelChannelOptions pchan_options;
pchan_options.timeout_ms = FLAGS_timeout_ms;
pchan_options.fail_limit = 0;
if (channel.Init(&pchan_options) != 0) {
LOG(ERROR) << "Fail to init ParallelChannel";
return -1;
}
brpc::ChannelOptions sub_options;
sub_options.protocol = "http";
sub_options.connection_type = FLAGS_connection_type;
sub_options.max_retry = FLAGS_max_retry;
for (int i = 0; i < FLAGS_channel_num; ++i) {
brpc::Channel* sub_channel = new brpc::Channel;
if (sub_channel->Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &sub_options) != 0) {
LOG(ERROR) << "Fail to initialize sub_channel[" << i << "]";
return -1;
}
if (channel.AddChannel(sub_channel, brpc::OWNS_CHANNEL,
NULL, NULL) != 0) {
LOG(ERROR) << "Fail to AddChannel, i=" << i;
return -1;
}
}
std::vector bids;
bids.resize(FLAGS_thread_num);
for (int i = 0; i < FLAGS_thread_num; ++i) {
if (bthread_start_background(
&bids[i], NULL, sender, &channel) != 0) {
LOG(ERROR) << "Fail to create bthread";
return -1;
}
}
for (int i = 0; i < FLAGS_thread_num; ++i) {
if (!FLAGS_use_bthread) {
pthread_join(pids[i], NULL);
} else {
bthread_join(bids[i], NULL);
}
}
return 0;
}
Contributor guide
Research direction
Start with the sender function and the ParallelChannel and ChannelOptions setup shown in the issue, then reproduce the reported HTTP request behavior. Trace the protocol, URI, and response-attachment values after CallMethod returns. Done means the causes of the 403, protocol value, and missing response body are identified and documented or corrected with verification.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100