wrest 1.5.2 → 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +4 -0
- data/README.md +23 -5
- data/lib/wrest.rb +2 -2
- data/lib/wrest/async_request.rb +13 -2
- data/lib/wrest/async_request/thread_backend.rb +15 -4
- data/lib/wrest/async_request/thread_pool.rb +65 -0
- data/lib/wrest/curl/request.rb +1 -1
- data/lib/wrest/native/request.rb +1 -1
- data/lib/wrest/version.rb +1 -1
- metadata +3 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af75d10ad96bd8e0539620594008b533d1d19a1a
|
4
|
+
data.tar.gz: 5760c9925189879d52eb6a83c599fcac4788f700
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e566458f7d00c59cda57695f0d8b55bc16d06d27e10d6980837584dbf184a7a74167e10644ba3b4c444d1073d0eaf8a9686cd51cda89a6825e2e13d4997a3edd
|
7
|
+
data.tar.gz: d1571b4cb17d98827dc391e616755d5e55d07c47da244fa09e5690c0dff1ac85a3f4480da7d46b856deecf598830363fbb48619bd3310fcdd6a952dfec45616e
|
data/CHANGELOG
CHANGED
@@ -2,6 +2,10 @@ Features under the section marked 'Current' are completed but pending release as
|
|
2
2
|
|
3
3
|
Features under a numbered section are complete and available in the Wrest gem.
|
4
4
|
|
5
|
+
== 1.5.3
|
6
|
+
* Implemented a thread pool for async requests using ThreadBackend
|
7
|
+
* Updated request/response logging to include current thread id
|
8
|
+
|
5
9
|
== 1.5.2
|
6
10
|
* Updated dependencies - ActiveSupport, MultipartPost, Dalli and JRuby OpenSSL
|
7
11
|
|
data/README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
[![Build Status](https://travis-ci.org/c42/wrest.svg?branch=master)](https://travis-ci.org/c42/wrest)
|
2
2
|
|
3
|
-
# Wrest 1.5.
|
3
|
+
# Wrest 1.5.3
|
4
4
|
|
5
|
-
(c) Copyright 2009-
|
5
|
+
(c) Copyright 2009-2015 [Sidu Ponnappa](http://twitter.com/ponnappa). All Rights Reserved.
|
6
6
|
|
7
7
|
Wrest is a ruby REST/HTTP client library which
|
8
8
|
|
9
9
|
* Allows you to use Net::HTTP or libCurl
|
10
|
-
* Allows you to pick your Ruby: use 2.x.x, JRuby 1.7.6 (and higher), JRuby 9.0.0.0.pre2
|
10
|
+
* Allows you to pick your Ruby: use 2.x.x, JRuby 1.7.6 (and higher), JRuby 9.0.0.0.pre2
|
11
11
|
* Supports RFC 2616 based [caching](https://github.com/kaiwren/wrest/blob/caching/Caching.markdown)
|
12
12
|
* **_Alpha_**
|
13
13
|
|
@@ -204,7 +204,7 @@ Please note that Wrest is a synchronous library. All requests are blocking, and
|
|
204
204
|
Asynchronous requests are non-blocking. They do not return a response and the request is executed on a separate thread. The only way to access the response
|
205
205
|
while using asynchronous request is through callbacks.
|
206
206
|
|
207
|
-
Asynchronous requests support pluggable backends. The default backend used for asynchronous requests is ruby threads.
|
207
|
+
Asynchronous requests support pluggable backends. The default backend used for asynchronous requests is ruby threads, which is only reliable when using JRuby.
|
208
208
|
|
209
209
|
```
|
210
210
|
"http://c42.in".to_uri.get_async do |callback|
|
@@ -213,7 +213,8 @@ Asynchronous requests support pluggable backends. The default backend used for a
|
|
213
213
|
end
|
214
214
|
end
|
215
215
|
|
216
|
-
|
216
|
+
# Wait until the background threads finish execution before letting the program end.
|
217
|
+
Wrest::AsyncRequest.wait_for_thread_pool!
|
217
218
|
```
|
218
219
|
|
219
220
|
You can change the default to eventmachine.
|
@@ -279,6 +280,23 @@ The Wrest logger can be set and accessed through Wrest.logger and is configured
|
|
279
280
|
Wrest.logger = Rails.logger
|
280
281
|
```
|
281
282
|
|
283
|
+
Every request and response is logged at level `debug`.
|
284
|
+
|
285
|
+
Here is an sample request log message:
|
286
|
+
```
|
287
|
+
<- (POST 515036017 732688777 2010) http://localhost:3000/events.json
|
288
|
+
```
|
289
|
+
|
290
|
+
The request log consists of request type (POST), request hash (515036017), connection hash (732688777), thread id (2010), URI (http://localhost:3000/events.json)
|
291
|
+
|
292
|
+
Here is a sample response log message:
|
293
|
+
```
|
294
|
+
-> (POST 515036017 732688777 2010) 200 OK (0 bytes 0.01s)
|
295
|
+
```
|
296
|
+
The response log consists of request type that generated the response (POST), hash of the request that generated the response (515036017), hash of the connection (732688777), thread id (2010), status (200 OK), response body length (0 bytes) and time taken (0.01)s.
|
297
|
+
|
298
|
+
The thread id, request hash and connection hashes are used to track requests and their corresponding responses when using asynchronous requests and/or http connection pooling.
|
299
|
+
|
282
300
|
### Json Backend
|
283
301
|
|
284
302
|
Wrest uses the multi_json gem to manage Json backends, allowing it to play nice with Rails 3.1. To change the backend used, you can do the following:
|
data/lib/wrest.rb
CHANGED
@@ -67,9 +67,9 @@ require "wrest/http_codes"
|
|
67
67
|
require "wrest/callback"
|
68
68
|
require "wrest/native"
|
69
69
|
|
70
|
-
require "wrest/async_request"
|
70
|
+
require "wrest/async_request/thread_pool"
|
71
71
|
require "wrest/async_request/thread_backend"
|
72
|
-
|
72
|
+
require "wrest/async_request"
|
73
73
|
|
74
74
|
require "wrest/caching"
|
75
75
|
|
data/lib/wrest/async_request.rb
CHANGED
@@ -31,9 +31,20 @@ module Wrest
|
|
31
31
|
self.default_backend = Wrest::AsyncRequest::ThreadBackend.new
|
32
32
|
end
|
33
33
|
|
34
|
-
# Returns the default backend
|
34
|
+
# Returns the default backend, which is the ThreadBackend
|
35
35
|
def self.default_backend
|
36
|
-
@default_backend
|
36
|
+
@default_backend || default_to_threads!
|
37
|
+
end
|
38
|
+
|
39
|
+
# Uses Thread#join to wait until all background requests
|
40
|
+
# are completed.
|
41
|
+
#
|
42
|
+
# Use this as the last instruction in a script to prevent it from
|
43
|
+
# exiting before background threads have completed running.
|
44
|
+
#
|
45
|
+
# Needs Wrest.default_backend to be an instance of ThreadBackend.
|
46
|
+
def self.wait_for_thread_pool!
|
47
|
+
default_backend.wait_for_thread_pool!
|
37
48
|
end
|
38
49
|
end
|
39
50
|
end
|
@@ -9,11 +9,22 @@
|
|
9
9
|
|
10
10
|
module Wrest
|
11
11
|
module AsyncRequest
|
12
|
-
|
12
|
+
# Uses a pool of Threads to make requests.
|
13
|
+
# Only recommended for production use on JRuby.
|
14
|
+
class ThreadBackend
|
15
|
+
attr_reader :thread_pool
|
16
|
+
def initialize(number_of_threads = 5)
|
17
|
+
@thread_pool = ThreadPool.new(number_of_threads)
|
18
|
+
end
|
19
|
+
|
13
20
|
def execute(request)
|
14
|
-
|
15
|
-
|
16
|
-
|
21
|
+
@thread_pool.execute_eventually(request)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Uses Thread#join to wait until all
|
25
|
+
# background requests are completed.
|
26
|
+
def wait_for_thread_pool!
|
27
|
+
@thread_pool.join_pool_threads!
|
17
28
|
end
|
18
29
|
end
|
19
30
|
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# Copyright 2009-2015 Sidu Ponnappa
|
2
|
+
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at native://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
# Unless required by applicable law or agreed to in writing, software distributed under the License
|
7
|
+
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
8
|
+
# See the License for the specific language governing permissions and limitations under the License.
|
9
|
+
|
10
|
+
module Wrest
|
11
|
+
module AsyncRequest
|
12
|
+
class ThreadPool
|
13
|
+
def initialize(number_of_threads)
|
14
|
+
@threads = []
|
15
|
+
@number_of_threads = number_of_threads
|
16
|
+
@queue = Queue.new
|
17
|
+
halt_on_sigint
|
18
|
+
halt_on_int
|
19
|
+
initialize_thread_pool
|
20
|
+
end
|
21
|
+
|
22
|
+
def execute_eventually(request)
|
23
|
+
@queue.push(request)
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def join_pool_threads!
|
28
|
+
@threads.each do |thread|
|
29
|
+
thread.join if thread.alive?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def initialize_thread_pool
|
35
|
+
main_thread = Thread.current
|
36
|
+
@threads = @number_of_threads.times.map do |i|
|
37
|
+
Thread.new do |thread|
|
38
|
+
while request = @queue.pop
|
39
|
+
request.invoke
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def halt_on_sigint
|
46
|
+
trap('SIGINT') do
|
47
|
+
halt
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def halt_on_int
|
52
|
+
trap('INT') do
|
53
|
+
halt
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def halt
|
58
|
+
Wrest.logger.debug "Shutting down ThreadPool..."
|
59
|
+
@threads.each(&:terminate)
|
60
|
+
Wrest.logger.debug "Halted ThreadPool, continuing with shutdown..."
|
61
|
+
Process.exit
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/wrest/curl/request.rb
CHANGED
@@ -87,7 +87,7 @@ module Wrest
|
|
87
87
|
@connection ||= Patron::Session.new
|
88
88
|
raise ArgumentError, "Empty URL" if http_request.url.empty?
|
89
89
|
|
90
|
-
prefix = "#{http_request.action.to_s.upcase} #{http_request.hash} #{connection.hash}"
|
90
|
+
prefix = "#{http_request.action.to_s.upcase} #{http_request.hash} #{connection.hash} #{Thread.current.object_id}"
|
91
91
|
|
92
92
|
Wrest.logger.debug "<- (#{prefix}) #{http_request.url}"
|
93
93
|
time = Benchmark.realtime { response = Wrest::Curl::Response.new(connection.handle_request(http_request))}
|
data/lib/wrest/native/request.rb
CHANGED
@@ -88,7 +88,7 @@ module Wrest::Native
|
|
88
88
|
@connection.set_debug_output @detailed_http_logging
|
89
89
|
http_request.basic_auth username, password unless username.nil? || password.nil?
|
90
90
|
|
91
|
-
prefix = "#{http_request.method} #{self.hash} #{@connection.hash}"
|
91
|
+
prefix = "#{http_request.method} #{self.hash} #{@connection.hash} #{Thread.current.object_id}"
|
92
92
|
|
93
93
|
Wrest.logger.debug "<- (#{prefix}) #{@uri.protocol}://#{@uri.host}:#{@uri.port}#{@http_request.path}"
|
94
94
|
time = Benchmark.realtime { response = Wrest::Native::Response.new( do_request ) }
|
data/lib/wrest/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wrest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sidu Ponnappa
|
@@ -9,22 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-08-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: rubyforge
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
requirements:
|
18
|
-
- - ">="
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: '0'
|
21
|
-
type: :development
|
22
|
-
prerelease: false
|
23
|
-
version_requirements: !ruby/object:Gem::Requirement
|
24
|
-
requirements:
|
25
|
-
- - ">="
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
version: '0'
|
28
14
|
- !ruby/object:Gem::Dependency
|
29
15
|
name: rspec
|
30
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -129,6 +115,7 @@ files:
|
|
129
115
|
- lib/wrest/async_request.rb
|
130
116
|
- lib/wrest/async_request/event_machine_backend.rb
|
131
117
|
- lib/wrest/async_request/thread_backend.rb
|
118
|
+
- lib/wrest/async_request/thread_pool.rb
|
132
119
|
- lib/wrest/cache_proxy.rb
|
133
120
|
- lib/wrest/caching.rb
|
134
121
|
- lib/wrest/caching/memcached.rb
|