Kadai3-2 hioki-daichi #50
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
課題3-2 分割ダウンロードを行う
その他やったこと
GET /fooで Gopher の画像を返すような Partial Requests に対応したダミーサーバーです。Range: bytes=0-99のようなリクエストヘッダを指定してリクエストすると その範囲の content を 206 Partial Content とともに返します。-failure-rate-failure-rate=5を指定して実行すると、5% の確率で Internal Server Error を返すようになります。デフォルトは 0% です。max-delay-max-delay=100msを指定して実行すると、0~100ms 遅延させてレスポンスを返すようになります。デフォルトは 1 秒のため、0 から 1 秒の間でランダムに遅延します。port-p-o"index.html"になります。wget と同じ挙動です。-tcontext.WithTimeoutを使いたかったため、指定できるようにしました。デフォルトは 30 秒です。dojo3/kadai3-2/hioki-daichi/termination/termination.go
Lines 15 to 42 in dbb3b82
dojo3/kadai3-2/hioki-daichi/downloading/downloading.go
Lines 64 to 70 in dbb3b82
DEMO
※ 上側では
$ ./bin/dummy_server.go -port=8080 -max-delay=500ms -failure-rate=1のようにしてダミーサーバーを実行しています。(レスポンス時に 0~500ms 遅延させ、1 %の確率で 500 エラーを返すようなオプション)※ 下側では
$ go run main.go -p=32 -t=10s -o=bar.png http://localhost:8080/foo.png; open bar.pngのようにしてコマンドを実行し、作成されたファイルを開いています。(32 並列でタイムアウトは 10 秒、出力先はbar.pngというオプション)補足
curl -s https://raw.githubusercontent.com/gopherdojo/dojo3/kadai3-2-hioki-daichi/kadai3-2/hioki-daichi/coverage.html -o /tmp/coverage-hioki-daichi.html && open /tmp/coverage-hioki-daichi.html所感
Gopher道場#3終わってしばらく経ってしまいましたが、やっと満足行くものが出せました 😌
Ctrl+C 実行時は
context.WithCancelを使い、-t=30sのようなタイムアウトオプション指定時はcontext.WithTimeoutを、リクエストの際は都度req = req.WithContext(ctx)し、errgroup もeg, ctx := errgroup.WithContext(ctx)で context に包み、eg.Goに渡す関数内ではselect { case<-ctx.Done():で待ち受けるなど、 context をたくさん使ったことで context に慣れたような気がします。