Skip to content

Kadai3 2 mizushima #68

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

Conversation

MizushimaToshihiko
Copy link

お手数をおかけ致しますが、レビュー頂けますと幸いです

課題3-2 分割ダウンローダを作ろう

  • 分割ダウンロードを行う
  • Rangeアクセスを用いる
  • いくつかのゴルーチンでダウンロードしてマージする
  • エラー処理を工夫する
    • golang.org/x/sync/errgourpパッケージなどを使ってみる
  • キャンセルが発生した場合の実装を行う

コマンドラインオプション

ショートオプション ロングオプション 説明 デフォルト
-h --help 使い方を表示して終了 -
-p <num> --procs <num> プロセス数を指定 お使いのPCのコア数
-o <path> --output <path> ダウンロードしたファイルをどこのディレクトリに保存するか指定する カレントディレクトリ
-t <num> --timeout <num> サーバーへのリクエストを止める時間を秒数で指定 120

インストール方法

go get github.com/MizushimaToshihiko/gopherdojo-studyroom/kadai3-2/Mizushima

使い方

  1. 実行ファイル作成
$ make build
  1. URLを指定してダウンロード実行
$ ./bin/paraDW [option] URL( URL URL ...)

※ URLは複数指定できます
※ ダウンロード先がRangeアクセスに対応していれば、go routineを使った並行ダウンロードを行い、そうでなければ1プロセスのダウンロードを行います

テストの方法

  • バイナリビルド & テスト
$ make
  • テスト後の処理(掃除)
$ make clean

ディレクトリ構成

.
├── bin  
│   └── paraDW  # "make build" command required.  
├── download  
│   ├── download.go  
│   ├── download_test.go  
│   ├── go.mod  
│   └── go.sum  
├── getheader  
│   ├── geHeader_test.go  
│   ├── getHeader.go  
│   └── go.mod  
├── .gitignore  
├── go.mod  
├── go.sum  
├── listen  
│   ├── listen.go  
│   └── listen_test.go  
├── main.go  
├── Makefile  
├── README.md  
├── request  
│   ├── go.mod  
│   ├── request.go  
│   └── request_test.go  
└── testdata  
    ├── 003  
    └── z4d4kWk.jpg  

参考にしたもの

pget (goroutineを使ったダウンロード処理、コマンドラインオプションの処理等々)
gopherdojo/dojo3#50 (ctrl+cを押したときのキャンセル処理など)

課題1のテストを追加
その他
main.go テストの追加により仕様変更
picconvert/picconvert.go godocでの見栄えが少しでも良くなるようにコメント追加
picconvert_test.go TestGlobでのテストケース追加、testdataをコピーしないように変更
Makefile 各コマンドをgoのものに変更
等々
"golang.org/x/sync/errgroup"
)

//PDownloader is user-defined struct

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

細かい事ですが、コメントでPDownloaderはユーザ定義型としか言っていないので、他と見比べるとPDownloader自体の説明が入るのが適切なのではと思います。


// make a empty context
ctxTimeout, cancelTimeout := context.WithTimeout(context.Background(), time.Duration(opts.Tm)*time.Second)
defer cancelTimeout()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ループの中でdeferを書くのはループ分defer処理が行われることになり、このケースではurlが三つあったとして関数を抜けるときに三連続でcancelTimeoutが走るのがおそらく必要ではない(実害はないかもしれませんが)ので望ましくないかと思います。

url一つづの処理で関数を切るのが、deferも適切になりますし、後に os.OpenFileしているところでdefer out.Close()が書けてCloseの関数を色々なところで呼ばなくても良くなるので良いかと思います。

log.Fatalf("err: getheader.ResHeader: %s\n", err)
} else if accept[0] != "bytes" || opts.Procs == 1 {
isPara = false
continue

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここのcontinueっておそらく間違いなのではと思います。isPara = falseを入れている意味もないかと思うので。

@MizushimaToshihiko
Copy link
Author

後続の方が増えてきたのでCloseします。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants