Skip to content

Instantly share code, notes, and snippets.

@rossta
Created January 8, 2013 03:37
Show Gist options
  • Save rossta/4480968 to your computer and use it in GitHub Desktop.
Save rossta/4480968 to your computer and use it in GitHub Desktop.
Ruby-trello and Threads: Making requests on behalf of multiple clients is not currently thread-safe, as demonstrated in the gist below (with placeholders for sensitive data).
include Trello
include Trello::Authorization
Trello::Authorization.const_set :AuthPolicy, OAuthPolicy
OAuthPolicy.consumer_credential = OAuthCredential.new('user_key', 'user_secret')
threads = []
tokens = %w[ not_rosstas_token rosstas_token ]
tokens.each_with_index do |token, i|
threads << Thread.new(token) do |access_token|
OAuthPolicy.token = OAuthCredential.new(access_token)
sleep 1
member = Trello::Member.find('rossta')
puts "Thread #{i + 1}: #{member.inspect}"
puts "Thread #{i + 1}: #{member.boards.map(&:name).inspect}"
end
end
threads.each { |thread| thread.join }
# The oauth token for the first thread can be overwritten by the second thread, leading to unexpected results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment