Comments (12)
jtmarmon@2ada527 Here's a change I made which seems harmless, and also seems to work fine, but would appreciate input on why this decision was made
from podio-rb.
It's an attempt of making the client thread-safe and avoid leaking the current client to other threads. What are the problems you're seeing?
from podio-rb.
@theflow - it's related to #15 . using this in a rails environment where (depending on your server) you have a separate thread or process handling the request means that Podio.client
is nil on every request, so we have to make an HTTP request to podio to re-initialize the connection
from podio-rb.
At Podio, part of our application stack is a rails component that leverages the podio-rb library for communication with the API. We have an initializer that establishes the connection and a before_filter
in the application_controller.rb
which invokes an init method found in the initializer.
from podio-rb.
@cpeters we have similar code which does
if Podio.client.nil?
Podio.setup(...)
Podio.client.authenticate_with_app(..)
end
but because the client is nil on every request, we're sending an HTTP request to podio for every client request. am I missing something here that would prevent that?
from podio-rb.
Where is this code located and then executed? I'm curious about the conditional statement. Let me dig into the code because this may not actually return nil, in the ruby sense.
def init_podio(username, password) Podio.client = Podio::Client.new(PODIO_CLIENT_OPTIONS) Podio.client.authenticate_with_credentials(username, password) end
That is our client init.
from podio-rb.
The difference is if you use username/password or initialize the client with a stored token. I'll do some more research but I think we can move the client to use instance variables.
from podio-rb.
I'm missing something here. How does moving to instance variables solve the problem? Each thread will still have to authenticate with an API call.
from podio-rb.
@cpeters it's actually not something I understand well and am doing some more research to try and understand, but my fork of podio-rb definitely fixes the issue
from podio-rb.
think I figured out why:
class Foo
class << self
@@foo = 0
def bar
@@foo = 5
end
def baz
@@foo
end
end
end
puts Foo.baz
Foo.bar
puts Foo.baz
Thread.new { puts Foo.baz }.join
this prints:
0
5
5
It seems that ruby class variables are both thread safe and globally available
from podio-rb.
scratch that, they are not thread safe. but they are available across threads
from podio-rb.
@jtmarmon I think we need to use a mutex.
class Foo
@mutex = Mutex.new
def self.bar
@mutex.synchronize {
@bar ||= create_no
}
end
def self.create_no
no = rand(10000)
sleep 1
no
end
end
http://stackoverflow.com/questions/9558192/thread-safety-class-variables-in-ruby
from podio-rb.
Related Issues (20)
- Tag versions HOT 1
- Unable to run example HOT 2
- Rails 4 support HOT 4
- NoMethodError (undefined method `post' for nil:NilClass) HOT 7
- getting bitten by uninitialized constant ActiveSupport::Autoload HOT 2
- faraday 0.9.0? HOT 2
- saving items with calculation fields returns an error HOT 3
- ItemDiff.revert used inside hook causes infinite loop HOT 2
- Ruby 2.1 & 2.2, Getting: Faraday::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed HOT 2
- error on adding podio gem to project HOT 2
- Versioning and Tagging? HOT 6
- NoMethodError (undefined method `[]' for nil:NilClass)
- Using this gem in a multi-user system HOT 1
- Rate limit information HOT 3
- External id not returned by "Get item revision difference" HOT 9
- Podio API & gem still alive? HOT 1
- "Active Podio" & custom attributes HOT 5
- Outdated Faraday dependency HOT 6
- Typo, please delete
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from podio-rb.