Sha256: 2847e19b3250be5bc196333132c8de17148ab925b456522a6223bc130306db88

Contents?: true

Size: 1.13 KB

Versions: 11

Compression:

Stored size: 1.13 KB

Contents

require 'thread'

module Skylight
  module Worker
    class ConnectionSet
      attr_reader :open_connections, :throughput

      def initialize
        @connections = {}
        @lock = Mutex.new

        # Metrics
        @open_connections = build_open_connections_metric
        @throughput = build_throughput_metric
      end

      def add(sock)
        conn = Connection.new(sock)
        @lock.synchronize { @connections[sock] = conn }
        conn
      end

      def socks
        @lock.synchronize { @connections.keys }
      end

      def [](sock)
        @lock.synchronize do
          @connections[sock]
        end
      end

      def cleanup(sock)
        if conn = @lock.synchronize { @connections.delete(sock) }
          conn.cleanup
          sock.close rescue nil
        end
      end

    private

      def build_open_connections_metric
        lambda do
          @lock.synchronize { @connections.length }
        end
      end

      def build_throughput_metric
        lambda do
          conns = @lock.synchronize { @connections.values }
          conns.map { |c| c.throughput.rate.to_i }
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
skylight-0.3.21 lib/skylight/worker/connection_set.rb
skylight-0.3.20 lib/skylight/worker/connection_set.rb
skylight-0.3.19 lib/skylight/worker/connection_set.rb
skylight-0.3.18 lib/skylight/worker/connection_set.rb
skylight-0.3.17 lib/skylight/worker/connection_set.rb
skylight-0.3.14 lib/skylight/worker/connection_set.rb
skylight-0.3.13 lib/skylight/worker/connection_set.rb
skylight-0.3.12 lib/skylight/worker/connection_set.rb
skylight-0.3.11 lib/skylight/worker/connection_set.rb
skylight-0.3.10 lib/skylight/worker/connection_set.rb
skylight-0.3.8 lib/skylight/worker/connection_set.rb