Skip to content

Kadai3-2: lfcd85 - #33

Open
lfcd85 wants to merge 23 commits into
masterfrom
kadai3-2-lfcd85
Open

Kadai3-2: lfcd85#33
lfcd85 wants to merge 23 commits into
masterfrom
kadai3-2-lfcd85

Conversation

@lfcd85

@lfcd85 lfcd85 commented Jun 20, 2019

Copy link
Copy Markdown

課題3-2 分割ダウンローダを作ろう の実装です。

Specs

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

TODO

  • テストを実装する
    • 結合テスト
      • テスト用サーバーを立てて testdata からテストできるようにしました: 0dd26ec
        ( hioki-daichi さんのこちらを参考にしました)
    • [WANT] テストの拡充

@lfcd85
lfcd85 requested review from a team and tenntenn June 20, 2019 14:24
@lfcd85 lfcd85 self-assigned this Jun 20, 2019
@lfcd85 lfcd85 changed the title [WIP] Kadai3-2: lfcd85 Kadai3-2: lfcd85 Jun 21, 2019
Comment thread kadai3-2/lfcd85/Makefile Outdated
PHONY: fmt
fmt:
go fmt ./...
go vet ./...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

実は最近はgo testで自動でかかる

Comment thread kadai3-2/lfcd85/mypget/mypget.go Outdated
}

// Execute do the split download.
func (d *Downloader) Execute() error {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

せっかくなので引数にcontext.Contextとって、nilならBackgroundにしても良いかも。

Comment thread kadai3-2/lfcd85/mypget/mypget.go Outdated
ctx, cancel := context.WithCancel(bc)
defer cancel()

req, err := http.NewRequest("GET", d.url.String(), nil)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

http.MethodGet

return err
}

resp, err := http.DefaultClient.Do(req)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

HTTPクライアントを変更できるようにしておくとよいかも。
最近はあんまり必要ないかもしれないけど、GAEだと特殊なHTTPクライアントを使う必要があったりするので。

Comment thread kadai3-2/lfcd85/mypget/mypget.go Outdated
return err
}

err = d.combine(tempDir)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if err := ...にしてスコープを小さくする

if err != nil {
return err
}
fmt.Printf("Download completed! saved at: %v\n", d.outputPath)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

出力先を変更できるようにする

Comment thread kadai3-2/lfcd85/mypget/mypget.go Outdated
}
fmt.Printf("Download completed! saved at: %v\n", d.outputPath)

return err

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nilを返す

Comment thread kadai3-2/lfcd85/mypget/mypget.go Outdated
}

func (d *Downloader) downloadByRanges(ctx context.Context, tempDir string) error {
var eg errgroup.Group

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

errgourp.WithContextを呼ばないとキャンセルされない

return err
}

partialPath := generatePartialPath(tempDir, i)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

MEMO
tempDirは読み込みだけなのでセーフ


for i, _ := range d.ranges {
partialPath := generatePartialPath(tempDir, i)
partial, err := os.Open(partialPath)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

閉じてない

Comment thread kadai3-2/lfcd85/mypget/mypget_test.go Outdated

w.Header().Set("Content-Length", strconv.Itoa(len(body)))
w.WriteHeader(http.StatusPartialContent)
w.Write(body)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

エラー処理

return ts, func() { ts.Close() }
}

func (tsf *testServerFile) testServerHandler(t *testing.T, w http.ResponseWriter, r *http.Request) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

せっかくなんで大きなファイルもテストできるようにするとよいかも。

@suaaa7 suaaa7 mentioned this pull request Aug 31, 2019
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants