Giter Site home page Giter Site logo

Why thread locals? about podio-rb HOT 12 OPEN

podio avatar podio commented on August 9, 2024
Why thread locals?

from podio-rb.

Comments (12)

jtmarmon avatar jtmarmon commented on August 9, 2024

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.

theflow avatar theflow commented on August 9, 2024

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.

jtmarmon avatar jtmarmon commented on August 9, 2024

@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.

cpeters avatar cpeters commented on August 9, 2024

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.

jtmarmon avatar jtmarmon commented on August 9, 2024

@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.

cpeters avatar cpeters commented on August 9, 2024

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.

theflow avatar theflow commented on August 9, 2024

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.

cpeters avatar cpeters commented on August 9, 2024

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.

jtmarmon avatar jtmarmon commented on August 9, 2024

@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.

jtmarmon avatar jtmarmon commented on August 9, 2024

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.

jtmarmon avatar jtmarmon commented on August 9, 2024

scratch that, they are not thread safe. but they are available across threads

from podio-rb.

cpeters avatar cpeters commented on August 9, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.