Stop download of stickers on canceling context (#2699)

This commit is contained in:
Parvesh Monu 2022-06-02 22:03:16 +05:30 committed by GitHub
parent c3b0582cc9
commit 869942c05e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 20 deletions

View File

@ -1 +1 @@
0.100.0
0.100.1

View File

@ -1,6 +1,7 @@
package ipfs
import (
"context"
"errors"
"io/ioutil"
"net/http"
@ -32,6 +33,8 @@ type taskRequest struct {
}
type Downloader struct {
ctx context.Context
cancel func()
ipfsDir string
wg sync.WaitGroup
rateLimiterChan chan taskRequest
@ -47,7 +50,11 @@ func NewDownloader(rootDir string) *Downloader {
panic("could not create IPFSDir")
}
ctx, cancel := context.WithCancel(context.TODO())
d := &Downloader{
ctx: ctx,
cancel: cancel,
ipfsDir: ipfsDir,
rateLimiterChan: make(chan taskRequest, maxRequestsPerSecond),
inputTaskChan: make(chan taskRequest, 1000),
@ -68,6 +75,8 @@ func NewDownloader(rootDir string) *Downloader {
func (d *Downloader) Stop() {
close(d.quit)
d.cancel()
d.wg.Wait()
close(d.inputTaskChan)
@ -75,18 +84,13 @@ func (d *Downloader) Stop() {
}
func (d *Downloader) worker() {
for {
select {
case <-d.quit:
return
case request := <-d.rateLimiterChan:
for request := range d.rateLimiterChan {
resp, err := d.download(request.cid, request.download)
request.doneChan <- taskResponse{
err: err,
response: resp,
}
}
}
}
func (d *Downloader) taskDispatcher() {
@ -94,10 +98,7 @@ func (d *Downloader) taskDispatcher() {
defer ticker.Stop()
for {
select {
case <-d.quit:
return
case <-ticker.C:
<-ticker.C
request, ok := <-d.inputTaskChan
if !ok {
return
@ -105,7 +106,6 @@ func (d *Downloader) taskDispatcher() {
d.rateLimiterChan <- request
}
}
}
func hashToCid(hash []byte) (string, error) {
@ -203,6 +203,8 @@ func (d *Downloader) download(cid string, download bool) ([]byte, error) {
return nil, err
}
req = req.WithContext(d.ctx)
resp, err := d.client.Do(req)
if err != nil {
return nil, err