Skip to content

Instantly share code, notes, and snippets.

@weeyum
Last active June 9, 2016 20:10
Show Gist options
  • Select an option

  • Save weeyum/bf394a827aaebb344691 to your computer and use it in GitHub Desktop.

Select an option

Save weeyum/bf394a827aaebb344691 to your computer and use it in GitHub Desktop.
kill redis connections
for_real = false
idle_max = 86400
# redis_constant is a connection pool, the variable c is a single redis client
(clients = redis_constant.with { |c| c.method_missing("client", "list") }.split("\n")).nil?
(addrs = clients.
# select idle clients
select { |client_string| client_string.match(/idle=([\d|\.|:]*)/).captures.first.to_i > idle_max }.
# return ip addresses
map { |client_string| client_string.match(/addr=([\d|\.|:]*)/).captures.first }).nil?
puts "found #{addrs.length} clients with idle time over #{idle_max}"
if !!for_real
killed_total = 0
(addrs.each do |addr|
killed_total += 1
puts "killed #{killed_total} / #{addrs.length} so far.."
redis_constant.with { |c| c.method_missing("client", "kill", addr) }
end).nil?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment