liu-meng-06 commented on issue #1835:
URL: 
https://github.com/apache/incubator-brpc/issues/1835#issuecomment-1185534843

   > 
   
   - 代码
   
   ``` c++
   #include "test.pb.h"
   #include <brpc/channel.h>
   #include <butil/logging.h>
   #include <memory>
   #include <boost/asio/thread_pool.hpp>
   #include <boost/asio/post.hpp>
   
   #define HOST "127.0.0.1"
   #define PORT (8009)
   
   class Done : public google::protobuf::Closure {
   public:
       explicit Done(int idx)
           : _idx(idx) {}
       ~Done() override = default;
   
       void Run() override {
           LOG(DEBUG) << "done, idx = " << _idx;
           delete this;
       }
   
   private:
       int _idx;
   };
   
   
   static int send(
       const std::string &urlPath, const google::protobuf::Message *req,
       google::protobuf::Message *resp, google::protobuf::Closure *done) {
       brpc::Controller cntl;
       brpc::Channel channel;
       cntl.http_request().uri() = urlPath;
       cntl.http_request().set_method(brpc::HTTP_METHOD_POST);
       brpc::ChannelOptions channel_options;
       channel_options.protocol = brpc::PROTOCOL_HTTP;
       channel_options.timeout_ms = 10000;
   
       try {
           if (channel.Init(HOST, PORT, &channel_options) != 0) {
               LOG(ERROR) << "Fail to init channel ";
               return -1;
           }
   
           channel.CallMethod(nullptr, &cntl, req, resp, done);
           if (!cntl.Failed()) {
               return -1;
           }
           LOG(WARNING) << "call " << cntl.http_request().uri()
                        << " failed : " << cntl.ErrorText();
       } catch (...) {
           LOG(DEBUG) << "channel init failed, address = ";
           return -1;
       }
       return 0;
   }
   
   int main() {
   
       boost::asio::thread_pool pool(5);
   
       for (int i = 0; i< 20; ++i) {
           boost::asio::post(pool, [=]() {
               // 如果不添加到 thread 中,运行没有问题
               // 或者添加到线程池,请求 ”/foo2“ 时,done = nullptr 运行也没有问题
               LOG(DEBUG) << "i = " << i;
               test::foo::FooRequest1 request1;
               test::foo::FooResponse1 response1;
               request1.set_id("foo2");
               request1.set_name("张三");
               request1.set_age(20);
               send("/foo1", &response1, &response1, nullptr);
   
               test::foo::FooRequest2 request2;
               test::foo::FooResponse2 response2;
               request2.set_id("foo2");
               request2.set_state(1);
               auto *done = new Done(i);
               send("/foo2", &response2, &response2, done);
   
           });
           usleep(100000);
       }
   
       pool.join();
   
       return 0;
   }
   
   ```
   
   - proto
   
   ``` proto
   syntax = "proto2";
   
   package test.foo;
   
   option  cc_enable_arenas = true;
   
   message FooRequest1 {
     optional string id = 1;
     optional string name = 2;
     optional int32 age = 3;
   }
   
   message FooResponse1 {}
   
   message FooRequest2 {
     optional string id = 1;
     optional int32 state = 2;
   }
   
   message FooResponse2 {}
   
   ```
   
   


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to