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