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.



76
77
78
79
# File 'lib/experiment/persistent_http_client.rb', line 76

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.



74
75
76
# File 'lib/experiment/persistent_http_client.rb', line 74

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.



74
75
76
# File 'lib/experiment/persistent_http_client.rb', line 74

def last_used
  @last_used
end

Instance Method Details

#close_connections!Object

close connections for each client



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

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

#get_client(uri, options, api_key) ⇒ Object



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
109
110
111
112
113
114
# File 'lib/experiment/persistent_http_client.rb', line 81

def get_client(uri, options, api_key)
  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, api_key]
    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



128
129
130
# File 'lib/experiment/persistent_http_client.rb', line 128

def mutex
  @mutex ||= Mutex.new
end

#stale?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/experiment/persistent_http_client.rb', line 124

def stale?
  Time.now - last_used > STALE_AFTER
end