Commit 2071a22c authored by jamesge's avatar jamesge

Controller::CreateProgressiveAttachment returns intrusive_ptr instead of raw…

Controller::CreateProgressiveAttachment returns intrusive_ptr instead of raw pointer which could be deleted and invalid before usage
parent 11da9c8c
......@@ -337,7 +337,7 @@ brpc server支持发送超大或无限长的body。方法如下:
```c++
#include <brpc/progressive_attachment.h>
...
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa(cntl->CreateProgressiveAttachment());
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa = cntl->CreateProgressiveAttachment();
```
2. 调用ProgressiveAttachment::Write()发送数据。
......
......@@ -338,7 +338,7 @@ brpc server is capable of sending large or infinite sized body, in following ste
```c++
#include <brpc/progressive_attachment.h>
...
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa (cntl->CreateProgressiveAttachment());
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa = cntl->CreateProgressiveAttachment();
```
2. Call `ProgressiveAttachment::Write()` to send the data.
......
......@@ -28,7 +28,7 @@
DEFINE_string(d, "", "POST this data to the http server");
DEFINE_string(load_balancer, "", "The algorithm for load balancing");
DEFINE_int32(timeout_ms, 1000, "RPC timeout in milliseconds");
DEFINE_int32(timeout_ms, 2000, "RPC timeout in milliseconds");
DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
DEFINE_string(protocol, "http", "Client-side protocol");
......
......@@ -69,17 +69,20 @@ public:
FileServiceImpl() {};
virtual ~FileServiceImpl() {};
static void* SendLargeFile(void* arg) {
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa(
(brpc::ProgressiveAttachment*)arg);
if (pa == NULL) {
struct Args {
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa;
};
static void* SendLargeFile(void* raw_args) {
std::unique_ptr<Args> args(static_cast<Args*>(raw_args));
if (args->pa == NULL) {
LOG(ERROR) << "ProgressiveAttachment is NULL";
return NULL;
}
for (int i = 0; i < 100; ++i) {
char buf[16];
int len = snprintf(buf, sizeof(buf), "part_%d ", i);
pa->Write(buf, len);
args->pa->Write(buf, len);
// sleep a while to send another part.
bthread_usleep(10000);
......@@ -97,9 +100,10 @@ public:
const std::string& filename = cntl->http_request().unresolved_path();
if (filename == "largefile") {
// Send the "largefile" with ProgressiveAttachment.
std::unique_ptr<Args> args(new Args);
args->pa = cntl->CreateProgressiveAttachment();
bthread_t th;
bthread_start_background(&th, NULL, SendLargeFile,
cntl->CreateProgressiveAttachment());
bthread_start_background(&th, NULL, SendLargeFile, args.release());
} else {
cntl->response_attachment().append("Getting file: ");
cntl->response_attachment().append(filename);
......
......@@ -1345,7 +1345,7 @@ void Controller::set_stream_creator(StreamCreator* sc) {
_stream_creator = sc;
}
ProgressiveAttachment*
butil::intrusive_ptr<ProgressiveAttachment>
Controller::CreateProgressiveAttachment(StopStyle stop_style) {
if (has_progressive_writer()) {
LOG(ERROR) << "One controller can only have one ProgressiveAttachment";
......@@ -1365,10 +1365,9 @@ Controller::CreateProgressiveAttachment(StopStyle stop_style) {
if (stop_style == FORCE_STOP) {
httpsock->fail_me_at_server_stop();
}
ProgressiveAttachment* pb = new ProgressiveAttachment(
httpsock, http_request().before_http_1_1());
_wpa.reset(pb);
return pb;
_wpa.reset(new ProgressiveAttachment(
httpsock, http_request().before_http_1_1()));
return _wpa;
}
void Controller::ReadProgressiveAttachmentBy(ProgressiveReader* r) {
......
......@@ -380,8 +380,9 @@ public:
// If `stop_style' is FORCE_STOP, the underlying socket will be failed
// immediately when the socket becomes idle or server is stopped.
// Default value of `stop_style' is WAIT_FOR_STOP.
ProgressiveAttachment*
butil::intrusive_ptr<ProgressiveAttachment>
CreateProgressiveAttachment(StopStyle stop_style = WAIT_FOR_STOP);
bool has_progressive_writer() const { return _wpa != NULL; }
// Set compression method for response.
......
......@@ -499,8 +499,8 @@ public:
cntl->http_response().set_content_type("text/plain");
brpc::StopStyle stop_style = (_nrep == std::numeric_limits<size_t>::max()
? brpc::FORCE_STOP : brpc::WAIT_FOR_STOP);
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa(
cntl->CreateProgressiveAttachment(stop_style));
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa
= cntl->CreateProgressiveAttachment(stop_style);
if (pa == NULL) {
cntl->SetFailed("The socket was just failed");
return;
......@@ -547,8 +547,8 @@ public:
cntl->http_response().set_content_type("text/plain");
brpc::StopStyle stop_style = (_nrep == std::numeric_limits<size_t>::max()
? brpc::FORCE_STOP : brpc::WAIT_FOR_STOP);
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa(
cntl->CreateProgressiveAttachment(stop_style));
butil::intrusive_ptr<brpc::ProgressiveAttachment> pa
= cntl->CreateProgressiveAttachment(stop_style);
if (pa == NULL) {
cntl->SetFailed("The socket was just failed");
return;
......
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