On Concurrency in Go HTTP Servers

On Concurrency in Go HTTP Servers

We start by defining a “counter manager” which is a background goroutine with access to a closure that stores the actual data: Instead of accessing the map of counters directly, handlers will send s on a channel and will receive replies on a reply channel they provide. For the former, we can add some middleware (full code here): // limitNumClients is HTTP handling middleware that ensures no more than // maxClients requests are passed concurrently to the given handler f. And wrap a handler using it: // Limit to max 10 connections for this handler. Its method actually serves a connection, pretty much as you would expect; it reads some data from the client and invokes the user-suppied handler depending on the path.

Source: eli.thegreenplace.net