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

gopher道場 #3-2 #43

Open
wants to merge 2 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
20 changes: 20 additions & 0 deletions kadai3-2/po3rin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Quick start

you can exex test file server via below command

```bash
$ cd server
$ go run main.go
```

exec splitt loader

```bash
$ cd loader
$ go run main.go
```

## 反省
まだキャンセルの実装ができていない。
errgroupによるエラー処理を追加したが、全てのgorutineがちゃんととまているのかテストする術を調査中
はじめてHeadメソッドを使ったので勉強になった。
21 changes: 21 additions & 0 deletions kadai3-2/po3rin/loader/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions kadai3-2/po3rin/loader/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[prune]
go-tests = true
unused-packages = true
22 changes: 22 additions & 0 deletions kadai3-2/po3rin/loader/length/length.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package length

import (
"fmt"
"net/http"
"strconv"
)

// Calc calcrate contents-length
func Calc() int {
// get contents length
res, err := http.Head("http://localhost:8080/5mqx.cif")
if err != nil {
fmt.Println(err)
}
maps := res.Header
length, err := strconv.Atoi(maps["Content-Length"][0])
if err != nil {
fmt.Println(err)
}
return length
}
85 changes: 85 additions & 0 deletions kadai3-2/po3rin/loader/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"context"
"dojo2/kadai3-2/po3rin/loader/length"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"

"golang.org/x/sync/errgroup"
)

// var wg sync.WaitGroup

func main() {
length := length.Calc()

// prepare val of split downloaf
limit := 10
leng := length / limit
diff := length % limit
body := make(map[int][]byte)

eg, ctx := errgroup.WithContext(context.Background())
ctx, cancel := context.WithCancel(ctx)
defer cancel()

// exec download file
for i := 0; i < limit; i++ {
i := i

// wg.Add(1)
min := leng * i
max := leng * (i + 1)

if i == limit-1 {
max += diff
}

eg.Go(func() error {
client := &http.Client{}
select {
case <-ctx.Done():
return errors.New("Error occurred")
default:
req, err := http.NewRequest("GET", "http://localhost:8080/5mqx.cif", nil)
if err != nil {
cancel()
}
rh := "bytes=" + strconv.Itoa(min) + "-" + strconv.Itoa(max-1)
req.Header.Add("Range", rh)
resp, err := client.Do(req)
if err != nil {
cancel()
}
defer resp.Body.Close()
reader, err := ioutil.ReadAll(resp.Body)
if err != err {
cancel()
}
body[i] = reader
return nil
}
})
}
if err := eg.Wait(); err != nil {
log.Fatal(err)
}

// create new file
buf := make([]byte, 0)
for i := 0; i < limit; i++ {
buf = append(buf, body[i]...)
}
file, err := os.Create(`../5mqx.cif`)
if err != nil {
fmt.Println(err)
}
defer file.Close()
file.Write(([]byte)(buf))
}
13 changes: 13 additions & 0 deletions kadai3-2/po3rin/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"log"
"net/http"
)

func main() {
http.Handle("/", http.FileServer(http.Dir("static")))
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Loading