This commit is contained in:
gospider
2025-03-26 14:18:21 +08:00
parent 8250e55d6d
commit b673b6aeca

45
http.go
View File

@@ -128,6 +128,21 @@ func (obj *clientConn) CloseWithError(err error) error {
} }
return obj.conn.Close() return obj.conn.Close()
} }
func (obj *clientConn) initTask(req *http.Request, orderHeaders []interface {
Key() string
Val() any
}) {
readCtx, readCnl := context.WithCancelCause(obj.closeCtx)
writeCtx, writeCnl := context.WithCancel(obj.closeCtx)
obj.task = &httpTask{
readCtx: readCtx,
readCnl: readCnl,
writeCtx: writeCtx,
writeCnl: writeCnl,
req: req,
orderHeaders: orderHeaders,
}
}
func (obj *clientConn) DoRequest(req *http.Request, orderHeaders []interface { func (obj *clientConn) DoRequest(req *http.Request, orderHeaders []interface {
Key() string Key() string
Val() any Val() any
@@ -142,27 +157,33 @@ func (obj *clientConn) DoRequest(req *http.Request, orderHeaders []interface {
if obj.task != nil { if obj.task != nil {
select { select {
case <-obj.task.writeCtx.Done(): case <-obj.task.writeCtx.Done():
case <-obj.ctx.Done():
return nil, nil, obj.ctx.Err()
case <-obj.closeCtx.Done():
return nil, nil, obj.closeCtx.Err()
default: default:
return nil, obj.task.readCtx, errLastTaskRuning return nil, obj.task.readCtx, errLastTaskRuning
} }
select { select {
case <-obj.task.readCtx.Done(): case <-obj.task.readCtx.Done():
case <-obj.ctx.Done():
return nil, nil, obj.ctx.Err()
case <-obj.closeCtx.Done():
return nil, nil, obj.closeCtx.Err()
default:
return nil, obj.task.readCtx, errLastTaskRuning
}
} else {
select {
case <-obj.ctx.Done():
return nil, nil, obj.ctx.Err()
case <-obj.closeCtx.Done():
return nil, nil, obj.closeCtx.Err()
default: default:
return nil, obj.task.readCtx, errLastTaskRuning return nil, obj.task.readCtx, errLastTaskRuning
} }
} }
readCtx, readCnl := context.WithCancelCause(obj.closeCtx) obj.initTask(req, orderHeaders)
writeCtx, writeCnl := context.WithCancel(obj.closeCtx)
obj.task = &httpTask{
readCtx: readCtx,
readCnl: readCnl,
writeCtx: writeCtx,
writeCnl: writeCnl,
req: req,
orderHeaders: orderHeaders,
}
obj.send() obj.send()
if obj.task.err != nil { if obj.task.err != nil {
obj.CloseWithError(obj.task.err) obj.CloseWithError(obj.task.err)