前面有一件事:如果您正在运行HTTP服务器(无论如何都是Go的标准服务器),则无法停止并重新启动服务器就无法控制goroutine的数量。每个请求至少启动一个goroutine,并且你无能为力。好消息是,这通常不是问题,因为goroutine非常轻巧。然而,你想保持正在努力工作的goroutines的数量是完全合理的。
您可以将任何值放入通道中,包括函数。因此,如果目标是只需要在http处理程序中编写代码,那么应该关闭这些工作 - 工作人员不知道(或关心)他们正在处理的是什么。
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
var largePool chan func()
var smallPool chan func()
func main() {
// Start two different sized worker pools (e.g., for different workloads).
// Cancelation and graceful shutdown omited for brevity.
largePool = make(chan func(), 100)
smallPool = make(chan func(), 10)
for i := 0; i < 100; i++ {
go func() {
for f := range largePool {
f()
}
}()
}
for i := 0; i < 10; i++ {
go func() {
for f := range smallPool {
f()
}
}()
}
http.HandleFunc("/endpoint-1", handler1)
http.HandleFunc("/endpoint-2", handler2) // naming things is hard, okay?
http.ListenAndServe(":8080", nil)
}
func handler1(w http.ResponseWriter, r *http.Request) {
// Imagine a JSON body containing a URL that we are expected to fetch.
// Light work that doesn't consume many of *our* resources and can be done
// in bulk, so we put in in the large pool.
var job struct{ URL string }
if err := json.NewDecoder(r.Body).Decode(&job); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
go func() {
largePool <- func() {
http.Get(job.URL)
// Do something with the response
}
}()
w.WriteHeader(http.StatusAccepted)
}
func handler2(w http.ResponseWriter, r *http.Request) {
// The request body is an image that we want to do some fancy processing
// on. That's hard work; we don't want to do too many of them at once, so
// so we put those jobs in the small pool.
b, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go func() {
smallPool <- func() {
processImage(b)
}
}()
w.WriteHeader(http.StatusAccepted)
}
func processImage(b []byte) {}
这是一个非常简单的例子来说明问题。设置工作池的方式并不重要。你只需要一个聪明的工作定义。在上面的例子中它是一个闭包,但是你也可以定义一个Job接口。现在
type Job interface {
Do()
}
var largePool chan Job
var smallPool chan Job
,我不会把整个工作池方法 “简单”。你说你的目标是限制goroutines(正在工作)的数量。这根本不需要工人;它只需要一个限制器。这和上面的例子是一样的,但是使用通道作为信号来限制并发。
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
var largePool chan struct{}
var smallPool chan struct{}
func main() {
largePool = make(chan struct{}, 100)
smallPool = make(chan struct{}, 10)
http.HandleFunc("/endpoint-1", handler1)
http.HandleFunc("/endpoint-2", handler2)
http.ListenAndServe(":8080", nil)
}
func handler1(w http.ResponseWriter, r *http.Request) {
var job struct{ URL string }
if err := json.NewDecoder(r.Body).Decode(&job); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
go func() {
// Block until there are fewer than cap(largePool) light-work
// goroutines running.
largePool <- struct{}{}
defer func() { <-largePool }() // Let everyone that we are done
http.Get(job.URL)
}()
w.WriteHeader(http.StatusAccepted)
}
func handler2(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go func() {
// Block until there are fewer than cap(smallPool) hard-work
// goroutines running.
smallPool <- struct{}{}
defer func() { <-smallPool }() // Let everyone that we are done
processImage(b)
}()
w.WriteHeader(http.StatusAccepted)
}
func processImage(b []byte) {}
Go的http包为每个传入连接启动一个go例程。除非你在谈论后台工作处理,否则这似乎是浪费精力。 – squiguy
是的,这是为了后台处理。有些人可能需要一段时间才能完成,我宁愿不让一个不受控制的goroutines宽松 –
goroutines有什么问题?它们基本上是内置异步支持的jobqueue实现。 –