glebtv-httpclient 3.2.0 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +3 -2
- data/Gemfile.lock +4 -4
- data/httpclient.gemspec +1 -1
- data/lib/httpclient.rb +1 -2
- data/lib/httpclient/lru_cache.rb +13 -107
- data/lib/httpclient/version.rb +1 -1
- metadata +3 -4
- data/lib/httpclient/lru_threadsafe.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf53dd7d1fb84aafaf0d492a43fea2120759dab6
|
4
|
+
data.tar.gz: e57fc09b5ff8aecaffe1b6691175c4fdadb50e33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 007324756e3fc82d18487b249df8568963b1b783d9af76179db92bc75be378672dd1afde44cce40a2a7175945ac403c889d3855fb1c63ac97f087005a729b489
|
7
|
+
data.tar.gz: fbcccac32b648db73853951458782f978e17e3094060740f4fb9d93fbd437cdd751afa300fa0cf871143fd87e9842a908910121b6a057c10b6108c0fe5d58404
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
glebtv-httpclient (3.2.
|
5
|
-
PriorityQueue
|
4
|
+
glebtv-httpclient (3.2.1)
|
6
5
|
activesupport
|
7
6
|
addressable
|
7
|
+
lru_redux
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: http://rubygems.org/
|
11
11
|
specs:
|
12
|
-
PriorityQueue (0.1.2)
|
13
12
|
activesupport (4.0.2)
|
14
13
|
i18n (~> 0.6, >= 0.6.4)
|
15
14
|
minitest (~> 4.2)
|
@@ -29,6 +28,7 @@ GEM
|
|
29
28
|
ffi2-generators (0.1.1)
|
30
29
|
i18n (0.6.9)
|
31
30
|
json (1.8.1)
|
31
|
+
lru_redux (0.8.1)
|
32
32
|
mime-types (2.1)
|
33
33
|
minitest (4.7.5)
|
34
34
|
multi_json (1.8.4)
|
@@ -46,7 +46,7 @@ GEM
|
|
46
46
|
diff-lcs (>= 1.1.3, < 2.0)
|
47
47
|
rspec-mocks (2.14.5)
|
48
48
|
rubinius-coverage (2.0.3)
|
49
|
-
rubinius-debugger (2.0.
|
49
|
+
rubinius-debugger (2.0.1)
|
50
50
|
rubinius-developer_tools (2.0.0)
|
51
51
|
rubinius-coverage (~> 2.0)
|
52
52
|
rubinius-debugger (~> 2.0)
|
data/httpclient.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.require_paths = ["lib"]
|
22
22
|
|
23
23
|
spec.add_dependency "activesupport"
|
24
|
-
spec.add_dependency "
|
24
|
+
spec.add_dependency "lru_redux"
|
25
25
|
spec.add_dependency "addressable"
|
26
26
|
|
27
27
|
spec.add_development_dependency "bundler"
|
data/lib/httpclient.rb
CHANGED
@@ -21,7 +21,6 @@ require 'httpclient/cookie'
|
|
21
21
|
require 'httpclient/cookie'
|
22
22
|
|
23
23
|
require 'httpclient/lru_cache'
|
24
|
-
require 'httpclient/lru_threadsafe'
|
25
24
|
|
26
25
|
require 'active_support/core_ext'
|
27
26
|
|
@@ -235,7 +234,7 @@ require 'active_support/core_ext'
|
|
235
234
|
# ruby -rhttpclient -e 'p HTTPClient.head(ARGV.shift).header["last-modified"]' http://dev.ctor.org/
|
236
235
|
#
|
237
236
|
class HTTPClient
|
238
|
-
@@dns_cache = HTTPClient::
|
237
|
+
@@dns_cache = HTTPClient::LRUCache.new(ttl: 20.minutes, soft_ttl: 10.minute, retry_delay: 5.minutes)
|
239
238
|
cattr_accessor :dns_cache
|
240
239
|
|
241
240
|
def own_methods
|
data/lib/httpclient/lru_cache.rb
CHANGED
@@ -3,18 +3,16 @@
|
|
3
3
|
# Taken from: https://github.com/kindkid/lrucache/blob/master/lib/lrucache.rb
|
4
4
|
# (MIT Licensed). Thanks!
|
5
5
|
|
6
|
-
require "
|
6
|
+
require "lru_redux"
|
7
7
|
|
8
8
|
# Not thread-safe!
|
9
9
|
class HTTPClient
|
10
10
|
class LRUCache
|
11
11
|
|
12
|
-
attr_reader :
|
12
|
+
attr_reader :max_size, :ttl, :soft_ttl, :retry_delay
|
13
13
|
|
14
14
|
def initialize(opts={})
|
15
15
|
@max_size = Integer(opts[:max_size] || 100)
|
16
|
-
@default = opts[:default]
|
17
|
-
@eviction_handler = opts[:eviction_handler]
|
18
16
|
@ttl = Float(opts[:ttl] || 0)
|
19
17
|
@soft_ttl = Float(opts[:soft_ttl] || 0)
|
20
18
|
@retry_delay = Float(opts[:retry_delay] || 0)
|
@@ -23,96 +21,39 @@ class HTTPClient
|
|
23
21
|
raise "soft_ttl must not be negative" if @soft_ttl < 0
|
24
22
|
raise "retry_delay must not be negative" if @retry_delay < 0
|
25
23
|
|
26
|
-
@
|
27
|
-
@data = {}
|
28
|
-
@counter = 0
|
24
|
+
@data = LruRedux::Cache.new(@max_size)
|
29
25
|
end
|
30
26
|
|
31
|
-
def
|
32
|
-
@
|
33
|
-
|
34
|
-
@counter = 0 #might as well
|
35
|
-
end
|
36
|
-
|
37
|
-
def include?(key)
|
38
|
-
datum = @data[key]
|
39
|
-
return false if datum.nil?
|
40
|
-
if datum.expired?
|
41
|
-
delete(key)
|
42
|
-
false
|
43
|
-
else
|
44
|
-
access(key)
|
45
|
-
true
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def store(key, value, args={})
|
50
|
-
evict_lru! unless @data.include?(key) || @data.size < max_size
|
51
|
-
ttl, soft_ttl, retry_delay = extract_arguments(args)
|
52
|
-
expiration = expiration_date(ttl)
|
53
|
-
soft_expiration = expiration_date(soft_ttl)
|
27
|
+
def store(key, value)
|
28
|
+
expiration = Time.now + @ttl
|
29
|
+
soft_expiration = Time.now + @soft_ttl
|
54
30
|
@data[key] = Datum.new(value, expiration, soft_expiration)
|
55
|
-
access(key)
|
56
31
|
value
|
57
32
|
end
|
58
33
|
|
59
34
|
alias :[]= :store
|
60
35
|
|
61
|
-
def fetch(key
|
36
|
+
def fetch(key)
|
62
37
|
datum = @data[key]
|
63
38
|
if datum.nil?
|
64
|
-
|
65
|
-
store(key, value = yield, args)
|
66
|
-
else
|
67
|
-
@default
|
68
|
-
end
|
39
|
+
store(key, value = yield)
|
69
40
|
elsif datum.expired?
|
70
41
|
delete(key)
|
71
|
-
|
72
|
-
store(key, value = yield, args)
|
73
|
-
else
|
74
|
-
@default
|
75
|
-
end
|
42
|
+
store(key, value = yield)
|
76
43
|
elsif datum.soft_expired?
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
access(key)
|
82
|
-
ttl, soft_ttl, retry_delay = extract_arguments(args)
|
83
|
-
datum.soft_expiration = (Time.now + retry_delay) if retry_delay > 0
|
84
|
-
datum.value
|
85
|
-
end
|
86
|
-
else
|
87
|
-
access(key)
|
44
|
+
begin
|
45
|
+
store(key, value = yield)
|
46
|
+
rescue RuntimeError => e
|
47
|
+
datum.soft_expiration = (Time.now + retry_delay) if retry_delay > 0
|
88
48
|
datum.value
|
89
49
|
end
|
90
50
|
else
|
91
|
-
access(key)
|
92
51
|
datum.value
|
93
52
|
end
|
94
53
|
end
|
95
54
|
|
96
55
|
alias :[] :fetch
|
97
56
|
|
98
|
-
def empty?
|
99
|
-
size == 0
|
100
|
-
end
|
101
|
-
|
102
|
-
def size
|
103
|
-
@data.size
|
104
|
-
end
|
105
|
-
|
106
|
-
def keys
|
107
|
-
@data.keys
|
108
|
-
end
|
109
|
-
|
110
|
-
def delete(key)
|
111
|
-
@pqueue.delete(key)
|
112
|
-
datum = @data.delete(key)
|
113
|
-
datum.value unless datum.nil?
|
114
|
-
end
|
115
|
-
|
116
57
|
private
|
117
58
|
|
118
59
|
class Datum
|
@@ -132,40 +73,5 @@ class HTTPClient
|
|
132
73
|
!@soft_expiration.nil? && @soft_expiration <= Time.now
|
133
74
|
end
|
134
75
|
end
|
135
|
-
|
136
|
-
def expiration_date(ttl)
|
137
|
-
if ttl.is_a?(Time)
|
138
|
-
ttl
|
139
|
-
else
|
140
|
-
ttl = Float(ttl)
|
141
|
-
(ttl > 0) ? (Time.now + ttl) : nil
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
def extract_arguments(args)
|
146
|
-
if args.is_a?(Hash)
|
147
|
-
ttl = args[:ttl] || @ttl
|
148
|
-
soft_ttl = args[:soft_ttl] || @soft_ttl
|
149
|
-
retry_delay = args[:retry_delay] || @retry_delay
|
150
|
-
[ttl, soft_ttl, retry_delay]
|
151
|
-
else
|
152
|
-
# legacy arg
|
153
|
-
ttl = args || @ttl
|
154
|
-
[ttl, @soft_ttl, @retry_delay]
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
def evict_lru!
|
159
|
-
key, priority = @pqueue.delete_min
|
160
|
-
unless priority.nil?
|
161
|
-
datum = @data.delete(key)
|
162
|
-
@eviction_handler.call(datum.value) if @eviction_handler && datum
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
def access(key)
|
167
|
-
@pqueue.change_priority(key, @counter += 1)
|
168
|
-
end
|
169
|
-
|
170
76
|
end
|
171
77
|
end
|
data/lib/httpclient/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: glebtv-httpclient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.2.
|
4
|
+
version: 3.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- glebtv
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
|
-
name:
|
29
|
+
name: lru_redux
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
32
|
- - '>='
|
@@ -169,7 +169,6 @@ files:
|
|
169
169
|
- lib/httpclient/http.rb
|
170
170
|
- lib/httpclient/include_client.rb
|
171
171
|
- lib/httpclient/lru_cache.rb
|
172
|
-
- lib/httpclient/lru_threadsafe.rb
|
173
172
|
- lib/httpclient/session.rb
|
174
173
|
- lib/httpclient/ssl_config.rb
|
175
174
|
- lib/httpclient/util.rb
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# Make LRU cache Threadsafe
|
2
|
-
# Idea from: https://github.com/SamSaffron/lru_redux/blob/master/lib/lru_redux/thread_safe_cache.rb
|
3
|
-
# (MIT License)
|
4
|
-
|
5
|
-
require 'thread'
|
6
|
-
require 'monitor'
|
7
|
-
|
8
|
-
class HTTPClient::ThreadSafeCache < HTTPClient::LRUCache
|
9
|
-
include MonitorMixin
|
10
|
-
|
11
|
-
def initialize(opts={})
|
12
|
-
super(opts)
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.synchronize(*methods)
|
16
|
-
methods.each do |method|
|
17
|
-
define_method method do |*args, &blk|
|
18
|
-
synchronize do
|
19
|
-
super(*args, &blk)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
alias :[]= :store
|
25
|
-
alias :[] :fetch
|
26
|
-
end
|
27
|
-
|
28
|
-
synchronize :fetch, :store, :delete, :clear, :include?, :empty, :size, :keys
|
29
|
-
end
|