class Fastdfs::Client::Tracker

Attributes

options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/fastdfs-client/tracker.rb, line 14
def initialize(options = {})
  @options = default_options_merge(options)
  @proxies = @options[:trackers].map do |tracker| 
    opt = tracker.fs_symbolize_keys
    ClientProxy.new(opt[:host], opt[:port], extract_proxy_options.merge(alive: true)) 
  end

end

Public Instance Methods

get_storage(alive = false) click to toggle source
# File lib/fastdfs-client/tracker.rb, line 23
def get_storage(alive = false)
  res = proxy.dispose(CMD::STORE_WITHOUT_GROUP_ONE) do |body|
    storage_ip = body[ProtoCommon::IPADDR].strip
    storage_port = body[ProtoCommon::PORT].unpack("C*").to_pack_long
    store_path = body[ProtoCommon::TRACKER_BODY_LEN-1].unpack("C*")[0]
  
    Storage.new(storage_ip, storage_port, store_path, extract_proxy_options.merge(alive: alive))
  end
  raise res[:err_msg] unless res[:status]
  res[:result]
end
pipeline() { |storage| ... } click to toggle source
# File lib/fastdfs-client/tracker.rb, line 35
def pipeline
  storage = get_storage(true)
  yield storage
  storage
ensure
  storage.proxy.close
end

Private Instance Methods

default_options() click to toggle source
# File lib/fastdfs-client/tracker.rb, line 55
def default_options
  {
    host: nil,
    port: nil,
    trackers: [
      {host: "127.0.0.1", port: "22122"}
    ],
    connection_timeout: 3,
    recv_timeout: 20
  }
end
default_options_merge(options = {}) click to toggle source
# File lib/fastdfs-client/tracker.rb, line 44
def default_options_merge(options = {})
  opts = default_options.merge(options)
  tracker = {host: opts.delete(:host), port: opts.delete(:port)}
  if !tracker[:host].nil?
    opts[:trackers] = [tracker]
  elsif opts[:trackers].is_a?(Hash)
    opts[:trackers] = [opts[:trackers]]
  end
  opts
end
extract_proxy_options() click to toggle source
# File lib/fastdfs-client/tracker.rb, line 67
def extract_proxy_options
  keys = [:connection_timeout, :recv_timeout]
  @options.select{|key| keys.include?(key) }
end
proxy() click to toggle source
# File lib/fastdfs-client/tracker.rb, line 72
def proxy
  @proxy_index ||= -1
  @proxy_index += 1
  @proxy_index = 0 if @proxy_index >= @proxies.length
  @proxies[@proxy_index]
end