Sha256: aacaaeb0286338342f24445e4720ea7bbc8f4a6a04f200581f029e55b480696f

Contents?: true

Size: 1.28 KB

Versions: 18

Compression:

Stored size: 1.28 KB

Contents

module Ftpd

  # This class limits the number of connections

  class ConnectionThrottle

    DEFAULT_MAX_CONNECTIONS = nil
    DEFAULT_MAX_CONNECTIONS_PER_IP = nil

    # The maximum number of connections, or nil if there is no limit.
    # @return [Integer]

    attr_accessor :max_connections

    # The maximum number of connections for an IP, or nil if there is
    # no limit.
    # @return [Integer]

    attr_accessor :max_connections_per_ip

    # @param connection_tracker [ConnectionTracker]

    def initialize(connection_tracker)
      @max_connections = DEFAULT_MAX_CONNECTIONS
      @max_connections_per_ip = DEFAULT_MAX_CONNECTIONS_PER_IP
      @connection_tracker = connection_tracker
    end

    # @return [Boolean] true if the connection should be allowed

    def allow?(socket)
      allow_by_total_count &&
        allow_by_ip_count(socket)
    end

    # Reject a connection

    def deny(socket)
      socket.write "421 Too many connections\r\n"
    end

    private

    def allow_by_total_count
      return true unless @max_connections
      @connection_tracker.connections < @max_connections
    end

    def allow_by_ip_count(socket)
      return true unless @max_connections_per_ip
      @connection_tracker.connections_for(socket) < @max_connections_per_ip
    end

  end

end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
ftpd-1.1.1 lib/ftpd/connection_throttle.rb
ftpd-1.1.0 lib/ftpd/connection_throttle.rb
investtools-ftpd-1.0.1 lib/ftpd/connection_throttle.rb
ftpd-1.0.1 lib/ftpd/connection_throttle.rb
ftpd-1.0.0 lib/ftpd/connection_throttle.rb
ftpd-0.17.0 lib/ftpd/connection_throttle.rb
ftpd-0.16.0 lib/ftpd/connection_throttle.rb
ftpd-0.15.0 lib/ftpd/connection_throttle.rb
ftpd-0.14.0 lib/ftpd/connection_throttle.rb
ftpd-0.13.0 lib/ftpd/connection_throttle.rb
ftpd-0.12.0 lib/ftpd/connection_throttle.rb
ftpd-0.11.0 lib/ftpd/connection_throttle.rb
ftpd-0.10.0 lib/ftpd/connection_throttle.rb
ftpd-0.9.0 lib/ftpd/connection_throttle.rb
ftpd-0.7.1 lib/ftpd/connection_throttle.rb
ftpd-0.8.0 lib/ftpd/connection_throttle.rb
ftpd-0.7.0 lib/ftpd/connection_throttle.rb
ftpd-0.6.0 lib/ftpd/connection_throttle.rb