Skip to content

Commit

Permalink
only check rate limit when there is actual download
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyuliu committed Dec 2, 2023
1 parent b1b995f commit 2f93a98
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ thread:

// downloadFile in a thread.
func (f *fetcher) downloadFile(bookID int64, format file.Format, share driver.Share) error {
f.progress.TakeRateLimit()
log.Debugf("Start download book id %d, format %s, share %v.", bookID, format, share)
// Create the file writer.
writer, err := f.creator.NewWriter(bookID, f.progress.Size(), share.FileName, share.SubPath, format, share.Size)
Expand Down
11 changes: 8 additions & 3 deletions internal/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var (
)

type Progress interface {
// TakeRateLimit would wait until the rate limit is available.
TakeRateLimit()

// AcquireBookID would find the book id from the assign array.
AcquireBookID() int64

Expand Down Expand Up @@ -127,14 +130,16 @@ func loadStorage(file *os.File) (*bitset.BitSet, error) {
return set, nil
}

// TakeRateLimit block until the rate meets the given config.
func (storage *bitProgress) TakeRateLimit() {
storage.limit.Take()
}

// AcquireBookID would find the book id from the assign array.
func (storage *bitProgress) AcquireBookID() int64 {
storage.lock.Lock()
defer storage.lock.Unlock()

// Block until the rate meets the given config.
storage.limit.Take()

for i := uint(0); i < storage.assigned.Len(); i++ {
if !storage.assigned.Test(i) {
storage.assigned.Set(i)
Expand Down

0 comments on commit 2f93a98

Please sign in to comment.