Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:current writes map #182

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ type Downloader struct {
tasks []*Task
listener Listener

lock *sync.Mutex
closed atomic.Bool
lock *sync.Mutex
fetcherMapLock *sync.RWMutex
closed atomic.Bool
}

func NewDownloader(cfg *DownloaderConfig) *Downloader {
Expand All @@ -63,8 +64,9 @@ func NewDownloader(cfg *DownloaderConfig) *Downloader {
fetchBuilders: make(map[string]fetcher.FetcherBuilder),
fetcherMap: make(map[string]fetcher.Fetcher),

storage: cfg.Storage,
lock: &sync.Mutex{},
storage: cfg.Storage,
lock: &sync.Mutex{},
fetcherMapLock: &sync.RWMutex{},
}
for _, f := range cfg.FetchBuilders {
for _, p := range f.Schemes() {
Expand Down Expand Up @@ -198,7 +200,9 @@ func (d *Downloader) Resolve(req *base.Request) (rr *ResolveResult, err error) {
if err != nil {
return
}
d.fetcherMapLock.Lock()
d.fetcherMap[rrId] = fetcher
d.fetcherMapLock.Unlock()
rr = &ResolveResult{
ID: rrId,
Res: fetcher.Meta().Res,
Expand All @@ -218,11 +222,17 @@ func (d *Downloader) Create(rrId string, opts *base.Options) (taskId string, err
if opts == nil {
opts = &base.Options{}
}
d.fetcherMapLock.RLock()
fetcher, ok := d.fetcherMap[rrId]
d.fetcherMapLock.RUnlock()
if !ok {
return "", errors.New("invalid resource id")
}
defer delete(d.fetcherMap, rrId)
defer func() {
d.fetcherMapLock.Lock()
delete(d.fetcherMap, rrId)
d.fetcherMapLock.Unlock()
}()

meta := fetcher.Meta()
meta.Opts = opts
Expand Down