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

fix(artifact): add simple rate limiting to use minIO #111

Merged
merged 2 commits into from
Oct 8, 2024
Merged
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
40 changes: 26 additions & 14 deletions pkg/minio/knowledgebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"fmt"
"sync"

"go.uber.org/zap"

"github.com/instill-ai/artifact-backend/pkg/logger"
"github.com/instill-ai/artifact-backend/pkg/utils"
"go.uber.org/zap"
)

// KnowledgeBaseI is the interface for knowledge base related operations.
Expand Down Expand Up @@ -57,6 +58,7 @@ type ChunkUIDType string
type ChunkContentType []byte

// SaveTextChunks saves batch of chunks(text files) to MinIO.
// rate limiting is implemented to avoid overwhelming the MinIO server.
func (m *Minio) SaveTextChunks(ctx context.Context, kbUID string, chunks map[ChunkUIDType]ChunkContentType) error {
logger, _ := logger.GetZapLogger(ctx)
var wg sync.WaitGroup
Expand All @@ -65,21 +67,31 @@ func (m *Minio) SaveTextChunks(ctx context.Context, kbUID string, chunks map[Chu
ErrorMessage string
}
errorUIDChan := make(chan ChunkError, len(chunks))

counter := 0
maxConcurrentUploads := 50
for chunkUID, chunkContent := range chunks {
wg.Add(1)
go utils.GoRecover(func() {
func(chunkUID ChunkUIDType, chunkContent ChunkContentType) {
defer wg.Done()
filePathName := m.GetChunkPathInKnowledgeBase(kbUID, string(chunkUID))

err := m.UploadBase64File(ctx, filePathName, base64.StdEncoding.EncodeToString(chunkContent), "text/plain")
if err != nil {
logger.Error("Failed to upload chunk after retries", zap.String("chunkUID", string(chunkUID)), zap.Error(err))
errorUIDChan <- ChunkError{ChunkUID: string(chunkUID), ErrorMessage: err.Error()}
return
}
}(chunkUID, chunkContent)
}, fmt.Sprintf("SaveTextChunks %s", chunkUID))
go utils.GoRecover(
func() {
func(chunkUID ChunkUIDType, chunkContent ChunkContentType) {
defer wg.Done()
filePathName := m.GetChunkPathInKnowledgeBase(kbUID, string(chunkUID))

err := m.UploadBase64File(ctx, filePathName, base64.StdEncoding.EncodeToString(chunkContent), "text/plain")
if err != nil {
logger.Error("Failed to upload chunk after retries", zap.String("chunkUID", string(chunkUID)), zap.Error(err))
errorUIDChan <- ChunkError{ChunkUID: string(chunkUID), ErrorMessage: err.Error()}
return
}
}(chunkUID, chunkContent)
}, fmt.Sprintf("SaveTextChunks %s", chunkUID))

counter++
if counter == maxConcurrentUploads {
wg.Wait()
counter = 0
}
}
wg.Wait()
close(errorUIDChan)
Expand Down
Loading