Skip to content

Commit

Permalink
perf: make files upload async in the background (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLL32 authored Jul 17, 2024
1 parent 72424ca commit a2039b4
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions internal/file/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,39 @@ func (s service) Create(ctx context.Context, req CreateFileRequest) (
// When a new file has been uploaded, we create a new doc in the db.
if err != nil && err.Error() == ErrDocumentNotFound {

// Create a context with a timeout that will abort the upload if it takes
// more than the passed in timeout.
uploadCtx, cancelFn := context.WithTimeout(context.Background(), fileUploadTimeout)

// Ensure the context is canceled to prevent leaking.
defer cancelFn()

// TODO: check if the file is already in Obj.
err = s.objSto.Upload(uploadCtx, s.bucket, sha256,
bytes.NewReader(fileContent))
if err != nil {
return File{}, err
}
go func() {
existsCtx, cancelExistsFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelExistsFn()

if ok, _ := s.objSto.Exists(existsCtx, s.bucket, sha256); ok {
return
}

var err error

for attempt := 0; attempt < 3; attempt++ {
// Create a context with a timeout that will abort the upload if it takes
// more than the passed in timeout.
uploadCtx, cancelUploadFn := context.WithTimeout(context.Background(), fileUploadTimeout)

// Ensure the context is canceled to prevent leaking.
defer cancelUploadFn()

err = s.objSto.Upload(uploadCtx, s.bucket, sha256, bytes.NewReader(fileContent))
if err == nil {
break
}
s.logger.With(uploadCtx).Error(err)

// Give time to the system to recover
time.Sleep(10 * time.Second)
}

if err != nil {
s.logger.Error(err)
return
}
}()

// Get the source of the HTTP request from the ctx.
source, _ := ctx.Value(entity.SourceKey).(string)
Expand Down

0 comments on commit a2039b4

Please sign in to comment.