From 85a874826911b562553ccb9e50b9053105c2c409 Mon Sep 17 00:00:00 2001 From: kokoichi206 Date: Sun, 19 Feb 2023 14:40:45 +0000 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E3=82=B3=E3=83=A1=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=81=AE=E4=BF=AE=E6=AD=A3=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 8 ++++---- usecase/download.go | 13 ++++++------- usecase/usecase.go | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 96a977a..58a54c2 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/usecase/download.go b/usecase/download.go index 95c8106..205c43b 100644 --- a/usecase/download.go +++ b/usecase/download.go @@ -15,8 +15,9 @@ import ( ) const ( - workingDir = "tmp" - imgDir = "imgs" + workingDir = "tmp" + imgDir = "imgs" + jsonFileName = "members_info.json" ) func (u *usecase) DownloadMembersZip(ctx context.Context, writer io.Writer) error { @@ -26,7 +27,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) @@ -38,7 +38,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) } @@ -110,8 +110,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 { @@ -160,7 +159,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) diff --git a/usecase/usecase.go b/usecase/usecase.go index de87869..499cb0f 100644 --- a/usecase/usecase.go +++ b/usecase/usecase.go @@ -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 { From ff0591e4b95d7d2f42a566fe9d0bd71fc91923a5 Mon Sep 17 00:00:00 2001 From: kokoichi206 Date: Sun, 19 Feb 2023 14:43:01 +0000 Subject: [PATCH 2/2] =?UTF-8?q?improve:=20=E4=B8=A6=E8=A1=8C=E3=81=97?= =?UTF-8?q?=E3=81=A6=E7=94=BB=E5=83=8F=E3=82=92=E5=8F=96=E5=BE=97=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ usecase/download.go | 30 ++++++++++++++++++++++++------ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 5055093..130d544 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index f58f889..9e5c6b8 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/usecase/download.go b/usecase/download.go index 205c43b..7d3ce80 100644 --- a/usecase/download.go +++ b/usecase/download.go @@ -12,6 +12,7 @@ import ( "github.com/android-project-46group/core-api/model" "github.com/google/uuid" + "golang.org/x/sync/errgroup" ) const ( @@ -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)