Class: MiyazakiResistance::Connection
- Inherits:
-
Object
- Object
- MiyazakiResistance::Connection
- Defined in:
- lib/miyazaki_resistance/connection.rb
Constant Summary collapse
- DEFAULT =
{:timeout => 5, :config => "miyazakiresistance.yml", :port => 1978, :role => :readonly}
Instance Attribute Summary collapse
-
#connection_pool ⇒ Object
Returns the value of attribute connection_pool.
-
#timeout_time ⇒ Object
Returns the value of attribute timeout_time.
Instance Method Summary collapse
- #connection(con) ⇒ Object
- #kaeru_timeout(&block) ⇒ Object
- #read_connection ⇒ Object
- #remove_pool(rdb) ⇒ Object
- #server(host, port = , role = ) ⇒ Object
- #server_config(env, file = ) ⇒ Object
- #timeout(seconds) ⇒ Object
- #write_connection ⇒ Object
Instance Attribute Details
#connection_pool ⇒ Object
Returns the value of attribute connection_pool.
63 64 65 |
# File 'lib/miyazaki_resistance/connection.rb', line 63 def connection_pool @connection_pool end |
#timeout_time ⇒ Object
Returns the value of attribute timeout_time.
64 65 66 |
# File 'lib/miyazaki_resistance/connection.rb', line 64 def timeout_time @timeout_time end |
Instance Method Details
#connection(con) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/miyazaki_resistance/connection.rb', line 98 def connection(con) unless con.open? unless con.open MR::MiyazakiLogger.fatal "TokyoTyrantConnectError host : #{con.host} port : #{con.port}" raise MiyazakiResistance::TokyoTyrantConnectError end end con end |
#kaeru_timeout(&block) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/miyazaki_resistance/connection.rb', line 132 def kaeru_timeout(&block) ret = nil thread = Thread.new{ret = yield} raise TimeoutError, "tokyo tyrant server response error" unless thread.join(self.timeout_time || DEFAULT[:timeout]) ret end |
#read_connection ⇒ Object
108 109 110 111 |
# File 'lib/miyazaki_resistance/connection.rb', line 108 def read_connection check_pool connection(self.connection_pool[:read].sort_by{rand}.first) end |
#remove_pool(rdb) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/miyazaki_resistance/connection.rb', line 117 def remove_pool(rdb) self.connection_pool[:read].delete_if{|pool| pool == rdb} host, port = rdb.host, rdb.port new_rdb = TokyoTyrant::RDBTBL.new if new_rdb.open(host, port) self.connection_pool[:read] << new_rdb self.connection_pool[:write] = new_rdb if rdb == self.connection_pool[:write] else MR::MiyazakiLogger.fatal "remove pool : host #{host} port : #{port}" fail_over if rdb == self.connection_pool[:write] end rdb.close end |
#server(host, port = , role = ) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/miyazaki_resistance/connection.rb', line 79 def server(host, port = DEFAULT[:port], role = DEFAULT[:role]) self.connection_pool ||= {:read => [], :write => nil, :standby => nil} rdb = TokyoTyrant::RDBTBL.new MR::MiyazakiLogger.info "set server host : #{host} port : #{port} role : #{role}" rdb.set_server(host.to_s, port) if role.to_sym == :standby self.connection_pool[:standby] = rdb else self.connection_pool[:read] << rdb self.connection_pool[:write] = rdb if role.to_sym == :write end end |
#server_config(env, file = ) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/miyazaki_resistance/connection.rb', line 66 def server_config(env, file = DEFAULT[:config]) conf = YAML.load_file(file) if config = conf[env.to_s] config["set_server"].each do |work| server(work["server"], work["port"], work["role"]) end else MR::MiyazakiLogger.fatal "specified environment(#{env}) is not found in conig file(#{file})" end rescue Errno::ENOENT => e MR::MiyazakiLogger.fatal "config file is not found : #{file}" end |
#timeout(seconds) ⇒ Object
94 95 96 |
# File 'lib/miyazaki_resistance/connection.rb', line 94 def timeout(seconds) self.timeout_time = seconds.to_i end |
#write_connection ⇒ Object
113 114 115 |
# File 'lib/miyazaki_resistance/connection.rb', line 113 def write_connection connection(self.connection_pool[:write]) end |