Include MonitorMixin in store classes and make them thread-safe.

This commit is contained in:
Akinori MUSHA 2013-03-28 01:05:22 +09:00
parent 30e2915c1e
commit add4a367fd
3 changed files with 79 additions and 58 deletions

View file

@ -1,4 +1,8 @@
require 'monitor'
class HTTP::CookieJar::AbstractStore
include MonitorMixin
class << self
@@class_map = {}
@ -31,6 +35,7 @@ class HTTP::CookieJar::AbstractStore
private :default_options
def initialize(options = nil)
super() # MonitorMixin
options ||= {}
@logger = options[:logger]
# Initializes each instance variable of the same name as option
@ -65,7 +70,13 @@ class HTTP::CookieJar::AbstractStore
# If (and only if) the +uri+ option is given, last access time of
# each cookie is updated to the current time.
def each(uri = nil, &block)
if uri
raise
else
synchronize {
raise
}
end
self
end
include Enumerable

View file

@ -81,6 +81,7 @@ class HTTP::CookieJar
}
}
else
synchronize {
@jar.each { |domain, paths|
paths.each { |path, hash|
hash.delete_if { |name, cookie|
@ -93,6 +94,7 @@ class HTTP::CookieJar
}
}
}
}
end
self
end
@ -109,6 +111,10 @@ class HTTP::CookieJar
def cleanup(session = false)
now = Time.now
all_cookies = []
synchronize {
break if @gc_index == 0
@jar.each { |domain, paths|
domain_cookies = []
@ -148,6 +154,8 @@ class HTTP::CookieJar
}
@gc_index = 0
}
self
end
end
end

View file

@ -335,9 +335,6 @@ class HTTP::CookieJar
end
def cleanup(session = false)
now = Time.now
all_cookies = []
@st_delete_expired ||=
@db.prepare("DELETE FROM moz_cookies WHERE expiry < :expiry")
@ -364,7 +361,10 @@ class HTTP::CookieJar
)
SQL
@st_delete_expired.execute({ 'expiry' => now.to_i })
synchronize {
break if @gc_index == 0
@st_delete_expired.execute({ 'expiry' => Time.now.to_i })
@st_overusing_domains.execute({
'count' => HTTP::Cookie::MAX_COOKIES_PER_DOMAIN
@ -384,6 +384,8 @@ class HTTP::CookieJar
end
@gc_index = 0
}
self
end
end
end