mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-20 04:36:09 +08:00
chore(net): remove unnecessary goroutine
This commit is contained in:
@ -156,7 +156,6 @@ func (d *downloader) download() (io.ReadCloser, error) {
|
|||||||
if err := d.concurrencyCheck(); err != nil {
|
if err := d.concurrencyCheck(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
d.ctx, d.cancel = context.WithCancelCause(d.ctx)
|
|
||||||
|
|
||||||
maxPart := int(d.params.Range.Length / int64(d.cfg.PartSize))
|
maxPart := int(d.params.Range.Length / int64(d.cfg.PartSize))
|
||||||
if d.params.Range.Length%int64(d.cfg.PartSize) > 0 {
|
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)
|
log.Debugf("cfgConcurrency:%d", d.cfg.Concurrency)
|
||||||
|
|
||||||
if maxPart == 1 {
|
if maxPart == 1 {
|
||||||
if d.cfg.ConcurrencyLimit != nil {
|
|
||||||
go func() {
|
|
||||||
<-d.ctx.Done()
|
|
||||||
d.concurrencyFinish()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
resp, err := d.cfg.HttpClient(d.ctx, d.params)
|
resp, err := d.cfg.HttpClient(d.ctx, d.params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
d.concurrencyFinish()
|
||||||
return nil, err
|
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
|
return resp.Body, nil
|
||||||
}
|
}
|
||||||
|
d.ctx, d.cancel = context.WithCancelCause(d.ctx)
|
||||||
|
|
||||||
// workers
|
// workers
|
||||||
d.chunkChannel = make(chan chunk, d.cfg.Concurrency)
|
d.chunkChannel = make(chan chunk, d.cfg.Concurrency)
|
||||||
|
@ -197,6 +197,9 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time
|
|||||||
if r.Method != "HEAD" {
|
if r.Method != "HEAD" {
|
||||||
written, err := utils.CopyWithBufferN(w, sendContent, sendSize)
|
written, err := utils.CopyWithBufferN(w, sendContent, sendSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(context.Cause(ctx), context.Canceled) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
log.Warnf("ServeHttp error. err: %s ", err)
|
log.Warnf("ServeHttp error. err: %s ", err)
|
||||||
if written != sendSize {
|
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)
|
log.Warnf("Maybe size incorrect or reader not giving correct/full data, or connection closed before finish. written bytes: %d ,sendSize:%d, ", written, sendSize)
|
||||||
|
Reference in New Issue
Block a user