Sha256: 8b7166412ca89a5afe35af0b2d657a3e8f00712e8a64fc5bbab127853057884e

Contents?: true

Size: 1.98 KB

Versions: 42

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module Middleware
    # = Shard Selector \Middleware
    #
    # The ShardSelector Middleware provides a framework for automatically
    # swapping shards. \Rails provides a basic framework to determine which
    # shard to switch to and allows for applications to write custom strategies
    # for swapping if needed.
    #
    # The ShardSelector takes a set of options (currently only +lock+ is supported)
    # that can be used by the middleware to alter behavior. +lock+ is
    # true by default and will prohibit the request from switching shards once
    # inside the block. If +lock+ is false, then shard swapping will be allowed.
    # For tenant based sharding, +lock+ should always be true to prevent application
    # code from mistakenly switching between tenants.
    #
    # Options can be set in the config:
    #
    #   config.active_record.shard_selector = { lock: true }
    #
    # Applications must also provide the code for the resolver as it depends on application
    # specific models. An example resolver would look like this:
    #
    #   config.active_record.shard_resolver = ->(request) {
    #     subdomain = request.subdomain
    #     tenant = Tenant.find_by_subdomain!(subdomain)
    #     tenant.shard
    #   }
    class ShardSelector
      def initialize(app, resolver, options = {})
        @app = app
        @resolver = resolver
        @options = options
      end

      attr_reader :resolver, :options

      def call(env)
        request = ActionDispatch::Request.new(env)

        shard = selected_shard(request)

        set_shard(shard) do
          @app.call(env)
        end
      end

      private
        def selected_shard(request)
          resolver.call(request)
        end

        def set_shard(shard, &block)
          ActiveRecord::Base.connected_to(shard: shard.to_sym) do
            ActiveRecord::Base.prohibit_shard_swapping(options.fetch(:lock, true), &block)
          end
        end
    end
  end
end

Version data entries

42 entries across 42 versions & 5 rubygems

Version Path
activerecord-8.0.2 lib/active_record/middleware/shard_selector.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/middleware/shard_selector.rb
activerecord-8.0.1 lib/active_record/middleware/shard_selector.rb
activerecord-8.0.0.1 lib/active_record/middleware/shard_selector.rb
activerecord-7.2.2.1 lib/active_record/middleware/shard_selector.rb
activerecord-7.1.5.1 lib/active_record/middleware/shard_selector.rb
activerecord-8.0.0 lib/active_record/middleware/shard_selector.rb
activerecord-7.2.2 lib/active_record/middleware/shard_selector.rb
activerecord-7.1.5 lib/active_record/middleware/shard_selector.rb
activerecord-8.0.0.rc2 lib/active_record/middleware/shard_selector.rb
activerecord-7.2.1.2 lib/active_record/middleware/shard_selector.rb
activerecord-7.1.4.2 lib/active_record/middleware/shard_selector.rb
activerecord-8.0.0.rc1 lib/active_record/middleware/shard_selector.rb
activerecord-7.2.1.1 lib/active_record/middleware/shard_selector.rb
activerecord-7.1.4.1 lib/active_record/middleware/shard_selector.rb
activerecord-8.0.0.beta1 lib/active_record/middleware/shard_selector.rb
omg-activerecord-8.0.0.alpha9 lib/active_record/middleware/shard_selector.rb
omg-activerecord-8.0.0.alpha8 lib/active_record/middleware/shard_selector.rb
omg-activerecord-8.0.0.alpha7 lib/active_record/middleware/shard_selector.rb
omg-activerecord-8.0.0.alpha4 lib/active_record/middleware/shard_selector.rb