Skip to content

“:pool_size => 1” not use same connection  #10

Description

@tablecell

serv.rb

require "socket"
require 'time'
server = TCPServer.open("127.0.0.1", 80)

loop {
  Thread.start(server.accept) do |client| # new thread  if  connection  accepted
    puts "connected", client
    loop { #for established connection   loop gets/write forever 
      lines = []
      while line = client.gets and line !~ /^\s*$/
       lines << line.chomp
      end
      puts lines
      puts Thread.current
t = Time.now.utc
      echo = "Your conn is serve by thread %s" % Thread.current   # echo Thread.current to client
      headers = ["HTTP/1.0 200 OK",
                 "Server: ruby multi thread server",
                 'Connection: Keep-Alive',
                 "Content-Type: text/plain; charset=utf-8",
		 "Date: " << Time.now.utc.strftime("%a, %d %b %Y %H:%M:%S GMT"),
       		 "Age: 267230",
                 "Content-Length: #{echo.length}\r\n\r\n"].join("\r\n")
      puts "%d +  %d  = %d" % [headers.size , echo.size,headers.size + echo.size]
      client.write headers   #send to the client
      client.write echo
      # loop  client gets not close conn
    }
  end
}

client.rb

require "httparty"
require "persistent_httparty"

class Twitter
  include HTTParty
  persistent_connection_adapter :name => "my_cool_rest_client",
                                :pool_size => 1,
                                :idle_timeout => 10,
                                :keep_alive => 30
  base_uri "localhost"

  format :html  
  def self.query
    get("/")
  end
end

10.times do
  resp = Twitter.query
  resp.headers.each do |k, v|
    print k.capitalize, ": ", v, "\n"
  end
  puts 
  puts resp.body
  sleep 2
end

```
client recieve  different Thread.current ,that means:pool_size => 1not use same connection 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions