This commit is contained in:
gospider
2025-06-30 20:05:01 +08:00
parent aec0c7aa25
commit 681f52dc22
3 changed files with 18 additions and 7 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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)
}
}
}