Giter VIP home page Giter VIP logo

Comments (4)

mquan avatar mquan commented on May 30, 2024 1

What you can do is to patch the subscriber and make it ignore queries from non-primary connection.

Do you think we still want to catch violations inside non-primary db transaction?

We should track connections separately using their ids.

It sounds like this statement means we track isolator_transactions per connection? This makes sense to me. Although I think the remaining challenge is to identify and ignore sql of connection establishment. May be we can use it as the trigger for initializing a new isolator_transactions.
Do you think using the db_config's host, adapter, and database would be a good connection identifier?

As for repro example

# This one is using the main db connection
class User < ActiveRecord::Base; end

# this is a different db that connects lazily
class Foo < ActiveRecord::Base
  self.establish_connection db_config
end

# simply reference the class would increment the count

# Thread.current[:isolator_transactions] == 0
User.transaction do
  # Thread.current[:isolator_transactions] == 1
  Foo
  # Thread.current[:isolator_transactions] == 2
end
# Thread.current[:isolator_transactions] == 1

I haven't tried but base on your comment may be the following is enough to throw off the count

User.transaction do
  # Would `Thread.current[:isolator_transactions]` still increment even if the adapter and database are the same as the main connection?
  ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
end

from isolator.

palkan avatar palkan commented on May 30, 2024 1

Hey @mquan!

Finally, I found some time to invest into this feature. I took your PR and added some changes: #32

It would be great if you try this branch in your project (we're currently testing it internally, too).

from isolator.

mquan avatar mquan commented on May 30, 2024 1

@palkan awesome, I tested it out in our repo, and it fixes the problem. I also added a few offenses in both main and secondary dbs and it correctly identifies them. Great job 👍

from isolator.

palkan avatar palkan commented on May 30, 2024

Do you have a recommended way for dealing with multiple connections?

Unfortunately, no. Isolator was designed for single connection per thread usage, and it's not that simple to make it work with multiple connections.

What you can do is to patch the subscriber and make it ignore queries from non-primary connection. Smth like this:

def self.subscribe!(event)
  ::ActiveSupport::Notifications.subscribe(event) do |_name, _start, _finish, _id, query|
    # not sure about this one
    next unless query[:connection] == ActiveRecord::Base.connection

    Isolator.incr_transactions! if START_PATTERN.match?(query[:sql])
    Isolator.decr_transactions! if FINISH_PATTERN.match?(query[:sql])
  end
end

We should track connections separately using their ids. That would require some work.

Could you please provide a reproduction example or, much better, open a PR to Isolator with a test scenario for multiple connections? That would help us to fix this quicker.

from isolator.

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.