Class: Tire::HTTP::Client::PooledCurb

Inherits:
Object
  • Object
show all
Defined in:
lib/tire/http/clients/pooled_curb.rb

Constant Summary collapse

HTTP_CLIENT_POOL =
CommonPool::ObjectPool.new(PooledCurbDataSource.new) { |config| config.max_active = 200 }

Class Method Summary collapse

Class Method Details

.delete(url) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/tire/http/clients/pooled_curb.rb', line 103

def self.delete(url)
  with_client do |curl|
    curl.timeout = 15
    curl.url = url
    curl.http_delete
    Response.new curl.body_str, curl.response_code
  end
end

.get(url, data = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tire/http/clients/pooled_curb.rb', line 46

def self.get(url, data=nil)
  response = nil

  3.times do |tries|
    # Sleep for 0.2 seconds after the second failure
    Kernel.sleep(0.2) if tries > 1

    begin
      response = get_once(url, data)
      if block_given?
        next unless yield response.body, response.code
      else
        next unless response.code == 200 && response.present?
      end

      return response
    rescue Curl::Err::CurlError
      # will retry
    end
  end

  response
end

.get_once(url, data = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tire/http/clients/pooled_curb.rb', line 70

def self.get_once(url, data=nil)
  with_client do |curl|
    curl.timeout = 15
    curl.url = url
    if data
      curl.post_body = data
      curl.http_post
    else
      curl.http_get
    end
    Response.new curl.body_str, curl.response_code
  end
end

.head(url) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/tire/http/clients/pooled_curb.rb', line 112

def self.head(url)
  with_client do |curl|
    curl.timeout = 15
    curl.url = url
    curl.http_head
    Response.new curl.body_str, curl.response_code
  end
end

.post(url, data) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/tire/http/clients/pooled_curb.rb', line 84

def self.post(url, data)
  with_client do |curl|
    curl.timeout = 15
    curl.url = url
    curl.post_body = data
    curl.http_post
    Response.new curl.body_str, curl.response_code
  end
end

.put(url, data) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/tire/http/clients/pooled_curb.rb', line 94

def self.put(url, data)
  with_client do |curl|
    curl.timeout = 15
    curl.url = url
    curl.http_put data
    Response.new curl.body_str, curl.response_code
  end
end

.with_clientObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tire/http/clients/pooled_curb.rb', line 34

def self.with_client
  curl = HTTP_CLIENT_POOL.borrow_object
  begin
    yield curl
  rescue Curl::Err::MultiBadEasyHandle
    curl = HTTP_CLIENT_POOL.borrow_object
    retry
  ensure
    HTTP_CLIENT_POOL.return_object(curl)
  end
end