Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Concurrent batching support for client
Browse files Browse the repository at this point in the history
  • Loading branch information
kyledrake committed Apr 17, 2013
1 parent a97d521 commit 7c5007f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ coinbase.get('/account/balance').to_hash

Or feel free to add a new wrapper method and submit a pull request.

## Batching

The client can be batched to run multiple requests concurrently. If you have to make a lot of API calls (for example, to get balances for a lot of users simultaneously), this will improve performance significantly. Under the hood, it uses internal threading to take advantage of Ruby's non-blocking IO model (one IO request per thread):

```ruby
buy_price, sell_price = coinbase.batch do |client|
client.buy_price 1
client.sell_price 1
end

buy_price.inspect # => #<Money fractional:1384 currency:USD>
```

## Security Notes

If someone gains access to your API Key they will have complete control of your Coinbase account. This includes the abillity to send all of your bitcoins elsewhere.
Expand Down
1 change: 1 addition & 0 deletions lib/coinbase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
require "coinbase/version"
require "coinbase/money"
require "coinbase/client"
require "coinbase/batch"
42 changes: 42 additions & 0 deletions lib/coinbase/batch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Coinbase
class Batch
MAXIMUM_CONCURRENT_REQUESTS = 10

def initialize(client, opts={}, &block)
@client = client
@commands = []
@opts = opts

instance_eval &block
end

def run
responses = []

until @commands.empty?
threads = []
commands = []

(@opts[:maximum_concurrent_requests] || MAXIMUM_CONCURRENT_REQUESTS).times do
break if @commands.empty?
commands << @commands.shift
end

commands.each do |c|
threads << Thread.new {
Thread.current[:resp] = @client.send c[0], *c[1]
}
end

threads.each {|t| t.join}
threads.each {|t| responses << t[:resp]}
end

responses
end

def method_missing(meth, args=[])
@commands << [meth, args]
end
end
end
6 changes: 6 additions & 0 deletions lib/coinbase/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ def sell! qty
r
end

# Batching

def batch(&block)
Batch.new(self, &block).run
end

# Wrappers for the main HTTP verbs

def get(path, options={})
Expand Down
25 changes: 25 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,31 @@
r.transfer.btc.should == 1.to_money
end

it "should batch multiple requests" do
buy_api_return = {"amount"=>"13.84", "currency"=>"USD"}
sell_api_return = {"amount"=>"15.00", "currency"=>"CAD"}
fake :get, "/prices/buy", buy_api_return
fake :get, "/prices/sell", sell_api_return

buy_price, sell_price = @c.batch do |c|
c.buy_price 1
c.sell_price 1
end

buy_price.to_f.should == 13.84
sell_price.to_f.should == 15.00

# Check to make sure the thread throttle doesn't cause an error

prices = @c.batch do |c|
(Coinbase::Batch::MAXIMUM_CONCURRENT_REQUESTS).times { c.buy_price(1); c.sell_price(1) }
end

prices.length.should == 20

prices.first.to_f.should == 13.84
prices.last.to_f.should == 15.00
end

private

Expand Down

0 comments on commit 7c5007f

Please sign in to comment.