Skip to content

Commit

Permalink
Add support to progress function to abort operation (#128)
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <[email protected]>
  • Loading branch information
balamurugana authored Apr 2, 2024
1 parent 0eb2a19 commit d3d828e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
3 changes: 2 additions & 1 deletion examples/GetObjectProgress.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ int main() {
<< std::endl;
return true;
};
args.progressfunc = [](minio::http::ProgressFunctionArgs args) -> void {
args.progressfunc = [](minio::http::ProgressFunctionArgs args) -> bool {
if (args.download_speed > 0) {
std::cout << "downloaded speed: " << (long)args.download_speed << " bps"
<< std::endl;
} else {
std::cout << "downloaded: " << (long)args.downloaded_bytes << " bytes of "
<< (long)args.download_total_bytes << " bytes" << std::endl;
}
return true;
};

// Call get object.
Expand Down
3 changes: 2 additions & 1 deletion examples/PutObjectProgress.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ int main() {
minio::s3::PutObjectArgs args(file, 47615315, 15728640);
args.bucket = "my-bucket";
args.object = "my-object";
args.progressfunc = [](minio::http::ProgressFunctionArgs args) -> void {
args.progressfunc = [](minio::http::ProgressFunctionArgs args) -> bool {
if (args.upload_speed > 0) {
std::cout << "uploaded speed: " << (long)args.upload_speed << " bps"
<< std::endl;
} else {
std::cout << "uploaded: " << (long)args.uploaded_bytes << " bytes of "
<< (long)args.upload_total_bytes << " bytes" << std::endl;
}
return true;
};

// Call put object.
Expand Down
2 changes: 1 addition & 1 deletion include/miniocpp/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ using DataFunction = std::function<bool(DataFunctionArgs)>;

struct ProgressFunctionArgs;

using ProgressFunction = std::function<void(ProgressFunctionArgs)>;
using ProgressFunction = std::function<bool(ProgressFunctionArgs)>;

struct Response;

Expand Down
14 changes: 10 additions & 4 deletions src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -484,21 +484,21 @@ PutObjectResponse Client::PutObject(PutObjectArgs args, std::string& upload_id,
[&object_size = object_size, &uploaded_bytes = uploaded_bytes,
&upload_speed = upload_speed, &progressfunc = args.progressfunc,
&progress_userdata = args.progress_userdata](
http::ProgressFunctionArgs args) -> void {
http::ProgressFunctionArgs args) -> bool {
if (args.upload_speed > 0) {
if (upload_speed == -1) {
upload_speed = args.upload_speed;
} else {
upload_speed = (upload_speed + args.upload_speed) / 2;
}
return;
return true;
}

http::ProgressFunctionArgs actual_args;
actual_args.upload_total_bytes = static_cast<double>(object_size);
actual_args.uploaded_bytes = uploaded_bytes + args.uploaded_bytes;
actual_args.userdata = progress_userdata;
progressfunc(actual_args);
return progressfunc(actual_args);
};
}
if (args.sse != nullptr) {
Expand All @@ -514,7 +514,10 @@ PutObjectResponse Client::PutObject(PutObjectArgs args, std::string& upload_id,
actual_args.upload_total_bytes = static_cast<double>(object_size);
actual_args.uploaded_bytes = uploaded_bytes;
actual_args.userdata = args.progress_userdata;
args.progressfunc(actual_args);
if (!args.progressfunc(actual_args)) {
return UploadPartResponse(
error::Error("aborted by progress function"));
}
}
parts.push_back(Part(part_number, std::move(resp.etag)));
} else {
Expand All @@ -533,6 +536,7 @@ PutObjectResponse Client::PutObject(PutObjectArgs args, std::string& upload_id,
http::ProgressFunctionArgs actual_args;
actual_args.upload_speed = upload_speed;
actual_args.userdata = args.progress_userdata;
// ignore the return value as we completed the upload
args.progressfunc(actual_args);
}
return PutObjectResponse(resp);
Expand Down Expand Up @@ -792,6 +796,8 @@ UploadObjectResponse Client::UploadObject(UploadObjectArgs args) {
po_args.retention = std::move(args.retention);
po_args.legal_hold = std::move(args.legal_hold);
po_args.content_type = std::move(args.content_type);
po_args.progressfunc = std::move(args.progressfunc);
po_args.progress_userdata = std::move(args.progress_userdata);

PutObjectResponse resp = PutObject(std::move(po_args));
file.close();
Expand Down
6 changes: 4 additions & 2 deletions src/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,10 @@ Response Request::execute() {
args.upload_total_bytes = ultotal;
args.uploaded_bytes = ulnow;
args.userdata = progress_userdata;
progressfunc(args);
return CURL_PROGRESSFUNC_CONTINUE;
if (progressfunc(args)) {
return CURL_PROGRESSFUNC_CONTINUE;
}
return 1;
};
if (progressfunc != nullptr) {
request.setOpt(new curlpp::options::NoProgress(false));
Expand Down

0 comments on commit d3d828e

Please sign in to comment.