Last active
February 16, 2020 09:26
-
-
Save SamSaffron/7295c67c90d7b38d043c3a85b193a25c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'pg', '1.2.2' | |
end | |
$max_time = 2 | |
Thread.new do | |
begin | |
while true | |
Thread.list.each do |t| | |
if (time = t["t"]) && time < Time.now - ($max_time*20) | |
puts "Thread #{t} appears stalled" | |
end | |
end | |
sleep 1 | |
end | |
rescue => e | |
STDERR.puts "Crashed monitor #{e}" | |
exit 1 | |
end | |
end | |
$connections = Queue.new | |
30.times do | |
$connections.push(PG::Connection.open(dbname: 'postgres')) | |
end | |
30.times do | |
$connections.push(PG::Connection.open(dbname: 'discourse_development')) | |
end | |
$iterations = 0 | |
(0...60).map do | |
Thread.new do | |
while true | |
Thread.current["t"] = Time.now | |
conn = $connections.pop | |
conn.async_exec("select pg_sleep($1)", [rand * $max_time]).to_a | |
$connections.push(conn) | |
if ($iterations += 1) % 1000 == 0 | |
puts "#{$iterations} done" | |
end | |
end | |
end | |
end.each(&:join) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment