You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by GitBox <gi...@apache.org> on 2020/02/27 04:28:26 UTC

[GitHub] [incubator-brpc] pklim101 opened a new issue #1050: ParallelChannel并发HTTP请求,但是响应里没有body数据

pklim101 opened a new issue #1050: ParallelChannel并发HTTP请求,但是响应里没有body数据
URL: https://github.com/apache/incubator-brpc/issues/1050
 
 
   ParallelChannel并发HTTP请求,但是响应里没有body数据,而且打印的”test-protocol“也是为0,但是前面已经设置了HTTP协议的,不应该为0.
   
   代码如下:
   `DEFINE_string(server, "127.0.0.1:8080", "IP Address of server");
   static void* sender(void* arg) {
           auto channel = static_cast<google::protobuf::RpcChannel*>(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<bthread_t> 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;
   }`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org