Skip to content

Commit

Permalink
Merge pull request #4 from android-project-46group/feature/img-fetch-…
Browse files Browse the repository at this point in the history
…concurrently

improve: 並行して画像を取得するよう変更
  • Loading branch information
kokoichi206 authored Feb 19, 2023
2 parents a9e20be + ff0591e commit 88b95d0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ godoc: ## godoc をローカルで表示する。http://localhost:8080/{module_n
lint: ## golangci を使って lint を走らせる
golangci-lint run -v

lint-fix:
lint-fix: ## lint 実行時, gofumpt のエラーが出たらやると良い
golangci-lint run --fix

serve:
serve: ## サーバーを起動する
go run main.go

test:
go test -cover -shuffle=on ./...
test: ## 全テストを実行
go test -cover -shuffle=on ./... -v
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
43 changes: 30 additions & 13 deletions usecase/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (

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

const (
workingDir = "tmp"
imgDir = "imgs"
workingDir = "tmp"
imgDir = "imgs"
jsonFileName = "members_info.json"
)

func (u *usecase) DownloadMembersZip(ctx context.Context, writer io.Writer) error {
Expand All @@ -26,7 +28,6 @@ func (u *usecase) DownloadMembersZip(ctx context.Context, writer io.Writer) erro
}

// 処理終了時に一時ファイルを削除する。
// return が file の reader になったら怪しいかもしれない。
defer func() {
if err := os.RemoveAll(randomPath); err != nil {
u.logger.Warnf(ctx, "failed to remove dir: %v", err)
Expand All @@ -38,7 +39,7 @@ func (u *usecase) DownloadMembersZip(ctx context.Context, writer io.Writer) erro
return fmt.Errorf("failed to ListMembers: %w", err)
}

jsonPath := filepath.Join(randomPath, "members_info.json")
jsonPath := filepath.Join(randomPath, jsonFileName)
if err := u.writeMembersJSON(members, jsonPath); err != nil {
return fmt.Errorf("faild to writeMembersJson: %w", err)
}
Expand All @@ -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 Expand Up @@ -110,8 +128,7 @@ func (u *usecase) downloadImage(ctx context.Context, url, fullPath string) error

// 引数で受け取った basePath 配下に、unique なフォルダを作成する。
//
// フォルダ名は uuid で構成され、return の string では
// basePath も含んだパス全体が返される。
// フォルダ名は uuid であり、return の string では basePath も含んだパス全体が返される。
func (u *usecase) createUniqueDir(basePath string) (string, error) {
uuid, err := uuid.NewRandom()
if err != nil {
Expand Down Expand Up @@ -160,7 +177,7 @@ func (u *usecase) createZip(targetDir string, readWriter io.Writer) error {
return nil
}

// 圧縮
// 圧縮する。
fileToZip, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to Open: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type Usecase interface {
// メンバー関連情報一覧を zip 形式で全取得する。
DownloadMembersZip(ctx context.Context, x io.Writer) error
DownloadMembersZip(ctx context.Context, writer io.Writer) error
}

type usecase struct {
Expand Down

0 comments on commit 88b95d0

Please sign in to comment.