Our approach then was a mixture of code instrumentation and throttling. Throttling, however, turned out to be tricky. We used to throttle all output indiscriminately at a certain rate (20 messages / second), essentially taxing all output to reduce the chance of lock-up in the most underpowered of our clients. As our users' applications got more complex and ambitious it became apparent that this handicapped our development environment and restricted a potential for high output applications.
Another complication was the introduction of Multiplayer: Now every program had multiple clients receiving output and sending input. So how do you build a system that works well for fast clients and degrades gracefully for slow clients?
To understand backpressure consider a scenario where you have a producer sending data to a consumer for processing. Often times the consumer is slower than the producer and might get flooded with messages before getting a chance at processing. At which point the consumer typically crashes. Backpressure is the mechanism in which the consumer tells the producer to slow the heck down!
In our case the producer is the your program and the consumer is your browser. If your program had an infinite loop with a
But this presents a new problem -- what if you want to interact with your program by providing keyboard or mouse input? If it's blocked then it can't handle your input. Enter bursts:
Bursts are periods for which the program can write freely without being constrained by the consumer. Every program starts out with a burst so that the initial set of output gets sent out to the client. Additional bursts happen with input. Every time you interact with your application you put it into burst mode so that it can catch up and be able to handle your input.
That way interactive programs remains interactive.
Now imagine coding with a group friends and one of them has a wooden laptop that can barely handle much output. At first the program will slow down to accommodate said friend. But say your friend's computer crashes and never processes any output ever -- what should we do? Sadly, we have to sacrifice the one to save the many: Simply disconnect your friend with the wooden computer.
while True: print("hello world")