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

View file

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

View file

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