Class: Ftpd::ConnectionTracker
- Inherits:
-
Object
- Object
- Ftpd::ConnectionTracker
- Defined in:
- lib/ftpd/connection_tracker.rb
Overview
This class keeps track of connections
Instance Method Summary collapse
-
#connections ⇒ Object
Return the total number of connections.
-
#connections_for(socket) ⇒ Object
Return the number of connections for a socket.
-
#initialize ⇒ ConnectionTracker
constructor
A new instance of ConnectionTracker.
-
#known_ip_count ⇒ Object
Return the number of known IPs.
-
#track(socket) ⇒ Object
Track a connection.
Constructor Details
#initialize ⇒ ConnectionTracker
Returns a new instance of ConnectionTracker.
7 8 9 10 11 |
# File 'lib/ftpd/connection_tracker.rb', line 7 def initialize @mutex = Mutex.new @connections = {} @socket_ips ={} end |
Instance Method Details
#connections ⇒ Object
Return the total number of connections
15 16 17 18 19 |
# File 'lib/ftpd/connection_tracker.rb', line 15 def connections @mutex.synchronize do @connections.values.inject(0, &:+) end end |
#connections_for(socket) ⇒ Object
Return the number of connections for a socket
23 24 25 26 27 28 |
# File 'lib/ftpd/connection_tracker.rb', line 23 def connections_for(socket) @mutex.synchronize do ip = peer_ip(socket) @connections[ip] || 0 end end |
#known_ip_count ⇒ Object
Return the number of known IPs. This exists for the benefit of the test, so that it can know the tracker has properly forgotten about an IP with no connections.
46 47 48 49 50 |
# File 'lib/ftpd/connection_tracker.rb', line 46 def known_ip_count @mutex.synchronize do @connections.size end end |
#track(socket) ⇒ Object
Track a connection. Yields to a block; the connection is tracked until the block returns.
33 34 35 36 37 38 39 40 |
# File 'lib/ftpd/connection_tracker.rb', line 33 def track(socket) start_track socket begin yield ensure stop_track socket end end |