Unexpected behaviour due to non atomic API calls #5293
Replies: 1 comment 1 reply
-
This is definitely a valid point. It looks like the limit on requests to As the |
Beta Was this translation helpful? Give feedback.
-
This is definitely a valid point. It looks like the limit on requests to As the |
Beta Was this translation helpful? Give feedback.
-
Some library methods can result in unexpected behavior based on undocumented internal calls and API behavior. There surely are points to be made for documentation to prevent confusing library users. However, I'm presently not too sure if this can be feasibly done without the documentation turning into confusing clutter.
One example of such a method is
GuildMemberRoleManger#add
(and accordingly remove). Provided with multiple roles this method internally callsGuildMemberRoleManager#set
discord.js/src/managers/GuildMemberRoleManager.js
Lines 88 to 89 in 8a7abc9
Which in itself calls
GuildMember#edit
discord.js/src/managers/GuildMemberRoleManager.js
Lines 150 to 152 in 8a7abc9
Which does a PATCH api call at
/guilds/{guild.id}/members/{user.id}
This call now is not atomic. If discord receives multiple patch calls to a specific member in a short timeframe (either by using #remove and #add back to back without sequencing the calls and handling the returned Promises accordingly - or even by different bots having the job to set or restore roles when a member joins the guild).
The API's principle of eventual consistency can here cause unwanted outcomes, making it appear as if member updates were disregarded.
@Wittiest has brought up that discord.py solves this by allowing an option to do atomic updates instead. (link). This flag causes the provided roles to instead be iterated and n calls to the
PUT /guilds/{guild.id}/members/{user.id}/roles/{role.id}
endpoint to be made.I personally am unsure if we should go this route, making multiple API calls where the user might not expect them, leading to the end-user being unaware of depleting rate-limits much quicker than expected, possibly causing major unexpected delays in the program flow.
Original point made by @Wittiest:
Beta Was this translation helpful? Give feedback.
All reactions