Class: RequestPool

Inherits:
Object show all
Includes:
EventMachine::Deferrable
Defined in:
ext/puppet-load.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(concurrency, repeat, parameters) ⇒ RequestPool

Returns a new instance of RequestPool.



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'ext/puppet-load.rb', line 249

def initialize(concurrency, repeat, parameters)
  @parameters = parameters
  @current_request = 0
  @max_request = repeat * concurrency
  @repeat = repeat
  @concurrency = concurrency
  @requests = []
  @responses = {:succeeded => [], :failed => []}
  @times = {}
  @sizes = {}

  # initial spawn
  (1..concurrency).each do |i|
    spawn
  end

end

Instance Attribute Details

#concurrencyObject (readonly)

Returns the value of attribute concurrency.



247
248
249
# File 'ext/puppet-load.rb', line 247

def concurrency
  @concurrency
end

#max_requestObject (readonly)

Returns the value of attribute max_request.



247
248
249
# File 'ext/puppet-load.rb', line 247

def max_request
  @max_request
end

#repeatObject (readonly)

Returns the value of attribute repeat.



247
248
249
# File 'ext/puppet-load.rb', line 247

def repeat
  @repeat
end

#requestsObject (readonly)

Returns the value of attribute requests.



246
247
248
# File 'ext/puppet-load.rb', line 246

def requests
  @requests
end

#responsesObject (readonly)

Returns the value of attribute responses.



246
247
248
# File 'ext/puppet-load.rb', line 246

def responses
  @responses
end

#sizesObject (readonly)

Returns the value of attribute sizes.



246
247
248
# File 'ext/puppet-load.rb', line 246

def sizes
  @sizes
end

#timesObject (readonly)

Returns the value of attribute times.



246
247
248
# File 'ext/puppet-load.rb', line 246

def times
  @times
end

Instance Method Details

#add(index, conn) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'ext/puppet-load.rb', line 286

def add(index, conn)
  @requests.push(conn)

  conn.stream { |data|
    @sizes[index] += data.length
  }

  conn.callback {
    @times[index] = Time.now - @times[index]
    code = conn.response_header.status
    if code >= 200 && code < 300
      Puppet.debug("Client #{index} finished successfully")
      @responses[:succeeded].push(conn)
    else
      Puppet.debug("Client #{index} finished with HTTP code #{code}")
      @responses[:failed].push(conn)
    end
    check_progress
  }

  conn.errback {
    Puppet.debug("Client #{index} finished with an error: #{conn.error}")
    @times[index] = Time.now - @times[index]
    @responses[:failed].push(conn)
    check_progress
  }
end

#all_responsesObject



314
315
316
# File 'ext/puppet-load.rb', line 314

def all_responses
  @responses[:succeeded] + @responses[:failed]
end

#spawn_request(index) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'ext/puppet-load.rb', line 267

def spawn_request(index)
  @times[index] = Time.now
  @sizes[index] = 0
  nodeidx = index % $options[:node].size
  node = $options[:node][nodeidx]
  EventMachine::HttpRequest.new("https://#{$options[:server]}:#{$options[:masterport]}/production/catalog/#{node}").get(
  :port => $options[:masterport],
  :query => @parameters[nodeidx],
  :timeout => $options[:timeout],
  :head => { "Accept" => "pson, yaml, b64_zlib_yaml, marshal, dot, raw", "Accept-Encoding" => "gzip, deflate" },
  :ssl => { :private_key_file => $options[:key],
            :cert_chain_file => $options[:cert],
            :verify_peer => false } ) do
      @times[index] = Time.now
      @sizes[index] = 0
      Puppet.debug("starting client #{index} for #{node}")
  end
end