Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Manager::receive_tick() {

m_resource_manager->receive_tick();
m_chunk_manager->periodic_sync();
m_file_manager->periodic_close_idle();

// To ensure the downloads get equal chance over time at using
// various limited resources, like sockets for handshakes, cycle the
Expand Down
26 changes: 26 additions & 0 deletions src/torrent/data/file_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,30 @@ FileManager::close_least_active() {
close(least);
}

void
FileManager::periodic_close_idle() {
if (m_close_idle == 0 || empty())
return;

auto now = this_thread::cached_time();
auto idle = std::chrono::seconds(m_close_idle);

size_type i = 0;
while (i < size()) {
File* f = base_type::operator[](i);

if (!f->is_open()) {
++i;
continue;
}

auto touched = std::chrono::microseconds(f->last_touched());

if (touched <= now && now - touched >= idle)
close(f);
else
++i;
}
}

} // namespace torrent
8 changes: 6 additions & 2 deletions src/torrent/data/file_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ class LIBTORRENT_EXPORT FileManager : private std::vector<File*> {
bool advise_random_hashing() const { return m_advise_random_hashing; }
void set_advise_random_hashing(bool state) { m_advise_random_hashing = state; }

// Idle seconds before closing open FDs; 0 disables.
uint32_t close_idle() const { return m_close_idle; }
void set_close_idle(uint32_t seconds) { m_close_idle = seconds; }

bool open(value_type file, bool hashing, int prot, int flags);
void close(value_type file);

// TODO: Close all files held by a download after hashing. Also flush all memory chunks.

void close_least_active();
void periodic_close_idle();

// Statistics:
uint64_t files_opened_counter() const { return m_files_opened_counter; }
Expand All @@ -53,6 +56,7 @@ class LIBTORRENT_EXPORT FileManager : private std::vector<File*> {
FileManager& operator=(const FileManager&) = delete;

size_type m_max_open_files{0};
uint32_t m_close_idle{60};
bool m_advise_random{false};
bool m_advise_random_hashing{false};

Expand Down
Loading