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 yuuyamad #46

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kadai3/yuuyamad/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./idea
20 changes: 20 additions & 0 deletions kadai3/yuuyamad/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# go concurrent downloader

## Command Line Options

### -p num
split ratio to download file

### -o filename
output file to filename

## example
./yuuyamad -p 20 -o hoge.tar.xz "https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.6.3.tar.xz"


## 疑問点
requestsメソッドをgoroutineで実行する時にcontextをわたしているのだが、1つのgoroutineがtimeoutしたりキャンセル処理された時に
同じcontextを渡して実行されているgoroutineにもキャンセルが伝播してcontextのDoneチャネルが返るのかなと考えて実装しました

timeoutが他のgoroutineに伝播しているかというのは上手く検証できず、、自信がないのでもし認識間違いなどあれば教えて頂きたいです。

13 changes: 13 additions & 0 deletions kadai3/yuuyamad/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"log"
"os"
)

func logError(err error){
if err != nil{
log.Fatal(err)
os.Exit(1)
}
}
151 changes: 151 additions & 0 deletions kadai3/yuuyamad/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package main

import (
"context"
"flag"
"fmt"
"golang.org/x/sync/errgroup"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
)

type Range struct {
low int
high int
worker int
}

type Downloader struct {
procs int
filename string
url string
}

func main() {

var (
procs = flag.Int("p", 10, "split ratio to download file")
output = flag.String("o", "", "output filename")
)

flag.Parse()
args := flag.Args()

downloder := Downloader{}
downloder.procs = *procs
downloder.filename = *output
downloder.url = args[0]

err := downloder.download()
if err != nil {
logError(err)
}

//分割ダウンロードしたファイルを結合する
fh, err := os.Create(downloder.filename)
if err != nil {
logError(err)
}
defer fh.Close()

for j := 0; j < downloder.procs; j++ {
f := fmt.Sprintf("%s.%d", downloder.filename, j)
subfp, err := os.Open(f)
if err != nil {
logError(err)
}

io.Copy(fh, subfp)

subfp.Close()
if err := os.Remove(f); err != nil {
logError(err)
}
}
}
func (d *Downloader) download() error {

// contents Headerを取得する
res, err := http.Head(d.url)
if err != nil {
return err
}

maps := res.Header
length, err := strconv.Atoi(maps["Content-Length"][0])

len_sub := length / d.procs
diff := length % d.procs

// errorGroup
grp, ctx := errgroup.WithContext(context.Background())
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()

for i := 0; i < d.procs; i++ {

min := len_sub * i
max := len_sub * (i + 1)

r := Range{}
r.low = min
r.high = max
r.worker = i

if i == (d.procs - 1) {
max += diff
}
// execute get request
grp.Go(func() error {
return d.requests(ctx, r)
})
}
if err := grp.Wait(); err != nil {
return err
}
return nil
}

func (d *Downloader) requests(ctx context.Context, r Range) error {
body := make([]string, 99)
client := &http.Client{}
req, err := http.NewRequest("GET", d.url, nil)
if err != nil {
return err
}

range_header := "bytes=" + strconv.Itoa(r.low) + "-" + strconv.Itoa(r.high-1)
req.Header.Add("Range", range_header)

errCh := make(chan error, 1)
tmpfile := d.filename + "." + strconv.Itoa(r.worker)

go func() {
resp, err := client.Do(req)
defer resp.Body.Close()

reader, err := ioutil.ReadAll(resp.Body)
body[r.worker] = string(reader)

err = ioutil.WriteFile(tmpfile, []byte(string(body[r.worker])), 0x777)
errCh <- err
}()

select {
case err := <-errCh:
if err != nil {
return err
}
case <-ctx.Done():
fmt.Printf("requests: %s\n", ctx.Err())
os.Remove(tmpfile)
<-errCh
return ctx.Err()

}
return nil
}