Skip to content

Some optimizations for uploading to minio #15255

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions modules/io/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package io

import "io"

// ReadSizer represents a reader which have size information
type ReadSizer interface {
io.Reader
Size() int64
}

type readSizer struct {
io.Reader
size int64
}

func (r *readSizer) Size() int64 {
return r.size
}

// WithSize binding io.Reader and size into a ReadSizer
func WithSize(rd io.Reader, size int64) ReadSizer {
return &readSizer{
Reader: rd,
size: size,
}
}
9 changes: 9 additions & 0 deletions modules/lfs/content_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"

"code.gitea.io/gitea/models"
myio "code.gitea.io/gitea/modules/io"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/storage"
)
Expand Down Expand Up @@ -125,6 +126,10 @@ type hashingReader struct {
expectedHash string
}

var (
_ myio.ReadSizer = &hashingReader{}
)

func (r *hashingReader) Read(b []byte) (int, error) {
n, err := r.internal.Read(b)

Expand All @@ -150,6 +155,10 @@ func (r *hashingReader) Read(b []byte) (int, error) {
return n, err
}

func (r *hashingReader) Size() int64 {
return r.expectedSize
}

func newHashingReader(expectedSize int64, expectedHash string, reader io.Reader) *hashingReader {
return &hashingReader{
internal: reader,
Expand Down
3 changes: 2 additions & 1 deletion modules/migrations/gitea_uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
myio "code.gitea.io/gitea/modules/io"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/migrations/base"
"code.gitea.io/gitea/modules/repository"
Expand Down Expand Up @@ -283,7 +284,7 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
}
}
defer rc.Close()
_, err = storage.Attachments.Save(attach.RelativePath(), rc)
_, err = storage.Attachments.Save(attach.RelativePath(), myio.WithSize(rc, attach.Size))
return err
}()
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion modules/storage/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

myio "code.gitea.io/gitea/modules/io"
"code.gitea.io/gitea/modules/log"

"github.com/minio/minio-go/v7"
Expand Down Expand Up @@ -132,12 +133,16 @@ func (m *MinioStorage) Open(path string) (Object, error) {

// Save save a file to minio
func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
var size int64 = -1
if sizer, ok := r.(myio.ReadSizer); ok {
size = sizer.Size()
}
uploadInfo, err := m.client.PutObject(
m.ctx,
m.bucket,
m.buildMinioPath(path),
r,
-1,
size,
minio.PutObjectOptions{ContentType: "application/octet-stream"},
)
if err != nil {
Expand Down