Closed oscarwin closed 4 years ago
以下测试用例会触发这个错误,已经尝试修复。但是真正高并发情况下这个错误还是可能出现。并发情况下还是建议先调用Begin()
func TestIssue41(t *testing.T) {
c := NewHttpClient()
c.Begin().Get("http://httpbin.org")
c.Get("http://httpbin.org")
}
是的,对于同一个client不能混合使用两种方式,我现在是每个包里分别创建各自的 client 来避免这个问题。因为并不是所有的都需要并发,当然对于非并发的方式也可以采用先调用 Begin()
来解决这个问题。非并发其实可以看成并发为 1 的特殊场景。
在一个工程里如果 pkg A 使用了非并发的方式,而 pkg B 使用了并发的方式,会导致panic。前提是都使用 defaultClient。
panic 信息:
panic 原因: withLock 为 true,非并发方式也会调用 Unlock 导致 panic。
即使在 Unlock 后将 withLock 置为 false,也不能解决问题,似乎只能将并发方式与非并发的 client 分开,或者每次都创建新的 client?