brpc
brpc copied to clipboard
brpc http client如何stream接收server返回的resp
hi ,我目前想用brpc做一些http相关的工作,基本的逻辑是client发送request给server,server那边会在一段时间内产生多个responses,所以client应该要持续的流式接收这些responses。目前我的client只能接收到最终的batch在一起resps,而不是每次streaming的收到返回值。帮忙看下这一块是否有解决办法呢,感谢🙏.
目前server的写resp的逻辑大概如下:
class ServerStreamData{
public:
ServerStreamData(brpc::Controller* controller,
::google::protobuf::Closure* done)
: controller_(controller),
done_(done) {
pa_ = controller_->CreateProgressiveAttachment();
controller_->http_response().set_content_type("text/event-stream");
controller_->http_response().set_status_code(200);
controller_->http_response().SetHeader("Connection", "keep-alive");
controller_->http_response().SetHeader("Cache-Control", "no-cache");
// stream模式下,先将done执行
done_->Run();
}
// server每次产生一个data就会调用一次这个write函数
// 直到结束
//
bool write(const std::string& attachment) {
io_buf_.clear();
io_buf_.append(attachment);
pa_->Write(io_buf_);
return true;
}
private:
brpc::Controller* controller_;
::google::protobuf::Closure* done_;
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa_;
butil::IOBuf io_buf_;
};
client目前的逻辑如下:
brpc::Controller cntl;
cntl.http_request().uri() = target_uri;
cntl.http_request().set_method(brpc::HTTP_METHOD_POST);
cntl.request_attachment().append(request);
// Because `done'(last parameter) is NULL, this function waits until
// the response comes back or error occurs(including timeout).
channel_ptr->CallMethod(NULL, &cntl, NULL, NULL, NULL);
if (cntl.Failed()) {
LOG(ERROR) << "Call method error: " << cntl.ErrorText();
return;
}
LOG(INFO) << "get response: " << cntl.response_attachment().to_string();
我看了下rpc模式下有支持streaming client的方法,不知道在http下面怎么解决呢,谢谢。
看看持续下载