Limiting the Number of Requests
Multiple clients may make their requests to a server all at once, which can result in a massive number of requests that burden the server. This lesson brings the solution to this problem: a workaround in Go.
We'll cover the following
Bounding requests processed concurrently
This is easily accomplished using a channel with a buffer, whose capacity is the maximum number of concurrent requests. The following program does nothing useful, but contains the technique to bound the requests. No more than MAXREQS
requests will be handled and processed simultaneously because, when the buffer of the channel sem
is full, the function handle blocks and no other requests can start until a request is removed from sem
. The sem
acts like a semaphore that is a technical term for a flag variable in a program that signals a certain condition.
Get hands-on with 1400+ tech skills courses.