mirror of
https://github.com/gospider007/requests.git
synced 2025-12-24 13:57:52 +08:00
sync
This commit is contained in:
4
conn.go
4
conn.go
@@ -90,7 +90,9 @@ func (obj *connPool) taskMain(conn *connecotr, task *reqTask) (err error) {
|
||||
case <-conn.forceCtx.Done():
|
||||
err = context.Cause(conn.forceCtx)
|
||||
case <-task.reqCtx.Context().Done():
|
||||
err = context.Cause(task.reqCtx.Context())
|
||||
if context.Cause(task.reqCtx.Context()) != errNoErr {
|
||||
err = context.Cause(task.reqCtx.Context())
|
||||
}
|
||||
case <-task.bodyCtx.Done():
|
||||
if context.Cause(task.bodyCtx) != errNoErr {
|
||||
err = context.Cause(task.bodyCtx)
|
||||
|
||||
@@ -142,7 +142,7 @@ func (obj *Client) retryRequest(ctx context.Context, option RequestOption, uhref
|
||||
if err != nil || loc == nil {
|
||||
return
|
||||
}
|
||||
response.Close()
|
||||
response.Close(nil)
|
||||
switch response.StatusCode() {
|
||||
case 307, 308:
|
||||
if response.Option().readOne {
|
||||
@@ -244,9 +244,10 @@ func (obj *Client) request(ctx *Response) (err error) {
|
||||
}
|
||||
//init ctx,cnl
|
||||
if ctx.option.Timeout > 0 { //超时
|
||||
ctx.ctx, ctx.cnl = context.WithTimeout(ctx.Context(), ctx.option.Timeout)
|
||||
ctx.ctx, _ = context.WithTimeout(ctx.ctx, ctx.option.Timeout)
|
||||
ctx.ctx, ctx.cnl = context.WithCancelCause(ctx.ctx)
|
||||
} else {
|
||||
ctx.ctx, ctx.cnl = context.WithCancel(ctx.Context())
|
||||
ctx.ctx, ctx.cnl = context.WithCancelCause(ctx.Context())
|
||||
}
|
||||
var isWebsocket bool
|
||||
//init Scheme
|
||||
|
||||
14
response.go
14
response.go
@@ -63,7 +63,7 @@ type Response struct {
|
||||
response *http.Response
|
||||
webSocket *websocket.Conn
|
||||
sse *SSE
|
||||
cnl context.CancelFunc
|
||||
cnl context.CancelCauseFunc
|
||||
option *RequestOption
|
||||
client *Client
|
||||
encoding string
|
||||
@@ -316,10 +316,10 @@ func (obj *Response) ReadBody() (err error) {
|
||||
return nil
|
||||
}
|
||||
defer func() {
|
||||
obj.Close(err)
|
||||
if err != nil {
|
||||
obj.CloseConn()
|
||||
} else {
|
||||
obj.Close()
|
||||
if obj.response.StatusCode == 101 && obj.webSocket == nil {
|
||||
obj.webSocket = websocket.NewConn(newFakeConn(obj.body.connStream()), func() { obj.CloseConn() }, true, obj.Headers().Get("Sec-WebSocket-Extensions"))
|
||||
}
|
||||
@@ -374,14 +374,22 @@ func (obj *Response) Proxys() []Address {
|
||||
|
||||
// close
|
||||
func (obj *Response) CloseConn() {
|
||||
obj.Close(errors.New("response close conn"))
|
||||
if obj.body != nil {
|
||||
obj.body.CloseConn()
|
||||
}
|
||||
}
|
||||
|
||||
// close
|
||||
func (obj *Response) Close() {
|
||||
func (obj *Response) Close(err error) {
|
||||
if obj.body != nil {
|
||||
obj.body.Close()
|
||||
}
|
||||
if obj.cnl != nil {
|
||||
if err == nil {
|
||||
obj.cnl(errNoErr)
|
||||
} else {
|
||||
obj.cnl(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user