package main
import (
"fmt"
"net/http"
"runtime"
//"time"
)
var (
msg string
ch = make(chan bool)
)
func receive(w http.ResponseWriter, r *http.Request) {
fmt.Println("正在接收")
<-ch
fmt.Fprint(w, msg)
fmt.Println("接收完毕 :", msg)
}
func send(w http.ResponseWriter, r *http.Request) {
msg = r.URL.RawQuery
close(ch)
fmt.Println("关闭通道")
//time.Sleep(2 * time.Second)
fmt.Println("重置通道")
ch = make(chan bool)
}
func main() {
http.Handle("/", http.FileServer(http.Dir("")))
http.HandleFunc("/receive", receive)
http.HandleFunc("/send", send)
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {})
fmt.Println("服务开启")
http.ListenAndServe(":8080", nil)
}
----------
我想让send之后, 堵塞在receive的goroutine全都恢复过来, 这样写只有一个恢复.
如果 time.Sleep 没注释掉的话, receive 不阻塞了, 但是我 js 写的是 receive 的请求一返回就立刻再次发送请求, 所以会一直发请求, 一直接收, 直到 Sleep 完才堵塞, 停止, 这样不行.
为什么注释掉只有一个goroutine恢复?
有疑问加站长微信联系(非本文作者)