For a chat bot I'm refactoring to not require locks for managing most of its state, the website it connects to via websocket throttles messages that can be received from regular users to a rate of 0.6s and voiced users to a rate of 0.3s, while administrators have no throttle. Whether or not a user is voiced or an administrator isn't known until some point well after the connection gets made; before then, everyone is considered a regular user.
Currently, I handle throttled messages by putting the react
block for listening for messages in a loop that exits once the connection has been forcibly closed. When the throttle gets updated, I call done
to enter the next iteration, which updates the whenever
block for the supply for messages to send to have the updated throttle. This is terrible, racy code!
What can I do to (a) ensure the connection starts out with a 0.3s throttle that can be used immediately after a websocket connection gets made, (b) make it possible to call a method that updates this throttle when needed, and (c) not keep any state related to this throttle (which can be inferred by other means)?
Edit: I forgot to mention this earlier, but there are unthrottled messages for all types of users, not just administrators.
Supply.throttle
, emitting"limit:..."
to the control supply changes how many messages can be passed through at a time, not the interval. – Aryanize