mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
changes to upload progress: use instance vars to keep from locking the main hash. update the progress only every second.
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@289 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
parent
cee3b05809
commit
f23d7778fa
1 changed files with 40 additions and 18 deletions
|
@ -5,36 +5,58 @@ class Mongrel::Uploads
|
|||
include Singleton
|
||||
|
||||
def initialize
|
||||
@guard = Mutex.new
|
||||
@guard = Mutex.new
|
||||
@counters = {}
|
||||
end
|
||||
|
||||
def check(upid)
|
||||
@counters[upid]
|
||||
status = @counters[upid]
|
||||
puts "#{upid}: Checking" if @debug
|
||||
instance_variable_get(upload_var(status)) if status
|
||||
end
|
||||
|
||||
def last_checked(upid)
|
||||
status = @counters[upid]
|
||||
instance_variable_get(checked_var(status)) if status
|
||||
end
|
||||
|
||||
def add(upid, size)
|
||||
stats = {'size' => size, 'received' => 0}
|
||||
@guard.synchronize do
|
||||
@counters[upid] = stats
|
||||
@counters[upid] = rand(Time.now.to_i).to_s.intern
|
||||
instance_variable_set(upload_var(@counters[upid]), {:size => size, :received => 0})
|
||||
puts "#{upid}: Added" if @debug
|
||||
end
|
||||
end
|
||||
|
||||
def mark(upid, len)
|
||||
upload = @counters[upid]
|
||||
recvd = upload['size'] - len
|
||||
@guard.synchronize do
|
||||
upload['received'] = recvd
|
||||
last_checked_time = last_checked(upid)
|
||||
if last_checked_time.nil? || Time.now-last_checked_time > 3
|
||||
status = check(upid)
|
||||
status[:received] = status[:size] - len
|
||||
instance_variable_set(checked_var(@counters[upid]), Time.now)
|
||||
puts "#{upid}: #{status[:received]}" if @debug
|
||||
end
|
||||
end
|
||||
|
||||
def finish(upid)
|
||||
upload = @counters[upid]
|
||||
recvd = upload['size']
|
||||
@guard.synchronize do
|
||||
upload['received'] = recvd
|
||||
puts "#{upid}: Finished" if @debug
|
||||
counter = @counters.delete(upid)
|
||||
return unless counter
|
||||
instance_variable_set(upload_var(counter), nil)
|
||||
instance_variable_set(checked_var(counter), nil)
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
def upload_var(key)
|
||||
"@upload_#{key}"
|
||||
end
|
||||
|
||||
def checked_var(key)
|
||||
"@checked_#{key}"
|
||||
end
|
||||
end
|
||||
|
||||
class Upload < GemPlugin::Plugin "/handlers"
|
||||
|
@ -43,25 +65,25 @@ class Upload < GemPlugin::Plugin "/handlers"
|
|||
def initialize(options = {})
|
||||
@path_info = options[:path_info]
|
||||
@request_notify = true
|
||||
Mongrel::Uploads.instance.instance_variable_set(:@debug, true) if options[:debug]
|
||||
end
|
||||
|
||||
def request_begins(params)
|
||||
upload_notify(:add, params, params[Mongrel::Const::CONTENT_LENGTH].to_i) if params['PATH_INFO'] == @path_info
|
||||
upload_notify(:add, params, params[Mongrel::Const::CONTENT_LENGTH].to_i)
|
||||
end
|
||||
|
||||
def request_progress(params, clen, total)
|
||||
upload_notify(:mark, params, clen) if params['PATH_INFO'] == @path_info
|
||||
upload_notify(:mark, params, clen)
|
||||
end
|
||||
|
||||
def process(request, response)
|
||||
upload_notify(:finish, request.params) if request.params['PATH_INFO'] == @path_info
|
||||
upload_notify(:finish, request.params)
|
||||
end
|
||||
|
||||
private
|
||||
def upload_notify(action, params, *args)
|
||||
upload_id = Mongrel::HttpRequest.query_parse(params['QUERY_STRING'])['upload_id']
|
||||
if params[Mongrel::Const::REQUEST_METHOD] == 'POST' && upload_id
|
||||
def upload_notify(action, params, *args)
|
||||
return unless params['PATH_INFO'] == @path_info && params[Mongrel::Const::REQUEST_METHOD] == 'POST'
|
||||
upload_id = Mongrel::HttpRequest.query_parse(params['QUERY_STRING'])['upload_id']
|
||||
Mongrel::Uploads.instance.send(action, upload_id, *args) if upload_id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue