Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the initial synchronization of a sharing faster #4147

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions model/sharing/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sharing

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -18,8 +19,8 @@ import (
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/realtime"
multierror "github.com/hashicorp/go-multierror"
"github.com/labstack/echo/v4"
"golang.org/x/sync/errgroup"
)

// UploadMsg is used for jobs on the share-upload worker.
Expand Down Expand Up @@ -52,25 +53,30 @@ func (s *Sharing) Upload(inst *instance.Instance, errors int) error {
}

lastTry := errors+1 == MaxRetries
for i := 0; i < BatchSize; i++ {
if len(members) == 0 {
break
}
m := members[0]
members = members[1:]
more, err := s.UploadTo(inst, m, lastTry)
if err != nil {
errm = multierror.Append(errm, err)
}
if more {
members = append(members, m)
}
done := true
g, _ := errgroup.WithContext(context.Background())
for i := range members {
m := members[i]
g.Go(func() error {
Comment on lines +59 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to pass m as an argument to the goroutine to create a closure?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errgroup doesn't allow to pass arguments to the goroutine. That's why I have written for i := range members { m := members[i] instead of the shorter for _, m := range members. It forces m to have the correct range.

In the future, I hope that https://github.com/golang/go/wiki/LoopvarExperiment will make things easier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation.

for i := 0; i < BatchSize; i++ {
more, err := s.UploadTo(inst, m, lastTry)
if err != nil {
return err
}
if !more {
return nil
}
}
done = false
return nil
})
}
err := g.Wait()

if errm != nil {
if err != nil {
s.retryWorker(inst, "share-upload", errors)
inst.Logger().WithNamespace("upload").Infof("errm=%s\n", errm)
} else if len(members) > 0 {
inst.Logger().WithNamespace("upload").Infof("err=%s\n", err)
} else if !done {
s.pushJob(inst, "share-upload")
}
return errm
Expand Down