Skip to content

Commit

Permalink
refactor proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Terry-Mao committed Jun 15, 2016
1 parent 97ce4b8 commit a7529fe
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 37 deletions.
12 changes: 7 additions & 5 deletions libs/errors/common.go
Original file line number Diff line number Diff line change
@@ -1,18 1,20 @@
package errors

const (
RetOK = 1
RetParamErr = 65534
RetInternalErr = 65535
RetOK = 1
RetServiceUnavailable = 65533
RetParamErr = 65534
RetInternalErr = 65535

// needle
RetNeedleExist = 5000
)

var (
// common
ErrParam = Error(RetParamErr)
ErrInternal = Error(RetInternalErr)
ErrParam = Error(RetParamErr)
ErrInternal = Error(RetInternalErr)
ErrServiceUnavailable = Error(RetServiceUnavailable)

ErrNeedleExist = Error(RetNeedleExist)
)
3 changes: 3 additions & 0 deletions libs/meta/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 12,7 @@ type Response struct {
Cookie int32 `json:"cookie"`
Vid int32 `json:"vid"`
Stores []string `json:"stores"`
MTime int64 `json:"update_time"`
Sha1 string `json:"sha1"`
Mine string `json:"mine"`
}
1 change: 1 addition & 0 deletions libs/meta/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 6,5 @@ type File struct {
Sha1 string `json:"sha1"`
Mine string `json:"mine"`
Status int32 `json:"status"`
MTime int64 `json:"update_time"`
}
3 changes: 2 additions & 1 deletion libs/meta/needle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 3,6 @@ package meta
type Needle struct {
Key int64 `json:"key"`
Cookie int32 `json:"cookie"`
Vid int32 `json:vid`
Vid int32 `json:"vid"`
MTime int64 `json:"update_time"`
}
33 changes: 19 additions & 14 deletions proxy/bfs/bfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 11,7 @@ import (
log "github.com/golang/glog"
"io"
"io/ioutil"
"math/rand"
"mime/multipart"
"net"
"net/http"
Expand Down Expand Up @@ -45,6 46,8 @@ var (
Transport: _transport,
}
_canceler = _transport.CancelRequest
// random store node
_rand = rand.New(rand.NewSource(time.Now().UnixNano()))
)

type Bfs struct {
Expand All @@ -58,14 61,14 @@ func New(c *conf.Config) (b *Bfs) {
}

// Get
func (b *Bfs) Get(bucket, filename string) (content []byte, err error) {
func (b *Bfs) Get(bucket, filename string) (src io.ReadCloser, ctlen int, mtime int64, sha1, mine string, err error) {
var (
params = url.Values{}
host string
uri string
req *http.Request
resp *http.Response
res meta.Response
i, ix, l int
uri string
req *http.Request
resp *http.Response
res meta.Response
params = url.Values{}
)
params.Set("bucket", bucket)
params.Set("filename", filename)
Expand All @@ -83,13 86,17 @@ func (b *Bfs) Get(bucket, filename string) (content []byte, err error) {
}
return
}

mtime = res.MTime
sha1 = res.Sha1
mine = res.Mine
params = url.Values{}
for _, host = range res.Stores {
l = len(res.Stores)
ix = _rand.Intn(l)
for i = 0; i < l; i {
params.Set("key", strconv.FormatInt(res.Key, 10))
params.Set("cookie", strconv.FormatInt(int64(res.Cookie), 10))
params.Set("vid", strconv.FormatInt(int64(res.Vid), 10))
uri = fmt.Sprintf(_storeGetApi, host) "?" params.Encode()
uri = fmt.Sprintf(_storeGetApi, res.Stores[(ix i)%l]) "?" params.Encode()
if req, err = http.NewRequest("GET", uri, nil); err != nil {
continue
}
Expand All @@ -104,10 111,8 @@ func (b *Bfs) Get(bucket, filename string) (content []byte, err error) {
if resp.StatusCode != http.StatusOK {
continue
}
if content, err = ioutil.ReadAll(resp.Body); err != nil {
continue
}
resp.Body.Close()
src = resp.Body
ctlen = int(resp.ContentLength)
break
}
return
Expand Down
2 changes: 1 addition & 1 deletion proxy/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 51,7 @@ func (i *Item) Public(read bool) bool {
return i.writePublic()
}

// NewBucket
// New a bucket.
func New() (b *Bucket, err error) {
var item *Item
b = new(Bucket)
Expand Down
6 changes: 2 additions & 4 deletions proxy/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 24,6 @@ type Config struct {
Prefix string
// file
MaxFileSize int
// purge channel
PurgeMaxSize int
}

// Code to implement the TextUnmarshaler interface for `duration`:
Expand Down Expand Up @@ -55,11 53,11 @@ func NewConfig(conf string) (c *Config, err error) {
if err = toml.Unmarshal(blob, c); err != nil {
return
}
// http://domain/ covert to http://domain
c.Domain = strings.TrimRight(c.Domain, "/")
// bfs,/bfs,/bfs/ convert to /bfs/
if c.Prefix != "" {
c.Prefix = path.Join("/", c.Prefix) "/"
// http://domain/ covert to http://domain
c.Domain = strings.TrimRight(c.Domain, "/")
}
return
}
30 changes: 20 additions & 10 deletions proxy/http_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 9,7 @@ import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -164,26 165,35 @@ func (s *server) getURI(bucket, file string) (uri string) {
// download.
func (s *server) download(item *ibucket.Item, bucket, file string, wr http.ResponseWriter, r *http.Request) {
var (
data []byte
err error
mtime int64
ctlen int
mine string
sha1 string
start = time.Now()
src io.ReadCloser
status = http.StatusOK
err error
)
defer httpLog("download", r.URL.Path, &bucket, &file, start, &status, &err)
if data, err = s.bfs.Get(bucket, file); err != nil {
if src, ctlen, mtime, sha1, mine, err = s.bfs.Get(bucket, file); err == nil {
wr.Header().Set("Content-Length", strconv.Itoa(ctlen))
wr.Header().Set("Content-Type", mine)
wr.Header().Set("Server", "bfs")
wr.Header().Set("Last-Modified", time.Unix(0, mtime).Format(http.TimeFormat))
wr.Header().Set("Etag", sha1)
if src != nil {
if r.Method == "GET" {
io.Copy(wr, src)
}
src.Close()
}
} else {
if err == errors.ErrNeedleNotExist {
status = http.StatusNotFound
} else {
status = http.StatusInternalServerError
}
http.Error(wr, "", status)
} else {
wr.Header().Set("Content-Type", http.DetectContentType(data))
wr.Header().Set("Content-Length", strconv.Itoa(len(data)))
wr.Header().Set("Server", "bfs")
if r.Method == "GET" {
_, err = wr.Write(data)
}
}
return
}
Expand Down
2 changes: 0 additions & 2 deletions proxy/proxy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 11,3 @@ Domain = "http://localhost:2232/"
Prefix = "/bfs/"

MaxFileSize = 20971520

PurgeMaxSize = 100000

0 comments on commit a7529fe

Please sign in to comment.