A simple solution is to have a channel of type struct{} of some bound, say 10. A process only gives service as long as it can pull a message from the channel.
More advanced solutions include handling the channel as a token bucket and regulating it to have a drip-rate. Even more advanced solutions sample your system and slows the drip feed when it is overloaded with resources. You might also consider pushback and queuing. If you are overloaded, you have to tell the caller to stop sending requests for a while. Consider using a model such as CoDel (Controlled Delay) on your queues. A more holistic view: Read google's SRE handbook which has some chapters on this. Adapt to your size as needed. On Fri, Feb 2, 2018 at 11:30 AM Patrik Iselind <patrik....@gmail.com> wrote: > Hi, > > If i define a webserver as such (taken from the docs) > > http.Handle("/foo", fooHandler) > > http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { > fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) > }) > > log.Fatal(http.ListenAndServe(":8080", nil)) > > > How do i control how many simultaneous requests my server may handle? > > As i understand it, the handlers get spawned off as separate go routines. > I'm looking for a way to limit the number of such go routines. > > I would like to limit the effects of getting a massive storm of requests > in a short amount of time which might chew up all RAM/CPU. > > // Patrik > > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.