From ac35a28f5416c9f8e78a8dbcfc4f51b2e6d69705 Mon Sep 17 00:00:00 2001 From: Nidev Plontra Date: Tue, 18 Aug 2015 22:17:08 +0900 Subject: [PATCH] DLThread now has thread pool size limit A number of threads could be in the pool and prevent created thread from being GCed, since they have at least one ref count. --- lib/rbitter/dlthread.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/rbitter/dlthread.rb b/lib/rbitter/dlthread.rb index 079969f..a3adce5 100644 --- a/lib/rbitter/dlthread.rb +++ b/lib/rbitter/dlthread.rb @@ -5,6 +5,8 @@ module Rbitter class DLThread + MAX_THREADS = 20 + def initialize(dlfolder, large_flag) @dest = dlfolder if not File.directory?(dlfolder) @@ -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' @@ -43,7 +45,11 @@ def <<(url_array) } } } - } + end + + if @pool.length >= MAX_THREADS + job_cleanup + end @pool.push download_task end @@ -60,4 +66,4 @@ def job_cleanup end end end -end \ No newline at end of file +end