Skip to content

Commit

Permalink
Merge pull request Terry-Mao#64 from zhapuyu/master
Browse files Browse the repository at this point in the history
fix pitchfork timeout bug
  • Loading branch information
Terry-Mao authored Nov 25, 2016
2 parents c62a4e7 + b16392a commit 67f2ccb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
28 changes: 25 additions & 3 deletions libs/meta/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
log "github.com/golang/glog"
"io/ioutil"
"net/http"
"time"
)

const (
Expand All @@ -27,6 +28,15 @@ const (
probeAPI = "http://%s/probe?vid=%d"
)

var (
_client = &http.Client{
Transport: &http.Transport{
DisableCompression: true,
},
Timeout: 2 * time.Second,
}
)

type StoreList []*Store

func (sl StoreList) Len() int {
Expand Down Expand Up @@ -83,12 +93,17 @@ func (s *Store) probeAPI(vid int32) string {
func (s *Store) Info() (vs []*Volume, err error) {
var (
body []byte
req *http.Request
resp *http.Response
data = new(Volumes)
url = s.statAPI()
)
if resp, err = http.Get(url); err != nil {
log.Warningf("http.Get(\"%s\") error(%v)", url, err)
if req, err = http.NewRequest("GET", url, nil); err != nil {
log.Info("http.NewRequest(GET,%s) error(%v)", url, err)
return
}
if resp, err = _client.Do(req); err != nil {
log.Errorf("_client.do(%s) error(%v)", url, err)
return
}
defer resp.Body.Close()
Expand All @@ -111,13 +126,20 @@ func (s *Store) Info() (vs []*Volume, err error) {
// Head send a head request to store.
func (s *Store) Head(vid int32) (err error) {
var (
req *http.Request
resp *http.Response
url string
)
url = s.probeAPI(vid)
if resp, err = http.Head(url); err != nil {
if req, err = http.NewRequest("HEAD", url, nil); err != nil {
log.Info("http.NewRequest(GET,%s) error(%v)", url, err)
return
}
if resp, err = _client.Do(req); err != nil {
log.Errorf("_client.do(%s) error(%v)", url, err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusInternalServerError {
err = errors.ErrInternal
}
Expand Down
8 changes: 6 additions & 2 deletions proxy/bfs/bfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ func (b *Bfs) Get(bucket, filename string) (src io.ReadCloser, ctlen int, mtime
ctlen = int(resp.ContentLength)
break
}
if err == nil && resp.StatusCode == http.StatusServiceUnavailable {
err = errors.ErrStoreNotAvailable
if err == nil {
if resp.StatusCode == http.StatusServiceUnavailable {
err = errors.ErrStoreNotAvailable
} else if resp.StatusCode == http.StatusNotFound {
err = errors.ErrNeedleNotExist
}
}
return
}
Expand Down
20 changes: 18 additions & 2 deletions proxy/http_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,18 @@ func (s *server) parseURI(r *http.Request, upload bool) (bucket, file string, st

// gentRI get uri by bucket and file.
func (s *server) getURI(bucket, file string) (uri string) {
var (
item *ibucket.Item
domain string
)
// http://domain/prefix/bucket/file
uri = s.c.Domain + path.Join(s.c.Prefix, bucket, file)
item, _ = s.bucket.Get(bucket)
if item.Domain != "" {
domain = item.Domain
} else {
domain = s.c.Domain
}
uri = domain + path.Join(s.c.Prefix, bucket, file)
return
}

Expand Down Expand Up @@ -235,10 +245,16 @@ func (s *server) upload(item *ibucket.Item, bucket, file string, wr http.Respons
return
}
r.Body.Close()
if len(body) > s.c.MaxFileSize {
length := len(body)
if length > s.c.MaxFileSize {
status = http.StatusRequestEntityTooLarge
return
}
if length == 0 {
status = http.StatusBadRequest
log.Errorf("file size equals 0")
return
}
sha = sha1.Sum(body)
sha1sum = hex.EncodeToString(sha[:])
// if empty filename or endwith "/": dir
Expand Down

0 comments on commit 67f2ccb

Please sign in to comment.