{"action":"created","issue":{"url":"https://api.github.com/repos/discordjs/discord.js/issues/2527","repository_url":"https://api.github.com/repos/discordjs/discord.js","labels_url":"https://api.github.com/repos/discordjs/discord.js/issues/2527/labels{/name}","comments_url":"https://api.github.com/repos/discordjs/discord.js/issues/2527/comments","events_url":"https://api.github.com/repos/discordjs/discord.js/issues/2527/events","html_url":"https://github.com/discordjs/discord.js/issues/2527","id":319563452,"number":2527,"title":"Error: socket hang up caused by rate limiting","user":{"login":"njb-said","id":2371386,"avatar_url":"https://avatars2.githubusercontent.com/u/2371386?v=4","gravatar_id":"","url":"https://api.github.com/users/njb-said","html_url":"https://github.com/njb-said","followers_url":"https://api.github.com/users/njb-said/followers","following_url":"https://api.github.com/users/njb-said/following{/other_user}","gists_url":"https://api.github.com/users/njb-said/gists{/gist_id}","starred_url":"https://api.github.com/users/njb-said/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/njb-said/subscriptions","organizations_url":"https://api.github.com/users/njb-said/orgs","repos_url":"https://api.github.com/users/njb-said/repos","events_url":"https://api.github.com/users/njb-said/events{/privacy}","received_events_url":"https://api.github.com/users/njb-said/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2018-05-02T13:57:06Z","updated_at":"2018-05-03T07:02:16Z","closed_at":null,"author_association":"NONE","body":"**Please describe the problem you are having in as much detail as possible:**\r\nReceiving socket hang up error when sending one message to multiple guilds.\r\nMy scenario is odd, the bot has a feature which sends a single message to guilds once per day at midnight UTC, all guilds which it sends to have enabled this feature, using a command to set the channel the message ends up in.\r\nSo for the first 10 or 20 messages it is absolutely fine, after that though I get an error like this:\r\n```js\r\n[2018-05-02T00:01:20.453Z] ERROR: bot/1 on f0f5ab0e0077: (err.code=ECONNRESET)\r\n    { discordServerId: '[redacted]',\r\n      discordServerName: '[redacted]' }\r\n    --\r\n    Error: socket hang up\r\n        at TLSSocket.onHangUp (_tls_wrap.js:1120:19)\r\n        at Object.onceWrapper (events.js:293:19)\r\n        at emitNone (events.js:91:20)\r\n        at TLSSocket.emit (events.js:188:7)\r\n        at endReadableNT (_stream_readable.js:975:12)\r\n        at _combinedTickCallback (internal/process/next_tick.js:80:11)\r\n        at process._tickDomainCallback (internal/process/next_tick.js:128:9)\r\n        at process.wrappedFunction (/src/node_modules/newrelic/lib/transaction/tracer/index.js:276:51)\r\n```\r\nAt the time of writing there are around 100 servers enabled with this feature, but this is growing daily.\r\nWhen the bot was being developed we tested this feature with a smaller number of guilds (about 15) and it worked fine, however it's harder to test at scale.\r\n\r\nI can only assume this is related to rate limiting, and the reason for not getting a 429 HTTP response is Cloudflare kicking in to protect discord and completely closing the connections - a behaviour I haven't seen from Cloudflare before.\r\nI was under the impression discord.js handles 429 errors by queuing the requests to be sent later, I also thought that rate limits were per-guild although I understand there is likely a global rate limit too to prevent flooding.\r\n\r\nIt is really hard to troubleshoot the issue as I have no way of gauging the actual rate limit (sent in headers iirc) or if I am getting too close.\r\n\r\n\r\nIn Discord I mentioned this issue and was told to add a delay between messages, this is fine by me although it would massively increase as more servers have this feature enabled.\r\nI was also told to come here to report the issue, hopefully a way to retrieve the rate limits could be added to the library?\r\n[This issue](https://github.com/discordjs/discord.js/issues/1176) suggested adding an event with rate limit information, this would be very useful.\r\n\r\n**Include a reproducible code sample here, if possible:**\r\nBit of a shoddy code sample, had to remove lots of application specific things like logging + database connections\r\n```js\r\n// You would need to add a bot variable to test this\r\n// Also will need a logger or remove it and replace with console.log\r\n\r\n// This uses bunyan as a logger\r\n// var logger = require('./logger');\r\nvar async = require('async');\r\nvar fileName = 'image-name.png';\r\nvar fullPath = 'path/to/file/' + fileName;\r\n\r\nvar embed = {\r\n    title: 'Test Message',\r\n    image: {\r\n        url: 'attachment://' + fileName\r\n    }\r\n};\r\n\r\n// arr is the array of discord servers with the feature enabled from a database\r\nvar arr = [{serverId: 'discord-guild-id', name: 'temporary name', channels: { shop: 'discord-channel-id' } } ];\r\n\r\nasync.each(arr, function(item, next) {\r\n    if(!item.channels.shop) {\r\n        logger.debug('[BOT] Skipping ' + item.serverId + ' no shop channel defined');\r\n        return next();\r\n    }\r\n\r\n    var g = bot.guilds.get(item.serverId);\r\n    if(!g) {\r\n        logger.debug('[BOT] Unable to find server matching ' + item.serverId);\r\n        return next();\r\n    }\r\n\r\n    var c = g.channels.get(item.channels.shop);\r\n    if(!c) {\r\n        logger.debug('[BOT] Unable to find channel ' + item.channels.shop + ' in ' + item.serverId);\r\n        return next();\r\n    }\r\n\r\n    c.send({embed: new _Discord.RichEmbed(embed), files: [ {attachment: fullPath, name: fileName } ]}).then((msg) => {\r\n        logger.debug('[BOT] Sent message to ' + item.name + ' (c:' + item.channels.shop + ')');\r\n        return next();\r\n    }).catch((err) => {\r\n        logger.error(err, {discordServerId: item.serverId});\r\n        // Commenting out for github issue\r\n        // bugsnag.notify(err, {discordServerId: item.serverId});\r\n        return next();\r\n    });\r\n}, function() {\r\n    logger.debug('[BOT] Finished sending messages');\r\n});\r\n```\r\n\r\n**Further details:**\r\n\r\n- discord.js version: 11.3.2\r\n- node.js version: 7.10.1\r\n- Operating system: Centos 7, node running in Docker\r\n- Priority this issue should have – please be realistic and elaborate if possible: low/medium\r\n\r\n<!--\r\nIf either of these applies to you, please check the respective checkbox: [ ] becomes [x].\r\nYou don't have to modify the text to suit your particular situation, if you want to\r\nelaborate please do so in the description.\r\nWhile it's not a requirement to test your issue on the master branch, it would make fixing\r\nthe problem a lot easier for us, so please do it if possible.\r\n-->\r\n- [ ] I found this issue while running code on a __user account__\r\n- [ ] I have also tested the issue on latest master, commit hash:\r\n"},"comment":{"url":"https://api.github.com/repos/discordjs/discord.js/issues/comments/386207008","html_url":"https://github.com/discordjs/discord.js/issues/2527#issuecomment-386207008","issue_url":"https://api.github.com/repos/discordjs/discord.js/issues/2527","id":386207008,"user":{"login":"BigBrainAFK","id":7173033,"avatar_url":"https://avatars2.githubusercontent.com/u/7173033?v=4","gravatar_id":"","url":"https://api.github.com/users/BigBrainAFK","html_url":"https://github.com/BigBrainAFK","followers_url":"https://api.github.com/users/BigBrainAFK/followers","following_url":"https://api.github.com/users/BigBrainAFK/following{/other_user}","gists_url":"https://api.github.com/users/BigBrainAFK/gists{/gist_id}","starred_url":"https://api.github.com/users/BigBrainAFK/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BigBrainAFK/subscriptions","organizations_url":"https://api.github.com/users/BigBrainAFK/orgs","repos_url":"https://api.github.com/users/BigBrainAFK/repos","events_url":"https://api.github.com/users/BigBrainAFK/events{/privacy}","received_events_url":"https://api.github.com/users/BigBrainAFK/received_events","type":"User","site_admin":false},"created_at":"2018-05-03T07:02:16Z","updated_at":"2018-05-03T07:02:16Z","author_association":"CONTRIBUTOR","body":"the delay didnt fix it cause its not the ratelimit"}}
{"actor":{"display_login":"BigBrainAFK"}}