Skip to content

Commit

Permalink
refactor: Little parallelism optimization
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Sep 3, 2022
1 parent 3ec892a commit 9d19fd8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
4 changes: 3 additions & 1 deletion pkg/crud/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/sha"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

func (a App) getWithMessage(w http.ResponseWriter, r *http.Request, request provider.Request, message renderer.Message) (renderer.Page, error) {
Expand Down Expand Up @@ -139,7 +141,7 @@ func (a App) handleDir(w http.ResponseWriter, r *http.Request, request provider.
}

func (a App) listFiles(r *http.Request, request provider.Request, item absto.Item) (items []absto.Item, err error) {
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "files")
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "files", trace.WithAttributes(attribute.String("item", item.Pathname)))
defer end()

if query.GetBool(r, "search") {
Expand Down
6 changes: 4 additions & 2 deletions pkg/crud/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

func (a App) list(ctx context.Context, request provider.Request, message renderer.Message, item absto.Item, files []absto.Item) (renderer.Page, error) {
ctx, end := tracer.StartSpan(ctx, a.tracer, "list")
ctx, end := tracer.StartSpan(ctx, a.tracer, "list", trace.WithAttributes(attribute.String("item", item.Pathname)))
defer end()

directoryAggregate, err := a.exifApp.GetAggregateFor(ctx, item)
Expand All @@ -28,7 +30,7 @@ func (a App) list(ctx context.Context, request provider.Request, message rendere
}

items := make([]provider.RenderItem, len(files))
wg := concurrent.NewLimited(6)
wg := concurrent.NewLimited(provider.MaxConcurrency)

var thumbnails map[string]absto.Item
wg.Go(func() {
Expand Down
37 changes: 28 additions & 9 deletions pkg/crud/story.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,49 @@ import (
"net/http"

absto "github.com/ViBiOh/absto/pkg/model"
exas "github.com/ViBiOh/exas/pkg/model"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/fibr/pkg/thumbnail"
"github.com/ViBiOh/httputils/v4/pkg/concurrent"
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/httputils/v4/pkg/renderer"
"github.com/ViBiOh/httputils/v4/pkg/tracer"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

func (a App) story(r *http.Request, request provider.Request, item absto.Item, files []absto.Item) (renderer.Page, error) {
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "story")
ctx, end := tracer.StartSpan(r.Context(), a.tracer, "story", trace.WithAttributes(attribute.String("item", item.Pathname)))
defer end()

items := make([]provider.StoryItem, 0, len(files))
var cover cover
var hasMap bool

directoryAggregate, err := a.exifApp.GetAggregateFor(ctx, item)
if err != nil && !absto.IsNotExist(err) {
logger.WithField("fn", "crud.story").WithField("item", request.Path).Error("get aggregate: %s", err)
}
wg := concurrent.NewSimple()

exifs, err := a.exifApp.ListExifFor(ctx, files...)
if err != nil {
logger.WithField("fn", "crud.story").WithField("item", request.Path).Error("list exifs: %s", err)
}
var directoryAggregate provider.Aggregate
var exifs map[string]exas.Exif

wg.Go(func() {
var err error

directoryAggregate, err = a.exifApp.GetAggregateFor(ctx, item)
if err != nil && !absto.IsNotExist(err) {
logger.WithField("fn", "crud.story").WithField("item", request.Path).Error("get aggregate: %s", err)
}
})

wg.Go(func() {
var err error

exifs, err = a.exifApp.ListExifFor(ctx, files...)
if err != nil {
logger.WithField("fn", "crud.story").WithField("item", request.Path).Error("list exifs: %s", err)
}
})

wg.Wait()

for _, file := range files {
if cover.IsZero() || (len(directoryAggregate.Cover) != 0 && cover.Img.Name != directoryAggregate.Cover) {
Expand Down

0 comments on commit 9d19fd8

Please sign in to comment.