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 と同じ挙動です。-t
context.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 に慣れたような気がします。