Skip to content

Commit

Permalink
DLThread now has thread pool size limit
Browse files Browse the repository at this point in the history
A number of threads could be in the pool and prevent created thread from
being GCed, since they have at least one ref count.
  • Loading branch information
nidev committed Aug 18, 2015
1 parent 5e54aea commit ac35a28
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/rbitter/dlthread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

module Rbitter
class DLThread
MAX_THREADS = 20

def initialize(dlfolder, large_flag)
@dest = dlfolder
if not File.directory?(dlfolder)
Expand All @@ -23,7 +25,7 @@ def initialize(dlfolder, large_flag)
end

def <<(url_array)
download_task = Thread.new {
download_task = Thread.new do
url_array.each { |url|
uri = URI.parse(@large_image ? url + ":large" : url)
ssl = uri.scheme.downcase == 'https'
Expand All @@ -43,7 +45,11 @@ def <<(url_array)
}
}
}
}
end

if @pool.length >= MAX_THREADS
job_cleanup
end

@pool.push download_task
end
Expand All @@ -60,4 +66,4 @@ def job_cleanup
end
end
end
end
end

0 comments on commit ac35a28

Please sign in to comment.