Class: Gearman::ConnectionPool

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/gearman/connection_pool.rb

Constant Summary collapse

DEFAULT_PORT =
4730
TIME_BETWEEN_CHECKS =

seconds

60
SLEEP_TIME =

seconds

30

Instance Method Summary collapse

Methods included from Logging

included, #logger

Constructor Details

#initialize(servers = []) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gearman/connection_pool.rb', line 11

def initialize(servers = [])
  @bad_servers          = []
  @coalesce_connections = {}
  @connection_handler   = nil
  @job_servers          = []
  @reconnect_seconds    = 10
  @server_counter       = 0   # Round-robin distribution of requests
  @servers_mutex        = Mutex.new
  @last_check_time      = Time.now

  add_servers(servers)
end

Instance Method Details

#add_connection(connection) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/gearman/connection_pool.rb', line 24

def add_connection(connection)
  @servers_mutex.synchronize do
    if connection.is_healthy?
      activate_connection(connection)
    else
      deactivate_connection(connection)
    end
  end
end

#add_host_port(host_port) ⇒ Object



34
35
36
37
38
# File 'lib/gearman/connection_pool.rb', line 34

def add_host_port(host_port)
  host, port = host_port.split(":")
  connection = Connection.new(host, port.to_i)
  add_connection(connection)
end

#add_servers(servers) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gearman/connection_pool.rb', line 40

def add_servers(servers)
  if servers.class == String or servers.class == Symbol
    servers = [ servers.to_s ]
  end

  servers = servers.map {|s| s =~ /:/ ? s : "#{s}:#{DEFAULT_PORT}" }

  servers.each do |host_port|
    add_host_port(host_port)
  end
end

#get_connection(coalesce_key = nil) ⇒ Object



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

def get_connection(coalesce_key = nil)
  @servers_mutex.synchronize do
    logger.debug "Available job servers: #{@job_servers.inspect}"
    raise NoJobServersError if @job_servers.empty?
    @server_counter += 1
    @job_servers[@server_counter % @job_servers.size]
  end
end

#on_connection(&block) ⇒ Object



61
62
63
# File 'lib/gearman/connection_pool.rb', line 61

def on_connection(&block)
  @connection_handler = block
end

#poll_connections(timeout = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gearman/connection_pool.rb', line 65

def poll_connections(timeout = nil)
  update_job_servers
  available_sockets = []
  @servers_mutex.synchronize do
    available_sockets.concat @job_servers.collect { |conn| conn.socket }
  end
  if available_sockets.size > 0
  	logger.debug "Polling on #{available_sockets.size} available server(s) with a #{timeout} second timeout"
  	IO::select(available_sockets, nil, nil, timeout)
  end
end

#with_all_connections(&block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gearman/connection_pool.rb', line 77

def with_all_connections(&block)
  update_job_servers
  @servers_mutex.synchronize do
    @job_servers.each do |connection|
      begin
        block.call(connection)
      rescue NetworkError => ex
        logger.debug "Error with #{connection}, marking as bad"
        deactivate_connection(connection)
      end
    end
  end
end