Skip to content

Commit

Permalink
support concurrency walkdir for webdav
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Jun 9, 2024
1 parent 337358c commit 84b03c9
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions storage/storage_webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -278,25 +279,44 @@ func (s *WebDavStorage) Remove(hash string) error {
}

func (s *WebDavStorage) WalkDir(walker func(hash string, size int64) error) error {
s.limitedDialer.Acquire()
defer s.limitedDialer.Release()

done := make(chan struct{}, len(utils.Hex256))
fileCh := make(chan fs.FileInfo, 0)
for _, dir := range utils.Hex256 {
files, err := s.cli.ReadDir(path.Join("download", dir))
if err != nil {
continue
}
for _, f := range files {
if !f.IsDir() {
if hash := f.Name(); len(hash) >= 2 && hash[:2] == dir {
if err := walker(hash, f.Size()); err != nil {
return err
done <- struct{}{}
go func(dir string) {
s.limitedDialer.Acquire()
defer s.limitedDialer.Release()
defer func() {
<-done
}()

files, err := s.cli.ReadDir(path.Join("download", dir))
if err != nil {
return
}
for _, f := range files {
if !f.IsDir() {
if hash := f.Name(); len(hash) >= 2 && hash[:2] == dir {
fileCh <- f
}
}
}
}(dir)
}
count := len(utils.Hex256)
for {
select {
case done <- struct{}{}:
count--
if count <= 0 {
return nil
}
case f := <-fileCh:
if err := walker(f.Name(), f.Size()); err != nil {
return err
}
}
}
return nil
}

func copyHeader(key string, dst, src http.Header) {
Expand Down

0 comments on commit 84b03c9

Please sign in to comment.