index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. module.exports = ForeverAgent
  2. ForeverAgent.SSL = ForeverAgentSSL
  3. var util = require('util')
  4. , Agent = require('http').Agent
  5. , net = require('net')
  6. , tls = require('tls')
  7. , AgentSSL = require('https').Agent
  8. function ForeverAgent(options) {
  9. var self = this
  10. self.options = options || {}
  11. self.requests = {}
  12. self.sockets = {}
  13. self.freeSockets = {}
  14. self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets
  15. self.minSockets = self.options.minSockets || ForeverAgent.defaultMinSockets
  16. self.on('free', function(socket, host, port) {
  17. var name = host + ':' + port
  18. if (self.requests[name] && self.requests[name].length) {
  19. self.requests[name].shift().onSocket(socket)
  20. } else if (self.sockets[name].length < self.minSockets) {
  21. if (!self.freeSockets[name]) self.freeSockets[name] = []
  22. self.freeSockets[name].push(socket)
  23. // if an error happens while we don't use the socket anyway, meh, throw the socket away
  24. var onIdleError = function() {
  25. socket.destroy()
  26. }
  27. socket._onIdleError = onIdleError
  28. socket.on('error', onIdleError)
  29. } else {
  30. // If there are no pending requests just destroy the
  31. // socket and it will get removed from the pool. This
  32. // gets us out of timeout issues and allows us to
  33. // default to Connection:keep-alive.
  34. socket.destroy()
  35. }
  36. })
  37. }
  38. util.inherits(ForeverAgent, Agent)
  39. ForeverAgent.defaultMinSockets = 5
  40. ForeverAgent.prototype.createConnection = net.createConnection
  41. ForeverAgent.prototype.addRequestNoreuse = Agent.prototype.addRequest
  42. ForeverAgent.prototype.addRequest = function(req, host, port) {
  43. var name = host + ':' + port
  44. if (this.freeSockets[name] && this.freeSockets[name].length > 0 && !req.useChunkedEncodingByDefault) {
  45. var idleSocket = this.freeSockets[name].pop()
  46. idleSocket.removeListener('error', idleSocket._onIdleError)
  47. delete idleSocket._onIdleError
  48. req._reusedSocket = true
  49. req.onSocket(idleSocket)
  50. } else {
  51. this.addRequestNoreuse(req, host, port)
  52. }
  53. }
  54. ForeverAgent.prototype.removeSocket = function(s, name, host, port) {
  55. if (this.sockets[name]) {
  56. var index = this.sockets[name].indexOf(s)
  57. if (index !== -1) {
  58. this.sockets[name].splice(index, 1)
  59. }
  60. } else if (this.sockets[name] && this.sockets[name].length === 0) {
  61. // don't leak
  62. delete this.sockets[name]
  63. delete this.requests[name]
  64. }
  65. if (this.freeSockets[name]) {
  66. var index = this.freeSockets[name].indexOf(s)
  67. if (index !== -1) {
  68. this.freeSockets[name].splice(index, 1)
  69. if (this.freeSockets[name].length === 0) {
  70. delete this.freeSockets[name]
  71. }
  72. }
  73. }
  74. if (this.requests[name] && this.requests[name].length) {
  75. // If we have pending requests and a socket gets closed a new one
  76. // needs to be created to take over in the pool for the one that closed.
  77. this.createSocket(name, host, port).emit('free')
  78. }
  79. }
  80. function ForeverAgentSSL (options) {
  81. ForeverAgent.call(this, options)
  82. }
  83. util.inherits(ForeverAgentSSL, ForeverAgent)
  84. ForeverAgentSSL.prototype.createConnection = createConnectionSSL
  85. ForeverAgentSSL.prototype.addRequestNoreuse = AgentSSL.prototype.addRequest
  86. function createConnectionSSL (port, host, options) {
  87. if (typeof port === 'object') {
  88. options = port;
  89. } else if (typeof host === 'object') {
  90. options = host;
  91. } else if (typeof options === 'object') {
  92. options = options;
  93. } else {
  94. options = {};
  95. }
  96. if (typeof port === 'number') {
  97. options.port = port;
  98. }
  99. if (typeof host === 'string') {
  100. options.host = host;
  101. }
  102. return tls.connect(options);
  103. }