Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 8d7c5fa

Browse files
committed
update
1 parent 6a7d288 commit 8d7c5fa

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

engine/services/download_service.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ void DownloadService::ProcessTask(DownloadTask& task, int worker_id) {
356356
// if the download has error, we are not run the callback
357357
ExecuteCallback(task);
358358
EmitTaskCompleted(task.id);
359+
{
360+
std::lock_guard<std::mutex> lock(event_emit_map_mutex);
361+
event_emit_map_.erase(task.id);
362+
}
359363
}
360364

361365
worker_data->downloading_data_map.clear();
@@ -372,11 +376,19 @@ cpp::result<void, std::string> DownloadService::ProcessMultiDownload(
372376
auto result = ProcessCompletedTransfers(multi_handle);
373377
if (result.has_error()) {
374378
EmitTaskError(task.id);
379+
{
380+
std::lock_guard<std::mutex> lock(event_emit_map_mutex);
381+
event_emit_map_.erase(task.id);
382+
}
375383
return cpp::fail(result.error());
376384
}
377385

378386
if (task.status == DownloadTask::Status::Cancelled || stop_flag_) {
379387
EmitTaskStopped(task.id);
388+
{
389+
std::lock_guard<std::mutex> lock(event_emit_map_mutex);
390+
event_emit_map_.erase(task.id);
391+
}
380392
return cpp::fail("Task " + task.id + " cancelled");
381393
}
382394
} while (still_running);

engine/services/download_service.h

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ class DownloadService {
137137
callbacks_;
138138
std::mutex callbacks_mutex_;
139139

140+
std::unordered_map<std::string,
141+
std::chrono::time_point<std::chrono::steady_clock>>
142+
event_emit_map_;
143+
std::mutex event_emit_map_mutex;
144+
140145
void WorkerThread();
141146

142147
bool IsTaskTerminated(const std::string& task_id);
@@ -154,18 +159,17 @@ class DownloadService {
154159
return 0;
155160
}
156161

157-
if (downloading_data->download_service == nullptr) {
162+
auto dl_srv = downloading_data->download_service;
163+
if (dl_srv == nullptr) {
158164
return 0;
159165
}
160166

161167
// Lock during the update and event emission
162-
std::lock_guard<std::mutex> lock(
163-
downloading_data->download_service->active_tasks_mutex_);
168+
std::lock_guard<std::mutex> lock(dl_srv->active_tasks_mutex_);
164169

165170
// Find and update the task
166-
if (auto task_it = downloading_data->download_service->active_tasks_.find(
167-
downloading_data->task_id);
168-
task_it != downloading_data->download_service->active_tasks_.end()) {
171+
if (auto task_it = dl_srv->active_tasks_.find(downloading_data->task_id);
172+
task_it != dl_srv->active_tasks_.end()) {
169173
auto& task = task_it->second;
170174
// Find the specific item in the task
171175
for (auto& item : task->items) {
@@ -182,22 +186,38 @@ class DownloadService {
182186
item.bytes = dltotal;
183187
item.downloadedBytes = dlnow;
184188

185-
static auto last_event_time = std::chrono::steady_clock::now();
186-
auto current_time = std::chrono::steady_clock::now();
187-
auto time_since_last_event =
188-
std::chrono::duration_cast<std::chrono::milliseconds>(
189-
current_time - last_event_time)
190-
.count();
191-
192-
// throttle event by 1 sec
193-
if (time_since_last_event >= 1000) {
194-
downloading_data->download_service->event_queue_->enqueue(
195-
EventType::DownloadEvent,
196-
DownloadEvent{.type_ = DownloadEventType::DownloadUpdated,
197-
.download_task_ = *task});
198-
199-
// Update the last event time
200-
last_event_time = current_time;
189+
// Emit the event
190+
{
191+
std::lock_guard<std::mutex> event_lock(dl_srv->event_emit_map_mutex);
192+
// find the task id in event_emit_map
193+
// if not found, add it to the map
194+
auto should_emit_event{false};
195+
if (dl_srv->event_emit_map_.find(task->id) !=
196+
dl_srv->event_emit_map_.end()) {
197+
auto last_event_time = dl_srv->event_emit_map_[task->id];
198+
auto current_time = std::chrono::steady_clock::now();
199+
200+
auto time_since_last_event =
201+
std::chrono::duration_cast<std::chrono::milliseconds>(
202+
current_time - last_event_time)
203+
.count();
204+
if (time_since_last_event >= 1000) {
205+
// if the time since last event is more than 1 sec, emit the event
206+
should_emit_event = true;
207+
}
208+
} else {
209+
// if the task id is not found in the map, emit
210+
should_emit_event = true;
211+
}
212+
213+
if (should_emit_event) {
214+
dl_srv->event_queue_->enqueue(
215+
EventType::DownloadEvent,
216+
DownloadEvent{.type_ = DownloadEventType::DownloadUpdated,
217+
.download_task_ = *task});
218+
dl_srv->event_emit_map_[task->id] =
219+
std::chrono::steady_clock::now();
220+
}
201221
}
202222

203223
break;

0 commit comments

Comments
 (0)