Module: MiyazakiResistance::TokyoConnection::ClassMethods

Defined in:
lib/miyazaki_resistance/tokyo_connection.rb

Constant Summary collapse

DEFAULT =
{
  :timeout => 60,
  :config => "miyazakiresistance.yml",
  :port => 1978,
  :role => :readonly
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#all_columnsObject

Returns the value of attribute all_columns.



11
12
13
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 11

def all_columns
  @all_columns
end

#connection_poolObject

Returns the value of attribute connection_pool.



10
11
12
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 10

def connection_pool
  @connection_pool
end

#timeout_timeObject

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

Raises:

  • (TimeoutError)


90
91
92
93
94
95
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 90

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_connectionObject



65
66
67
68
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 65

def read_connection
  check_pool
  self.connection_pool[:read].sort_by{rand}.first
end

#remove_pool(rdb) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 74

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

#server_config(env, file = DEFAULT[:config]) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 21

def server_config(env, file = DEFAULT[:config])
  env = env.to_s
  conf = YAML.load_file(file)

  class_variable_set("@@logger", Logger.new(config["log_file"])) if conf["log_file"]

  if (config = conf[env]).nil?
    logger_fatal "specified environment(#{env}) is not found in conig file(#{file})"
    return
  end

  config["set_server"].each do |work|
    set_server work["server"], work["port"], work["role"]
  end
rescue Errno::ENOENT => e
  logger_fatal "config file is not found : #{file}"
end

#set_column(name, type, index = :no_index) ⇒ Object



58
59
60
61
62
63
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 58

def set_column(name, type, index = :no_index)
  self.all_columns ||= {}
  name = name.to_s
  self.__send__(:attr_accessor, name)
  self.all_columns.update(name => type)
end

#set_server(host, port = DEFAULT[:port], role = DEFAULT[:role]) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 39

def set_server(host, port = DEFAULT[:port], role = DEFAULT[:role])
  self.connection_pool ||= {:read => [], :write => nil, :standby => nil}
  rdb = TokyoTyrant::RDBTBL.new
  logger_info "set server host : #{host} port : #{port} role : #{role}"

  unless rdb.open(host.to_s, port)
    logger_fatal "TokyoTyrantConnectError host : #{host} port : #{port} role : #{role}"
    raise TokyoTyrantConnectError
  end

  self.connection_pool[:read] << rdb
  self.connection_pool[:write] = rdb if role.to_sym == :write
  self.connection_pool[:standby] = rdb if role.to_sym == :standby
end

#set_timeout(seconds) ⇒ Object



54
55
56
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 54

def set_timeout(seconds)
  self.timeout_time = seconds.to_i
end

#write_connectionObject



70
71
72
# File 'lib/miyazaki_resistance/tokyo_connection.rb', line 70

def write_connection
  self.connection_pool[:write]
end