Last active
June 9, 2016 20:10
-
-
Save weeyum/bf394a827aaebb344691 to your computer and use it in GitHub Desktop.
kill redis connections
This file contains hidden or 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
| 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