Skip to content

Instantly share code, notes, and snippets.

View zanker's full-sized avatar

Zachary Anker zanker

  • San Francisco, CA
View GitHub Profile
if io = sock.accept_nonblock
client = Client.new io, @binder.env(sock)
if remote_addr_value
client.peerip = remote_addr_value
elsif remote_addr_header
client.remote_addr_header = remote_addr_header
end
pool << client
pool.wait_until_not_full unless queue_requests
def <<(work)
@mutex.synchronize do
if @shutdown
raise "Unable to add work while shutting down"
end
@todo << work
if @waiting < @todo.size and @spawned < @max
spawn_thread
# Thread 1
total = 0
loop do
total = total + 1
end
# Thread 2
response = HTTP.post("/users/create")
data = JSON.parse(response.body)
HTTP.post("/update-profile/#data['user_id']}")
Thread.new do
started_at = Time.now.to_f
sleep 1
report_time("thread.contention", (Time.now.to_f - started_at - 1) * 1000)
end
started_at = Time.now.to_f
response = HTTP.post("/users/create")
report_time("users.create", (Time.now.to_f - started_at) * 1000)
response
started_at = Time.now.to_f # 00:00
# Context switch to another thread
# 00:05, Context switch back 5 seconds later
response = HTTP.post("/users/create") # 00:06
report_time("users.create", (Time.now.to_f - started_at) * 1000) # 00:06