Skip to content

Commit

Permalink
fix(redis): fix redis connection
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed Apr 21, 2019
1 parent d01d44a commit 48117d2
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/mort/mort.go
Expand Up @@ -223,6 +223,7 @@ func main() {
obj.Debug = debug

res := rp.Process(req, obj)
defer res.Close()
res.SetDebug(obj)
if debug {
res.Set("X-Mort-Version", Version)
Expand Down
4 changes: 3 additions & 1 deletion configuration/config.yml
@@ -1,10 +1,12 @@
server:
logLevel: "debug"
cache:
type: "memory"
type: "redis"
address:
- "localhost:6379"
maxCacheItemSizeMB: 50
clentContg:

listens:
- ":8080"
- "unix:/tmp/mort.sock"
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/redis.go
Expand Up @@ -15,7 +15,7 @@ func parseAddress(addrs []string) map[string]string {

for _, addr := range addrs {
parts := strings.Split(addr, ":")
mp[parts[0]] = ":" + parts[1]
mp[parts[0]] = parts[0] + ":" + parts[1]
}

return mp
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/config.go
Expand Up @@ -251,7 +251,9 @@ func (c *Config) validateServer() error {
}

if c.Server.Cache.MaxCacheItemSize == 0 {
c.Server.Cache.MaxCacheItemSize = 50 * 1024
c.Server.Cache.MaxCacheItemSize = 50 * 2 << 20
} else {
c.Server.Cache.MaxCacheItemSize = c.Server.Cache.MaxCacheItemSize * 2 << 20
}

if c.Server.Cache.Type == "" {
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/types.go
Expand Up @@ -109,11 +109,13 @@ type HeaderYaml struct {
Values map[string]string `yaml:"values"`
}

// CacheCfg configure type of cache
type CacheCfg struct {
Type string `yaml:"type"`
Address []string `yaml:"address"`
MaxCacheItemSize int64 `yaml:"maxCacheItemSizeMB"`
CacheSize int64 `yaml:"cacheSize"`
ClientConfig map[string]string `yaml:"clientConfig"`
}

// Server configure HTTP server
Expand Down
7 changes: 6 additions & 1 deletion pkg/helpers/helpers.go
Expand Up @@ -50,7 +50,12 @@ func FetchObject(uri string) ([]byte, error) {

defer f.Close()

return ioutil.ReadAll(f)
buf, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}

return buf, nil
}

// IsRangeOrCondition check if request is range or condition
Expand Down
10 changes: 6 additions & 4 deletions pkg/processor/processor.go
Expand Up @@ -166,10 +166,11 @@ func (r *RequestProcessor) process(req *http.Request, obj *object.FileObject) *r
res = updateHeaders(obj, r.handleGET(req, obj))
}

if res.IsCachable() && res.IsBuffered() && res.ContentLength < r.serverConfig.Cache.MaxCacheItemSize {
if res.IsCachable() && res.ContentLength != -1 && res.ContentLength < r.serverConfig.Cache.MaxCacheItemSize {
resCpy, err := res.Copy()
if err == nil {
go func() {
resCpy.ReadBody()
err = r.responseCache.Set(obj, resCpy)
if err != nil {
monitoring.Log().Error("response cache error set", obj.LogData(zap.Error(err))...)
Expand All @@ -178,6 +179,7 @@ func (r *RequestProcessor) process(req *http.Request, obj *object.FileObject) *r
}
}


return res
case "PUT":
return handlePUT(req, obj)
Expand Down Expand Up @@ -300,13 +302,13 @@ func (r *RequestProcessor) handleGET(req *http.Request, obj *object.FileObject)
}()

} else {
monitoring.Report().Inc("request_type;type:download")

if res.StatusCode == 404 {
return r.handleNotFound(obj, parentObj, transformsTab, parentRes, res)
}

if res.StatusCode == 200 {
monitoring.Report().Inc("request_type;type:download")

if res.StatusCode > 199 && res.StatusCode < 299 {
if obj.CheckParent && parentObj != nil && parentRes.StatusCode == 200 {
return res
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/storage/storage.go
Expand Up @@ -457,7 +457,9 @@ func prepareResponse(obj *object.FileObject, resData responseData) *response.Res
resSize, _ = item.Size()
}

monitoring.Report().Gauge("storage_throughput;method:get,storage:"+obj.Storage.Kind, float64(resSize))
if resData.stream != nil {
monitoring.Report().Gauge("storage_throughput;method:get,storage:"+obj.Storage.Kind, float64(resSize))
}

if etag != "" {
res.Set("ETag", etag)
Expand Down

0 comments on commit 48117d2

Please sign in to comment.