Skip to content

Commit

Permalink
improve: 並行して画像を取得するよう変更
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoichi206 committed Feb 19, 2023
1 parent 85a8748 commit ff0591e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
30 changes: 24 additions & 6 deletions usecase/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/android-project-46group/core-api/model"
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
)

const (
Expand Down Expand Up @@ -47,16 +48,33 @@ func (u *usecase) DownloadMembersZip(ctx context.Context, writer io.Writer) erro
return fmt.Errorf("faild to initializeImgDir: %w", err)
}

//nolint:varnamelen
eg := errgroup.Group{}
imgParallel := make(chan bool, len(members))

for _, member := range members {
fileName := path.Base(member.ImgURL)
fullPath := filepath.Join(randomPath, imgDir, fileName)
member := member
imgParallel <- true

err := u.downloadImage(ctx, member.ImgURL, fullPath)
if err != nil {
u.logger.Warnf(ctx, "failed to downloadImage: %v", err)
}
eg.Go(func() error {
fileName := path.Base(member.ImgURL)
fullPath := filepath.Join(randomPath, imgDir, fileName)

err := u.downloadImage(ctx, member.ImgURL, fullPath)
if err != nil {
u.logger.Warnf(ctx, "failed to downloadImage: %v", err)
}

return nil
})
}

if err := eg.Wait(); err != nil {
return fmt.Errorf("failed to image parallel fetch: %w", err)
}

close(imgParallel)

err = u.createZip(randomPath, writer)
if err != nil {
u.logger.Warnf(ctx, "failed to createZip: %v", err)
Expand Down

0 comments on commit ff0591e

Please sign in to comment.