diff --git a/object/file_object.go b/object/file_object.go index 2e112c8..a2697bd 100644 --- a/object/file_object.go +++ b/object/file_object.go @@ -90,6 +90,7 @@ type FileObject struct { func NewFileObject(uri string, mortConfig *config.Config) (*FileObject, error) { obj := FileObject{} obj.Uri = uri + //obj.uriBytes = []byte(uri) obj.CheckParent = false err := obj.decode(mortConfig) @@ -99,16 +100,17 @@ func NewFileObject(uri string, mortConfig *config.Config) (*FileObject, error) { } func (self *FileObject) decode(mortConfig *config.Config) error { - elements := strings.Split(self.Uri, "/") + elements := strings.SplitN(self.Uri, "/", 3) self.Bucket = elements[1] if len(elements) > 2 { - self.Key = "/" + strings.Join(elements[2:], "/") + self.Key = "/" + elements[2] } + if bucket, ok := mortConfig.Buckets[self.Bucket]; ok { err := self.decodeKey(bucket, mortConfig) - if self.HasTransform() { + if self.Transforms.NotEmpty { self.Storage = bucket.Storages.Transform() } else { self.Storage = bucket.Storages.Basic() @@ -129,10 +131,9 @@ func (self *FileObject) decodeKey(bucket config.Bucket, mortConfig *config.Confi trans := bucket.Transform matches := trans.PathRegexp.FindStringSubmatch(self.Key) - if len(matches) < 3 { + if matches == nil { return nil - - } + } subMatchMap := make(map[string]string, 2) @@ -142,7 +143,7 @@ func (self *FileObject) decodeKey(bucket config.Bucket, mortConfig *config.Confi } } presetName := subMatchMap["presetName"] //string(matches[trans.Order.PresetName+1]) - parent := "/" + subMatchMap["parent"] // "/" + string(matches[trans.Order.Parent+1]) + parent := subMatchMap["parent"] // "/" + string(matches[trans.Order.Parent+1]) if _, ok := bucket.Transform.Presets[presetName]; !ok { log.Log().Warnw("FileObject decodeKey unknown preset", "obj.Key", self.Key, "parent", parent, "presetName", presetName, "regexp", trans.Path) @@ -156,9 +157,7 @@ func (self *FileObject) decodeKey(bucket config.Bucket, mortConfig *config.Confi } - if bucket.Transform.ParentBucket != "" { - parent = "/" + path.Join(bucket.Transform.ParentBucket, parent) - } + parent = "/" + path.Join(bucket.Transform.ParentBucket, parent) parentObj, err := NewFileObject(parent, mortConfig) parentObj.Storage = bucket.Storages.Get(bucket.Transform.ParentStorage) diff --git a/object/file_object_test.go b/object/file_object_test.go index 744ec9e..b86074c 100644 --- a/object/file_object_test.go +++ b/object/file_object_test.go @@ -1,28 +1,16 @@ package object import ( - "os" "testing" "github.com/stretchr/testify/assert" - "go.uber.org/zap" "mort/config" - "mort/log" "mort/transforms" ) var imageInfo = transforms.ImageInfo{} -func TestMain(m *testing.M) { - logger, _ := zap.NewDevelopment() - zap.ReplaceGlobals(logger) - log.RegisterLogger(logger.Sugar()) - code := m.Run() - defer logger.Sync() - os.Exit(code) -} - func TestNewFileObjectWhenUnknowBucket(t *testing.T) { mortConfig := config.GetInstance() _, err := NewFileObject("/bucket/path", mortConfig) @@ -166,3 +154,30 @@ func TestNewFileObjecWithNestedParent(t *testing.T) { assert.Equal(t, "/parent.jpg", parent.Parent.Key, "parent of parent should have correct path") } + +func BenchmarkNewFileObject(b *testing.B) { + + benchmarks := []struct{ + path string + configPath string + } { + {"/bucket/width/thumb_121332.jpg", "testdata/bucket-transform-parent-storage.yml"}, + {"/bucket/parent.jpg", "testdata/bucket-transform.yml"}, + } + + b.ReportAllocs() + for _, bm := range benchmarks { + config := config.Config{} + err := config.Load(bm.configPath) + if err != nil { + panic(err) + } + + b.Run(bm.path, func(b *testing.B) { + for i := 0; i < b.N; i++ { + NewFileObject(bm.path, &config) + } + }) + } + +} diff --git a/object/testdata/bucket-transform-parent-storage.yml b/object/testdata/bucket-transform-parent-storage.yml index eefdd74..2239ba6 100644 --- a/object/testdata/bucket-transform-parent-storage.yml +++ b/object/testdata/bucket-transform-parent-storage.yml @@ -5,6 +5,7 @@ buckets: kind: "presets" parentStorage: "other" parentBucket: "bucket" + resultKey: "hash" presets: blog_small: quality: 75 diff --git a/processor.go b/processor.go index 3cc7e55..0aacb26 100644 --- a/processor.go +++ b/processor.go @@ -42,7 +42,7 @@ type RequestProcessor struct { func (r *RequestProcessor) Init(max int, l lock.Lock) { r.queue = make(chan requestMessage, max) r.collapse = l - r.throttler = throttler.New(10) + r.throttler = throttler.New(max) } func (r *RequestProcessor) Process(req *http.Request, obj *object.FileObject) *response.Response{