Skip to content
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

Kadai3-2: lfcd85 #33

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Kadai3-2: lfcd85 #33

wants to merge 23 commits into from

Conversation

lfcd85
Copy link

@lfcd85 lfcd85 commented Jun 20, 2019

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

Specs

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

TODO

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

@lfcd85 lfcd85 requested review from tenntenn and a team 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
PHONY: fmt
fmt:
go fmt ./...
go vet ./...
Copy link
Member

Choose a reason for hiding this comment

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

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

}

// Execute do the split download.
func (d *Downloader) Execute() error {
Copy link
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にしても良いかも。

ctx, cancel := context.WithCancel(bc)
defer cancel()

req, err := http.NewRequest("GET", d.url.String(), nil)
Copy link
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
Member

Choose a reason for hiding this comment

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

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

return err
}

err = d.combine(tempDir)
Copy link
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
Member

Choose a reason for hiding this comment

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

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

}
fmt.Printf("Download completed! saved at: %v\n", d.outputPath)

return err
Copy link
Member

Choose a reason for hiding this comment

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

nilを返す

}

func (d *Downloader) downloadByRanges(ctx context.Context, tempDir string) error {
var eg errgroup.Group
Copy link
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
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
Member

Choose a reason for hiding this comment

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

閉じてない


w.Header().Set("Content-Length", strconv.Itoa(len(body)))
w.WriteHeader(http.StatusPartialContent)
w.Write(body)
Copy link
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
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