chore(net): remove unnecessary goroutine

This commit is contained in:
j2rong4cn
2025-07-01 00:28:26 +08:00
parent 52c93f2046
commit 862b1c3c53
2 changed files with 17 additions and 7 deletions

View File

@ -156,7 +156,6 @@ func (d *downloader) download() (io.ReadCloser, error) {
if err := d.concurrencyCheck(); err != nil {
return nil, err
}
d.ctx, d.cancel = context.WithCancelCause(d.ctx)
maxPart := int(d.params.Range.Length / int64(d.cfg.PartSize))
if d.params.Range.Length%int64(d.cfg.PartSize) > 0 {
@ -172,18 +171,26 @@ func (d *downloader) download() (io.ReadCloser, error) {
log.Debugf("cfgConcurrency:%d", d.cfg.Concurrency)
if maxPart == 1 {
if d.cfg.ConcurrencyLimit != nil {
go func() {
<-d.ctx.Done()
d.concurrencyFinish()
}()
}
resp, err := d.cfg.HttpClient(d.ctx, d.params)
if err != nil {
d.concurrencyFinish()
return nil, err
}
closeFunc := resp.Body.Close
resp.Body = utils.NewReadCloser(resp.Body, func() error {
d.m.Lock()
defer d.m.Unlock()
if closeFunc != nil {
d.concurrencyFinish()
err := closeFunc()
closeFunc = nil
return err
}
return nil
})
return resp.Body, nil
}
d.ctx, d.cancel = context.WithCancelCause(d.ctx)
// workers
d.chunkChannel = make(chan chunk, d.cfg.Concurrency)

View File

@ -197,6 +197,9 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time
if r.Method != "HEAD" {
written, err := utils.CopyWithBufferN(w, sendContent, sendSize)
if err != nil {
if errors.Is(context.Cause(ctx), context.Canceled) {
return nil
}
log.Warnf("ServeHttp error. err: %s ", err)
if written != sendSize {
log.Warnf("Maybe size incorrect or reader not giving correct/full data, or connection closed before finish. written bytes: %d ,sendSize:%d, ", written, sendSize)