Skip to content
Snippets Groups Projects
Commit 347e74f1 authored by Travis Ralston's avatar Travis Ralston
Browse files

Improve error handling around redis cache population on upload

parent 07183614
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ package upload
import (
"io"
"github.com/getsentry/sentry-go"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/redislib"
)
......@@ -18,6 +19,7 @@ func PopulateCacheAsync(ctx rcontext.RequestContext, reader io.Reader, size int6
err = redislib.StoreMedia(ctx, sha256hash, reader, size)
if err != nil {
ctx.Log.Debug("Not populating cache due to error: ", err)
sentry.CaptureException(err)
return
}
}()
......
......@@ -39,19 +39,31 @@ func StoreMedia(ctx rcontext.RequestContext, hash string, content io.Reader, siz
return err
}
cleanup := func() {
if delErr := DeleteMedia(ctx, hash); delErr != nil {
ctx.Log.Warn("Error while attempting to clean up cache during another error: ", delErr)
sentry.CaptureException(delErr)
}
}
buf := make([]byte, appendBufferSize)
for {
read, err := content.Read(buf)
eof := errors.Is(err, io.EOF)
eof := false
if err != nil {
if errors.Is(err, io.EOF) {
eof = true
} else {
cleanup()
return err
}
}
if read > 0 {
if err = ring.ForEachShard(ctx.Context, func(ctx2 context.Context, client *redis.Client) error {
res := client.Append(ctx2, hash, string(buf[0:read]))
return res.Err()
}); err != nil {
if delErr := DeleteMedia(ctx, hash); delErr != nil {
ctx.Log.Warn("Error while attempting to clean up cache during another error: ", delErr)
sentry.CaptureException(delErr)
}
cleanup()
return err
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment