This commit is contained in:
gospider
2024-12-15 20:07:15 +08:00
parent 8493aecc83
commit 80f4be585c
6 changed files with 32 additions and 22 deletions

33
conn.go
View File

@@ -137,19 +137,6 @@ func (obj *connecotr) httpReq(task *reqTask, done chan struct{}) {
close(done)
}
func (obj *connecotr) waitBodyClose() error {
select {
case <-obj.bodyCtx.Done(): //wait body close
if err := context.Cause(obj.bodyCtx); errors.Is(err, errGospiderBodyClose) {
return nil
} else {
return err
}
case <-obj.forceCtx.Done(): //force conn close
return tools.WrapError(context.Cause(obj.forceCtx), "connecotr force close")
}
}
func (obj *connecotr) taskMain(task *reqTask) (retry bool) {
defer func() {
if task.err != nil && task.option.ErrCallBack != nil {
@@ -161,13 +148,27 @@ func (obj *connecotr) taskMain(task *reqTask) (retry bool) {
if retry {
task.err = nil
obj.rawConn.CloseWithError(errors.New("taskMain retry close"))
if task.res != nil && task.res.Body != nil {
task.res.Body.Close()
}
} else {
task.cnl()
if task.err == nil && task.res != nil && task.res.Body != nil {
select {
case <-obj.bodyCtx.Done(): //wait body close
if task.err = context.Cause(obj.bodyCtx); !errors.Is(task.err, errGospiderBodyClose) {
task.err = tools.WrapError(task.err, "bodyCtx close")
}
case <-task.req.Context().Done(): //wait request close
task.err = tools.WrapError(context.Cause(task.req.Context()), "requestCtx close")
case <-obj.forceCtx.Done(): //force conn close
task.err = tools.WrapError(context.Cause(obj.forceCtx), "connecotr force close")
}
}
if task.err != nil {
obj.rawConn.CloseWithError(task.err)
} else {
if err := obj.waitBodyClose(); err != nil {
obj.rawConn.CloseWithError(err)
if task.res != nil && task.res.Body != nil {
task.res.Body.Close()
}
}
}

4
go.mod
View File

@@ -7,8 +7,8 @@ require (
github.com/gospider007/bs4 v0.0.0-20241205092056-32204f68d82e
github.com/gospider007/gson v0.0.0-20240912023741-2238f9748e4a
github.com/gospider007/gtls v0.0.0-20240527084326-e580531eb89e
github.com/gospider007/http2 v0.0.0-20241215075940-632d27d19c93
github.com/gospider007/http3 v0.0.0-20241214104411-f9a6d924a254
github.com/gospider007/http2 v0.0.0-20241215120203-9ee063748247
github.com/gospider007/http3 v0.0.0-20241215120136-980caa047c47
github.com/gospider007/ja3 v0.0.0-20240620005139-f0602f169903
github.com/gospider007/re v0.0.0-20240227100911-e27255e48eff
github.com/gospider007/tools v0.0.0-20241120013952-ff42051bfc9f

4
go.sum
View File

@@ -109,8 +109,12 @@ github.com/gospider007/http2 v0.0.0-20241214105312-2cf2a243a9eb h1:BqfS6+kMHYPoR
github.com/gospider007/http2 v0.0.0-20241214105312-2cf2a243a9eb/go.mod h1:0A43UUydE6EB5QlgYiRyPlidwN3e0faIGEEaI8mxYeU=
github.com/gospider007/http2 v0.0.0-20241215075940-632d27d19c93 h1:GpHEydQZfXxH+uHbP87JtfNKe6MVZ5ygwsmqmE1E1aQ=
github.com/gospider007/http2 v0.0.0-20241215075940-632d27d19c93/go.mod h1:0A43UUydE6EB5QlgYiRyPlidwN3e0faIGEEaI8mxYeU=
github.com/gospider007/http2 v0.0.0-20241215120203-9ee063748247 h1:OaW1iVBa0GdYE5CILbO/40nF/Rtktv1TCXh9muCw08s=
github.com/gospider007/http2 v0.0.0-20241215120203-9ee063748247/go.mod h1:0A43UUydE6EB5QlgYiRyPlidwN3e0faIGEEaI8mxYeU=
github.com/gospider007/http3 v0.0.0-20241214104411-f9a6d924a254 h1:qur79Bvjm07E11mcdXFrvtGW9HKo0ZUB7qn5TJmx4v8=
github.com/gospider007/http3 v0.0.0-20241214104411-f9a6d924a254/go.mod h1:dCygjc5CZHjrAOYZkdGcwHTZV8w4DbTQkbuTEIl7jMg=
github.com/gospider007/http3 v0.0.0-20241215120136-980caa047c47 h1:HYYKcKHPFqpjFjEjwceE8hi2lxQ+EZtMLFJG4KEr9NY=
github.com/gospider007/http3 v0.0.0-20241215120136-980caa047c47/go.mod h1:dCygjc5CZHjrAOYZkdGcwHTZV8w4DbTQkbuTEIl7jMg=
github.com/gospider007/ja3 v0.0.0-20240620005139-f0602f169903 h1:elWGt/rRpoPLc7dsnC8axzXnzAAyNdrtOzOgJ3OxisY=
github.com/gospider007/ja3 v0.0.0-20240620005139-f0602f169903/go.mod h1:coutudbhlLqA/xpGB+IFKUGKkzivbYO/Ghl4e0N6hmI=
github.com/gospider007/kinds v0.0.0-20240929092451-8f867acde255 h1:X+AM/mgmh/EfyQUjKZp1VFc9TSlrhkwS0eSYeo5fMs4=

View File

@@ -292,6 +292,7 @@ func (obj *Response) ReadBody() (err error) {
}
obj.readBody = true
bBody := bytes.NewBuffer(nil)
if obj.requestOption.Bar && obj.ContentLength() > 0 {
_, err = io.Copy(&barBody{
bar: bar.NewClient(obj.response.ContentLength),
@@ -300,6 +301,7 @@ func (obj *Response) ReadBody() (err error) {
} else {
_, err = io.Copy(bBody, obj.Body())
}
if obj.requestOption.Logger != nil {
obj.requestOption.Logger(Log{
Id: obj.requestOption.requestId,

View File

@@ -108,7 +108,9 @@ func (obj *roundTripper) http3Dial(option *RequestOption, req *http.Request) (co
return
}
conn = obj.newConnecotr()
conn.rawConn = http3.NewClient(netConn)
conn.rawConn = http3.NewClient(netConn, func() {
conn.forceCnl(errors.New("http3 client close"))
})
return
}
@@ -121,7 +123,9 @@ func (obj *roundTripper) ghttp3Dial(option *RequestOption, req *http.Request) (c
return
}
conn = obj.newConnecotr()
conn.rawConn = http3.NewUClient(netConn)
conn.rawConn = http3.NewUClient(netConn, func() {
conn.forceCnl(errors.New("http3 client close"))
})
return
}

3
rw.go
View File

@@ -36,8 +36,7 @@ var errGospiderBodyClose = errors.New("gospider body close error")
func (obj *readWriteCloser) Close() (err error) {
obj.conn.bodyCnl(errGospiderBodyClose)
err = obj.body.Close() //reuse conn
return
return obj.body.Close() //reuse conn
}
// safe close conn