/usr/lib/ruby/vendor_ruby/sequel/extensions/server_block.rb is in ruby-sequel 3.33.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | # The server_block extension adds the Database#with_server method, which takes a shard
# argument and a block, and makes it so that access inside the block will use the
# specified shard by default.
#
# First, you need to enable it on the database object:
#
# Sequel.extension :server_block
# DB.extend Sequel::ServerBlock
#
# Then you can call with_server:
#
# DB.with_server(:shard1) do
# DB[:a].all # Uses shard1
# DB[:a].server(:shard2).all # Uses shard2
# end
# DB[:a].all # Uses default
#
# You can even nest calls to with_server:
#
# DB.with_server(:shard1) do
# DB[:a].all # Uses shard1
# DB.with_server(:shard2) do
# DB[:a].all # Uses shard2
# end
# DB[:a].all # Uses shard1
# end
# DB[:a].all # Uses default
#
# Note this this extension assumes the following shard names should use the
# server/shard passed to with_server: :default, nil, :read_only. All other
# shard names will cause the standard behavior to be used.
module Sequel
module ServerBlock
# Enable the server block on the connection pool, choosing the correct
# extension depending on whether the connection pool is threaded or not.
# Also defines the with_server method on the receiver for easy use.
def self.extended(db)
pool = db.pool
if defined?(ShardedThreadedConnectionPool) && pool.is_a?(ShardedThreadedConnectionPool)
pool.extend(ThreadedServerBlock)
pool.instance_variable_set(:@default_servers, {})
else
pool.extend(UnthreadedServerBlock)
pool.instance_variable_set(:@default_servers, [])
end
end
# Delegate to the connection pool
def with_server(server, &block)
pool.with_server(server, &block)
end
end
# Adds with_server support for the sharded single connection pool.
module UnthreadedServerBlock
# Set a default server/shard to use inside the block.
def with_server(server)
begin
set_default_server(server)
yield
ensure
clear_default_server
end
end
private
# Make the given server the new default server.
def set_default_server(server)
@default_servers << server
end
# Remove the current default server, restoring the
# previous default server.
def clear_default_server
@default_servers.pop
end
# Use the server given to with_server if appropriate.
def pick_server(server)
if @default_servers.empty?
super
else
case server
when :default, nil, :read_only
@default_servers.last
else
super
end
end
end
end
# Adds with_server support for the sharded threaded connection pool.
module ThreadedServerBlock
# Set a default server/shard to use inside the block for the current
# thread.
def with_server(server)
begin
set_default_server(server)
yield
ensure
clear_default_server
end
end
private
# Make the given server the new default server for the current thread.
def set_default_server(server)
sync{(@default_servers[Thread.current] ||= [])} << server
end
# Remove the current default server for the current thread, restoring the
# previous default server.
def clear_default_server
t = Thread.current
a = sync{@default_servers[t]}
a.pop
sync{@default_servers.delete(t)} if a.empty?
end
# Use the server given to with_server for the given thread, if appropriate.
def pick_server(server)
a = sync{@default_servers[Thread.current]}
if !a || a.empty?
super
else
case server
when :default, nil, :read_only
a.last
else
super
end
end
end
end
end
|