Class: AmplitudeExperiment::PersistentHttpClient::ConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/experiment/persistent_http_client.rb

Overview

connection manager represents a cache of all keep-alive connections in a current thread

Constant Summary collapse

STALE_AFTER =

if a client wasn’t used within this time range it gets removed from the cache and the connection closed. This helps to make sure there are no memory leaks.

300
KEEP_ALIVE_TIMEOUT =

Seconds to reuse the connection of the previous request. If the idle time is less than this Keep-Alive Timeout, Net::HTTP reuses the TCP/IP socket used by the previous communication. Source: Ruby docs

30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnectionManager

Returns a new instance of ConnectionManager.



70
71
72
73
# File 'lib/experiment/persistent_http_client.rb', line 70

def initialize
  self.clients_store = {}
  self.last_used = Time.now
end

Instance Attribute Details

#clients_storeObject

KEEP_ALIVE_TIMEOUT vs STALE_AFTER STALE_AFTER - how long an Net::HTTP client object is cached in ruby KEEP_ALIVE_TIMEOUT - how long that client keeps TCP/IP socket open.



68
69
70
# File 'lib/experiment/persistent_http_client.rb', line 68

def clients_store
  @clients_store
end

#last_usedObject

KEEP_ALIVE_TIMEOUT vs STALE_AFTER STALE_AFTER - how long an Net::HTTP client object is cached in ruby KEEP_ALIVE_TIMEOUT - how long that client keeps TCP/IP socket open.



68
69
70
# File 'lib/experiment/persistent_http_client.rb', line 68

def last_used
  @last_used
end

Instance Method Details

#close_connections!Object

close connections for each client



111
112
113
114
115
116
# File 'lib/experiment/persistent_http_client.rb', line 111

def close_connections!
  mutex.synchronize do
    clients_store.values.each(&:finish)
    self.clients_store = {}
  end
end

#get_client(uri, options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/experiment/persistent_http_client.rb', line 75

def get_client(uri, options)
  mutex.synchronize do
    # refresh the last time a client was used,
    # this prevents the client from becoming stale
    self.last_used = Time.now

    # we use params as a cache key for clients.
    # 2 connections to the same host but with different
    # options are going to use different HTTP clients
    params = [uri.host, uri.port, options]
    client = clients_store[params]

    return client if client

    client = Net::HTTP.new(uri.host, uri.port)
    client.keep_alive_timeout = KEEP_ALIVE_TIMEOUT

    # set SSL to true if a scheme is https
    client.use_ssl = uri.scheme == 'https'

    # dynamically set Net::HTTP options
    DEFAULT_OPTIONS.merge(options).each_pair do |key, value|
      client.public_send("#{key}=", value)
    end

    # open connection
    client.start

    # cache the client
    clients_store[params] = client

    client
  end
end

#mutexObject



122
123
124
# File 'lib/experiment/persistent_http_client.rb', line 122

def mutex
  @mutex ||= Mutex.new
end

#stale?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/experiment/persistent_http_client.rb', line 118

def stale?
  Time.now - last_used > STALE_AFTER
end