drbrain / net-http-persistent

Thread-safe persistent connections with Net::HTTP

Home Page:http://seattlerb.rubyforge.org/net-http-persistent

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

http.request uri in loop not use same connection

tablecell opened this issue · comments

serve.rb

require 'socket' 
server = TCPServer.open('127.0.0.1', 80) 
loop { 
    Thread.start(server.accept) do | client |  # new thread if new  conn comming
	    puts  'connected',client;
	    loop{  #for establish cconn  loop read/write
	        
	        lines = []
	        while line = client.gets and line !~ /^\s*$/
	            lines << line.chomp
	        end
	        puts lines
	        puts Thread.current
	  
	     
	        echo_resp="Your conn is serve by thread %s" % Thread.current  # echo back     to client 
	        headers = ["HTTP/1.0 200 OK", 
	                   "Server: ruby multi thread server", 
	                   "Content-Type: text/plain; charset=utf-8", 
	                   "Content-Length: #{echo_resp.length}\r\n\r\n"].join("\r\n")
	      
	        client.write headers   #send to the client
	        client.write echo_resp 
	        puts '-------------- next --------------'     # loop    next client.gets 
	            
	    }

    end
}

client.rb

require 'net/http/persistent'



url='http://127.0.0.1'
uri = URI  url

http = Net::HTTP::Persistent.new name: 'my_app_name'

# perform 10 times GET/POST

(1..10).each do |idx |
	response = http.request uri
        puts response.body
	# create a POST
	post_uri = uri + 'create'
	post = Net::HTTP::Post.new post_uri.path
	post.set_form_data 'some' => 'cool data'

	# perform the POST, the URI is always required
	response = http.request post_uri, post
	puts response.body
	sleep 3
end 


http.shutdown

client send requests (assume persistent) , server fork new thread to serve request and echo back thread.current to client
but ever request in 10 times echo different Thread.current that means fork different thread to serve next request

Server add headers 'Connection: Keep-Alive'
'net/http/persistent' recieve correct same Trhead .currrent

please close the issue