Skip to content

Instantly share code, notes, and snippets.

@KINGSABRI
Forked from sonots/gist:8923003
Created February 14, 2017 11:35
Show Gist options
  • Save KINGSABRI/fdf0132b0dfb69bc78948c7efc85e886 to your computer and use it in GitHub Desktop.
Save KINGSABRI/fdf0132b0dfb69bc78948c7efc85e886 to your computer and use it in GitHub Desktop.
how to use net/http in muliti threads
require 'net/http'
require 'uri'
host = "localhost"
port = 5125
path = "/api/hoge/hoge/hoge"
body = URI.encode_www_form({'number'=>0, 'mode'=>'gauge'})
# 1)
@client = Net::HTTP.new(host, port)
# @client.set_debug_output(STDOUT)
100.times { @client.post(path, body) }
# IOError closed stream /usr/lib/fluent/ruby/lib/ruby/1.9.1/net/protocol.rb:143:in `select'
# IOError stream closed /usr/lib/fluent/ruby/lib/ruby/1.9.1/net/protocol.rb:199:in `write'
# IOError attempt to read body out of block /usr/lib/fluent/ruby/lib/ruby/1.9.1/net/http.rb:2788:in `stream_check'
# EOFError end of file reached /usr/lib/fluent/ruby/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock'
# Net::HTTPBadResponse wrong chunk size line: /usr/lib/fluent/ruby/lib/ruby/1.9.1/net/http.rb:2772:in `read_chunked'
# Timeout::Error Timeout::Error /usr/lib/fluent/ruby/lib/ruby/1.9.1/net/protocol.rb:146:in `rescue in rbuf_fill'
#POST /api/multiforecast/development%252Ffluentd%252Fflowcounter%252Fwatcher/localhost%3A28000 HTTP/1.1
# Content-Type: application/x-www-form-urlencoded
# Accept: */*
# User-Agent: Ruby
# Connection: close
# Host: 10.33.83.33:5125
# Content-Length: 21
# HTTP/1.1 200 OK
# Date: Mon, 10 Feb 2014 20:28:12 GMT
# Server: Plack::Handler::Starlet
# Content-Type: application/json; charset=UTF-8
# X-Content-Type-Options: nosniff
# X-Frame-Options:
# X-XSS-Protection: 1
# X-Powered-By: GrowthForecast/0.80
# Transfer-Encoding: chunked
# Connection: close
#
# 216
# {"error":0,"data":{"number":"0","llimit":"-1000000000","mode":"gauge","stype":"AREA","adjustval":"1","meta":"","service_name":"multiforecast","gmode":"gauge","color":"#663399","created_at":"2014/01/22 21:00:26","section_name":"development%2Ffluentd%2Fflowcounter%2Fworker","ulimit":"9.22337203685478e\u002b18","id":"9314","graph_name":"localhost:22000","description":"","sulimit":"10000
# 0","unit":"","sort":"0","updated_at":"2014/02/11 05:28:11","adjust":"*","type":"AREA","sllimit":"-100000","md5":"9afe487de556e59e6db6c862adfe25a4"}}
# 0
#
# 2)
@client = Net::HTTP.new(host, port)
req = Net::HTTP::Post.new(path).tap {|req| req.body = body }
1000.times { @client.start {|http| http.request(req) } }
# IOError HTTP session already opened /usr/lib/fluent/ruby/lib/ruby/1.9.1/net/http.rb:742:in `start'
# 3)
100.times {
@client = Net::HTTP.new(host, port)
req = Net::HTTP::Post.new(path).tap {|req| req.body = body }
@client.start {|http| http.request(req) }
}
# Looks fine
# POST /api/multiforecast/development%252Ffluentd%252Fflowcounter%252Fworker/localhost%3A22000 HTTP/1.1
# Accept: */*
# User-Agent: Ruby
# Host: 10.33.83.33
# Content-Type: application/x-www-form-urlencoded
# Content-Length: 21
# Date: Mon, 10 Feb 2014 19:42:09 GMT
# Server: Plack::Handler::Starlet
# Content-Type: application/json; charset=UTF-8
# X-Content-Type-Options: nosniff
# X-Frame-Options:
# X-XSS-Protection: 1
# X-Powered-By: GrowthForecast/0.80
# Transfer-Encoding: chunked
# Connection: close
#
# 218
# {"error":0,"data":{"number":"5","llimit":"-1000000000","mode":"gauge","stype":"AREA","adjustval":"1","meta":"","service_name":"multiforecast","gmode":"gauge","color":"#cc6633","created_at":"201
# 4/01/30 00:56:31","section_name":"development%2Ffluentd%2Felapsed_time%2Fwatcher","ulimit":"9.22337203685478e\u002b18","id":"9903","graph_name":"ping.worker_num","description":"","sulimit":"100
# 000","unit":"","sort":"0","updated_at":"2014/02/11 04:42:09","adjust":"*","type":"AREA","sllimit":"-100000","md5":"d93c96e6a23fff65b91b900aaa541998"}}
# 0
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment