From 35deb1a8e277df88ea1ce55c6751fafff97fe903 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 20 Feb 2024 15:00:35 -0800 Subject: [PATCH] do not block on send channels under high load (#19090) all send channels must compete with `ctx` if not they will perpetually stay alive. --- cmd/erasure-object.go | 6 +++++- cmd/metacache-set.go | 5 ++++- cmd/metacache-walk.go | 12 ++++++++++-- cmd/xl-storage.go | 14 +++++++++++--- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/cmd/erasure-object.go b/cmd/erasure-object.go index 36e8b349c..a5162a899 100644 --- a/cmd/erasure-object.go +++ b/cmd/erasure-object.go @@ -937,7 +937,11 @@ func (er erasureObjects) getObjectFileInfo(ctx context.Context, bucket, object s onlineDisks[i] = nil } - mrfCheck <- fi.ShallowCopy() + select { + case mrfCheck <- fi.ShallowCopy(): + case <-ctx.Done(): + return fi, onlineMeta, onlineDisks, toObjectErr(ctx.Err(), bucket, object) + } return fi, onlineMeta, onlineDisks, nil } diff --git a/cmd/metacache-set.go b/cmd/metacache-set.go index 39eac9358..e2b336ec7 100644 --- a/cmd/metacache-set.go +++ b/cmd/metacache-set.go @@ -219,7 +219,10 @@ func (o *listPathOptions) gatherResults(ctx context.Context, in <-chan metaCache // Do not return io.EOF if resCh != nil { resErr = nil - resCh <- results + select { + case resCh <- results: + case <-ctx.Done(): + } resCh = nil returned = true } diff --git a/cmd/metacache-walk.go b/cmd/metacache-walk.go index a23847f3f..e01760aad 100644 --- a/cmd/metacache-walk.go +++ b/cmd/metacache-walk.go @@ -299,7 +299,11 @@ func (s *xlStorage) WalkDir(ctx context.Context, opts WalkDirOptions, wr io.Writ // If directory entry on stack before this, pop it now. for len(dirStack) > 0 && dirStack[len(dirStack)-1] < meta.name { pop := dirStack[len(dirStack)-1] - out <- metaCacheEntry{name: pop} + select { + case <-ctx.Done(): + return ctx.Err() + case out <- metaCacheEntry{name: pop}: + } if opts.Recursive { // Scan folder we found. Should be in correct sort order where we are. err := scanDir(pop) @@ -368,7 +372,11 @@ func (s *xlStorage) WalkDir(ctx context.Context, opts WalkDirOptions, wr io.Writ return ctx.Err() } pop := dirStack[len(dirStack)-1] - out <- metaCacheEntry{name: pop} + select { + case <-ctx.Done(): + return ctx.Err() + case out <- metaCacheEntry{name: pop}: + } if opts.Recursive { // Scan folder we found. Should be in correct sort order where we are. logger.LogIf(ctx, scanDir(pop)) diff --git a/cmd/xl-storage.go b/cmd/xl-storage.go index 076ff9477..9350cfc33 100644 --- a/cmd/xl-storage.go +++ b/cmd/xl-storage.go @@ -2869,14 +2869,22 @@ func (s *xlStorage) ReadMultiple(ctx context.Context, req ReadMultipleReq, resp if req.MaxSize > 0 && int64(len(data)) > req.MaxSize { r.Exists = true r.Error = fmt.Sprintf("max size (%d) exceeded: %d", req.MaxSize, len(data)) - resp <- r - continue + select { + case <-ctx.Done(): + return ctx.Err() + case resp <- r: + continue + } } found++ r.Exists = true r.Data = data r.Modtime = mt - resp <- r + select { + case <-ctx.Done(): + return ctx.Err() + case resp <- r: + } if req.MaxResults > 0 && found >= req.MaxResults { return nil }