Class: HttpClient::ConnectionCleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client/connection_cleaner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnectionCleaner

Returns a new instance of ConnectionCleaner.



9
10
11
12
13
14
15
# File 'lib/http_client/connection_cleaner.rb', line 9

def initialize
  @lock = Mutex.new
  @references = []
  @thread = Thread.new { work until @shutdown }
  @min_idle = 5
  @shutdown = false
end

Instance Attribute Details

#min_idleObject (readonly)

Returns the value of attribute min_idle.



7
8
9
# File 'lib/http_client/connection_cleaner.rb', line 7

def min_idle
  @min_idle
end

Instance Method Details

#register(client) ⇒ Object



17
18
19
20
21
22
# File 'lib/http_client/connection_cleaner.rb', line 17

def register(client)
  @lock.synchronize do
    @min_idle = [@min_idle, [1, client.config[:max_idle]].max].min
    @references.push(WeakRef.new(client))
  end
end

#shutdownObject



38
39
40
# File 'lib/http_client/connection_cleaner.rb', line 38

def shutdown
  @shutdown = true
end

#workObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/http_client/connection_cleaner.rb', line 24

def work
  sleep min_idle
  @lock.synchronize do
    @references = @references.map do |client|
      begin
        client.cleanup_connections
        client
      rescue
        nil
      end
    end.compact
  end
end