Skip to content

Commit

Permalink
Made number of threads in thread pool configurable
Browse files Browse the repository at this point in the history
Changed thread pool to create threads only on first use
  • Loading branch information
kaiwren committed Aug 5, 2015
1 parent 5e2f52e commit 63d1c7f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ Features under the section marked 'Current' are completed but pending release as

Features under a numbered section are complete and available in the Wrest gem.

== 1.5.4
* Make thread pool configurable

== 1.5.3
* Implemented a thread pool for async requests using ThreadBackend
* Updated request/response logging to include current thread id
Expand Down
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Build Status](https://travis-ci.org/c42/wrest.svg?branch=master)](https://travis-ci.org/c42/wrest)

# Wrest 1.5.3
# Wrest 1.5.4

(c) Copyright 2009-2015 [Sidu Ponnappa](http://twitter.com/ponnappa). All Rights Reserved.

Expand All @@ -9,10 +9,7 @@ Wrest is a ruby REST/HTTP client library which
* Allows you to use Net::HTTP or libCurl
* Allows you to pick your Ruby: use 2.x.x, JRuby 1.7.6 (and higher), JRuby 9.0.0.0.pre2
* Supports RFC 2616 based [caching](https://github.com/kaiwren/wrest/blob/caching/Caching.markdown)
* **_Alpha_**

Allows you to go async: use Threads or EventMachine

* Async http calls using Threads (reliable only on JRuby) or EventMachine
* Allows you to quickly build object oriented wrappers around any web service
* Is designed to be used as a library, not just a command line REST client (fewer class/static methods, more object oriented)
* Is spec driven, strongly favours immutable objects and avoids class methods and setters making it better suited for use as a library, especially in multi-threaded environments
Expand All @@ -31,15 +28,16 @@ For Facebook, Twitter, Delicious, GitHub and other API examples, see http://gith
* Basic API calls

```
'http://twitter.com/statuses/public_timeline.json'.to_uri.get.deserialise # works with json and xml out of the box
'http://twitter.com/statuses/public_timeline.xml'.to_uri.get.deserialise # see lib/wrest/components/translators to add other formats
# Works with json and xml out of the box
# See lib/wrest/components/translators to add other formats
'https://api.github.com/repos/c42/wrest/issues'.to_uri.get.deserialize
```

* Timeout support

```
'http://twitter.com/statuses/public_timeline.json'.to_uri.get(:timeout => 5).body
'https://api.github.com/repos/c42/wrest/issues'.to_uri.get(:timeout => 5).body
```

* Redirect support
Expand Down Expand Up @@ -217,11 +215,15 @@ Asynchronous requests support pluggable backends. The default backend used for a
Wrest::AsyncRequest.wait_for_thread_pool!
```

You can change the default to eventmachine.
You can change the default to eventmachine or to threads.

```
Wrest::AsyncRequest.default_to_em!
```
or
```
Wrest::AsyncRequest.default_to_threads!
```

You can also override the default on Uri objects.

Expand All @@ -233,7 +235,16 @@ You can also override the default on Uri objects.
end
```

Note: The current implementation of asynchronous requests is currently in alpha and should not be used in production.
You can decide which AsyncBackend to use at runtime through to `to_uri`'s options hash.

```
"http://c42.in".to_uri(asynchronous_backend: ThreadBackend.new(number_of_threads)).get_async do |callback|
callback.on_ok do |response|
Wrest.logger.info "Ok."
end
end
```


### Other useful stuff

Expand Down
4 changes: 2 additions & 2 deletions lib/wrest/async_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def self.default_to_em!
end

# Assign default backend for asynchronous request to using threads.
def self.default_to_threads!
self.default_backend = Wrest::AsyncRequest::ThreadBackend.new
def self.default_to_threads!(number_of_threads = 5)
self.default_backend = Wrest::AsyncRequest::ThreadBackend.new(number_of_threads)
end

# Returns the default backend, which is the ThreadBackend
Expand Down
14 changes: 8 additions & 6 deletions lib/wrest/async_request/thread_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ def initialize(number_of_threads)
@threads = []
@number_of_threads = number_of_threads
@queue = Queue.new
halt_on_sigint
halt_on_int
initialize_thread_pool
end

def execute_eventually(request)
initialize_thread_pool if @threads.empty?
@queue.push(request)
nil
end
Expand All @@ -32,6 +30,8 @@ def join_pool_threads!

private
def initialize_thread_pool
halt_on_sigint
halt_on_int
main_thread = Thread.current
@threads = @number_of_threads.times.map do |i|
Thread.new do |thread|
Expand All @@ -55,9 +55,11 @@ def halt_on_int
end

def halt
Wrest.logger.debug "Shutting down ThreadPool..."
@threads.each(&:terminate)
Wrest.logger.debug "Halted ThreadPool, continuing with shutdown..."
unless @threads.empty?
Wrest.logger.debug "Wrest: Shutting down ThreadPool..."
@threads.each(&:terminate)
Wrest.logger.debug "Wrest: Halted ThreadPool, continuing with shutdown."
end
Process.exit
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/wrest/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
# See the License for the specific language governing permissions and limitations under the License.

module Wrest
VERSION = "1.5.3"
VERSION = "1.5.4"
end

0 comments on commit 63d1c7f

Please sign in to comment.