open_sockets.coffee 1.0 KB

12345678910111213141516171819202122232425262728
  1. URL = require "url"
  2. seconds = 1000
  3. # In Node 0.10 the default is 5, which means only 5 open connections at one.
  4. # Node 0.12 has a default of Infinity. Make sure we have no limit set,
  5. # regardless of Node version.
  6. require("http").globalAgent.maxSockets = Infinity
  7. require("https").globalAgent.maxSockets = Infinity
  8. module.exports = OpenSocketsMonitor =
  9. monitor: (logger) ->
  10. interval = setInterval () ->
  11. OpenSocketsMonitor.gaugeOpenSockets()
  12. , 5 * seconds
  13. Metrics = require "./metrics"
  14. Metrics.registerDestructor () ->
  15. clearInterval(interval)
  16. gaugeOpenSockets: () ->
  17. Metrics = require "./metrics"
  18. for url, agents of require('http').globalAgent.sockets
  19. url = URL.parse("http://#{url}")
  20. hostname = url.hostname?.replace(/\./g, "_")
  21. Metrics.gauge "open_connections.http.#{hostname}", agents.length
  22. for url, agents of require('https').globalAgent.sockets
  23. url = URL.parse("https://#{url}")
  24. hostname = url.hostname?.replace(/\./g, "_")
  25. Metrics.gauge "open_connections.https.#{hostname}", agents.length