Sha256: 4d9f0d5020d10ca1658f6a23b84c0273f56cd17fc00b94ffee0974ac6324e7dc

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module LitmusPaper
  module Dependency
    class TCP
      class EndpointAvailable < EM::Connection
        def initialize(fiber, timeout, ip, port)
          @fiber = fiber
          @ip = ip
          @port = port
          EM.add_timer(timeout, method(:connection_timeout))
        end

        def connection_completed
          close_connection
          @fiber.resume(true)
        end

        def connection_timeout
          LitmusPaper.logger.info("Available check to #{@ip}:#{@port} failed with a timeout")
          @fiber.resume(false)
        end
      end

      def initialize(ip, port, options = {})
        @ip, @port = ip, port
        @timeout = options.fetch(:timeout, 2)
      end

      def available?
        fiber = Fiber.current

        EM.connect(@ip, @port, EndpointAvailable, fiber, @timeout, @ip, @port) do |connection|
          connection.set_pending_connect_timeout @timeout
        end

        return Fiber.yield
      end

      def to_s
        "Dependency::TCP(tcp://#{@ip}:#{@port})"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
litmus_paper-0.2.1 lib/litmus_paper/dependency/tcp.rb
litmus_paper-0.2.0 lib/litmus_paper/dependency/tcp.rb