Sha256: b82889e010c0a7ee0c9c65a1a1ec1d1235d634dec6914365f3f34218d90492c0

Contents?: true

Size: 1.77 KB

Versions: 22

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require 'bundler/setup'
require 'polyphony'
require 'http/parser'

class Client
  def initialize(id, host, port, http_host, interval)
    @id = id
    @host = host
    @port = port
    @http_host = http_host
    @interval = interval.to_f
    @interval_delta = @interval / 2
  end

  def run
    while true
      connect && issue_requests
      sleep 5
    end
  end

  def connect
    @socket = Polyphony::Net.tcp_connect(@host, @port)
  rescue SystemCallError
    false
  end

  REQUEST = <<~HTTP
  GET / HTTP/1.1
  Host: %s

  HTTP

  def issue_requests
    @parser = Http::Parser.new
    @parser.on_message_complete = proc { @got_reply = true }
    @parser.on_body = proc { |chunk| @response = chunk }

    while true
      do_request
      sleep rand((@interval - @interval_delta)..(@interval + @interval_delta))
    end
  rescue IOError, Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED => e
    # fail quitely
  ensure
    @parser = nil
  end

  def do_request
    @got_reply = nil
    @response = nil
    @socket << format(REQUEST, @http_host)
    wait_for_response
    # if @parser.status_code != 200
    #   puts "Got status code #{@parser.status_code} from #{@http_host} => #{@parser.headers && @parser.headers['X-Request-ID']}"
    # end
    # puts "#{Time.now} [client-#{@id}] #{@http_host} => #{@response || '<error>'}"
  end

  def wait_for_response
    @socket.recv_loop do |data|
      @parser << data
      return @response if @got_reply
    end
  end
end

def spin_client(id, host)
  spin do
    client = Client.new(id, 'localhost', 4411, host, 30)
    client.run
  end
end

spin_loop(interval: 60) { GC.start }

10000.times { |id| spin_client(id, "#{rand(1..400)}.realiteq.net") }

trap('SIGINT') { exit! }

puts "Multi client pid: #{Process.pid}"
sleep

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
tipi-0.55 df/multi_client.rb
tipi-0.54 df/multi_client.rb
tipi-0.53 df/multi_client.rb
tipi-0.52 df/multi_client.rb
tipi-0.51 df/multi_client.rb
tipi-0.50 df/multi_client.rb
tipi-0.49 df/multi_client.rb
tipi-0.47 df/multi_client.rb
tipi-0.46 df/multi_client.rb
tipi-0.45 df/multi_client.rb
tipi-0.43 df/multi_client.rb
tipi-0.42 df/multi_client.rb
tipi-0.41 df/multi_client.rb
tipi-0.40 df/multi_client.rb
tipi-0.39 df/multi_client.rb
tipi-0.38 df/multi_client.rb
tipi-0.37.2 df/multi_client.rb
tipi-0.37.1 df/multi_client.rb
tipi-0.37 df/multi_client.rb
tipi-0.36 df/multi_client.rb