Class: Tair::Client

Inherits:
Object
  • Object
show all
Includes:
Log, Operation
Defined in:
lib/tair/client.rb

Constant Summary collapse

WRITE_OPERATIONS =
[:put, :incr, :set_count, :delete]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#colorize, included, #log_bytes, #log_time, #logger, #logger_colorize, #tair_bytes_log_file

Methods included from Operation

#count, #decr, #delete, #fetch_data_servers, #get, #incr, #put, #valid_count_step?

Methods included from Key

#valid_key?

Constructor Details

#initialize(host, port, group: nil, namespace: 0xab) ⇒ Client

Returns a new instance of Client.



31
32
33
# File 'lib/tair/client.rb', line 31

def initialize(host, port, group: nil, namespace: 0xab)
  @host, @port, @group, @namespace = host, port, group, namespace
end

Instance Attribute Details

#data_servers=(value) ⇒ Object (writeonly)

Sets the attribute data_servers

Parameters:

  • value

    the value to set the attribute data_servers to.



29
30
31
# File 'lib/tair/client.rb', line 29

def data_servers=(value)
  @data_servers = value
end

#groupObject (readonly)

Returns the value of attribute group.



28
29
30
# File 'lib/tair/client.rb', line 28

def group
  @group
end

#hostObject (readonly)

Returns the value of attribute host.



28
29
30
# File 'lib/tair/client.rb', line 28

def host
  @host
end

#namespaceObject

Returns the value of attribute namespace.



16
17
18
# File 'lib/tair/client.rb', line 16

def namespace
  @namespace
end

#portObject (readonly)

Returns the value of attribute port.



28
29
30
# File 'lib/tair/client.rb', line 28

def port
  @port
end

Class Method Details

.create(host, port, opts = {}, &block) ⇒ Object



21
22
23
24
25
# File 'lib/tair/client.rb', line 21

def self.create(host, port, opts={}, &block)
  new(host, port, opts).tap do |client|
    block && block.call(client)
  end
end

Instance Method Details

#init_cluster(retries: 5) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tair/client.rb', line 62

def init_cluster(retries: 5)
  count = retries
  begin
    cluster
  rescue => e
    error { "init cluster error: #{e}" }
    count -= 1
    if count >= 0
      retry
    else
      raise
    end
  end
end

#operate(op, request, key: nil, &block) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/tair/client.rb', line 52

def operate(op, request, key: nil, &block)
  request.namespace = namespace
  will_write_data = WRITE_OPERATIONS.include? op
  conn = data_conn(key, will_write_data: will_write_data)
  log_time "#{op} #{key.inspect} on #{conn.host}" do
    conn.operate(request, &block)
  end
end

#watch_cluster_config(interval = 120) ⇒ Object

配置有变化时,需要更新到客户端



37
38
39
# File 'lib/tair/client.rb', line 37

def watch_cluster_config(interval=120)
  Thread.new { watch_cluster_config!(interval) }
end

#watch_cluster_config!(interval = 120) ⇒ Object



42
43
44
45
46
47
# File 'lib/tair/client.rb', line 42

def watch_cluster_config!(interval=120)
  while true
    sleep interval
    @cluster = fetch_data_servers(group)
  end
end