考虑以下代码块,预计会因达到缓冲区限制而引发错误:
package main
import (
"fmt"
"time"
)
func main() {
burstyLimiter := make(chan time.Time, 4)
for i := 0; i < 5; i++ {
burstyLimiter <- time.Now()
fmt.Println("adding to burstyLimiter")
}
}
但是,当我尝试使用以下确切块时,它会阻塞而不是抛出错误。我想了解这种行为。谢谢你的时间。
package main
import (
"fmt"
"time"
)
func main() {
requests := make(chan int, 5)
close(requests)
limiter := time.Tick(time.Millisecond * 200)
for req := range requests {
<-limiter
fmt.Println("request", req, time.Now())
}
burstyLimiter := make(chan time.Time, 4)
for i := 0; i < 5; i++ {
burstyLimiter <- time.Now()
fmt.Println("adding to burstyLimiter")
}
}
九州编程
相关分类