Module: MiyazakiResistance::TokyoConnection::ClassMethods
- Defined in:
- lib/miyazaki_resistance/tokyo_connection.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
60
Instance Attribute Summary collapse
-
#all_columns ⇒ Object
Returns the value of attribute all_columns.
-
#all_indexes ⇒ Object
Returns the value of attribute all_indexes.
-
#connection_pool ⇒ Object
Returns the value of attribute connection_pool.
-
#timeout_time ⇒ Object
Returns the value of attribute timeout_time.
Instance Method Summary collapse
- #kaeru_timeout(&block) ⇒ Object
- #read_connection ⇒ Object
- #remove_pool(rdb) ⇒ Object
- #set_column(name, type, index = :no_index) ⇒ Object
- #set_server(host, port, target = :readonly) ⇒ Object
- #set_timeout(seconds) ⇒ Object
- #write_connection ⇒ Object
Instance Attribute Details
#all_columns ⇒ Object
Returns the value of attribute all_columns.
10 11 12 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 10 def all_columns @all_columns end |
#all_indexes ⇒ Object
Returns the value of attribute all_indexes.
11 12 13 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 11 def all_indexes @all_indexes end |
#connection_pool ⇒ Object
Returns the value of attribute connection_pool.
9 10 11 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 9 def connection_pool @connection_pool end |
#timeout_time ⇒ Object
Returns the value of attribute timeout_time.
12 13 14 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 12 def timeout_time @timeout_time end |
Instance Method Details
#kaeru_timeout(&block) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 68 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
43 44 45 46 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 43 def read_connection check_pool self.connection_pool[:read].sort_by{rand}.first end |
#remove_pool(rdb) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 52 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 logger_fatal "remove pool : host #{host} port : #{port}" check_pool fail_over if rdb == self.connection_pool[:write] end rdb.close end |
#set_column(name, type, index = :no_index) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 35 def set_column(name, type, index = :no_index) self.all_indexes ||= [] self.all_columns ||= {} name = name.to_s self.__send__(:attr_accessor, name) self.all_columns.update(name => type) end |
#set_server(host, port, target = :readonly) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 16 def set_server(host, port, target = :readonly) logger_debug "set_server host : #{host} port : #{port} target : #{target}" self.connection_pool ||= {:read => [], :write => nil, :standby => nil} rdb = TokyoTyrant::RDBTBL.new unless rdb.open(host.to_s, port) logger_fatal "TokyoTyrantConnectError host : #{host} port : #{port} target : #{target}" raise TokyoTyrantConnectError end self.connection_pool[:read] << rdb self.connection_pool[:write] = rdb if target == :write self.connection_pool[:standby] = rdb if target == :standby end |
#set_timeout(seconds) ⇒ Object
31 32 33 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 31 def set_timeout(seconds) self.timeout_time = seconds.to_i end |
#write_connection ⇒ Object
48 49 50 |
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 48 def write_connection self.connection_pool[:write] end |