Skip to content

Instantly share code, notes, and snippets.

@ellismarte
Last active September 16, 2016 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ellismarte/713042ba0a2bece0957318239d8fb55d to your computer and use it in GitHub Desktop.
Save ellismarte/713042ba0a2bece0957318239d8fb55d to your computer and use it in GitHub Desktop.
require 'manticore'
class SfdcHTTP
attr_accessor :expiration_date, :client
def initialize
@client ||= set_client
@expiration_date = nil
end
def self.get_client
if @expiration_date == nil
self.set_client
@client
elsif Time.now - @expiration_date < 240
@client
else
self.set_client
@client
end
end
def self.set_client
@expiration_date = Time.now
@client = Manticore::Client.new(
keepalive: true,
pool_max: 50,
pool_max_per_route: 50,
ssl: { verify: :disable }
)
@client
end
def self.call_sfdc(n)
if n == 4
set_client
end
puts "##{n}'s request client: #{@client}"
response = @client.get(
'http://localhost:3000'
).call
puts "##{n}s response client: #{@client}, response body: #{response.body}\n"
end
def self.spin_up(n_connections)
set_client
threads = []
n_connections.times do |n|
threads << Thread.new do
call_sfdc(n)
end
end
threads.each do |thread|
thread.join
end
end
end
puts SfdcHTTP.spin_up(10)
#1's request client: #<Manticore::Client:0x15ff3e9e>
#2's request client: #<Manticore::Client:0x15ff3e9e>
#0's request client: #<Manticore::Client:0x15ff3e9e>
#5's request client: #<Manticore::Client:0x15ff3e9e>
#3's request client: #<Manticore::Client:0x15ff3e9e>
#6's request client: #<Manticore::Client:0x15ff3e9e>
#7's request client: #<Manticore::Client:0x15ff3e9e>
#8's request client: #<Manticore::Client:0x15ff3e9e>
#9's request client: #<Manticore::Client:0x15ff3e9e>
#4's request client: #<Manticore::Client:0x4d72f551>
#9s response client: #<Manticore::Client:0x4d72f551>, response body: Hello world
#5s response client: #<Manticore::Client:0x4d72f551>, response body: Hello world
#4s response client: #<Manticore::Client:0x4d72f551>, response body: Hello world
#6s response client: #<Manticore::Client:0x4d72f551>, response body: Hello world
#2s response client: #<Manticore::Client:0x4d72f551>, response body: Hello world
#1s response client: #<Manticore::Client:0x4d72f551>, response body: Hello world
#0s response client: #<Manticore::Client:0x4d72f551>, response body: Hello world
#7s response client: #<Manticore::Client:0x4d72f551>, response body: Hello world
#8s response client: #<Manticore::Client:0x4d72f551>, response body: Hello world
#3s response client: #<Manticore::Client:0x4d72f551>, response body: Hello world
var express = require('express');
var app = express();
var port = 3000;
app.get('/', function(req, res){
setTimeout(function(){res.send('Hello world')}, 1000)
})
app.listen(port, function(){
console.log('listening on port 3000')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment