From defb9dc797c3d9e6eeea92793f5b97336afb5aa7 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Sat, 28 Dec 2024 11:36:46 +0300 Subject: [PATCH] [#1589] client: Add ctx.Done check To not to perform locking, checking cached connections, create connection if context is already canceled. Signed-off-by: Dmitrii Stepanov --- pkg/services/object/internal/client/client.go | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pkg/services/object/internal/client/client.go b/pkg/services/object/internal/client/client.go index 3e8832640..08900a3d5 100644 --- a/pkg/services/object/internal/client/client.go +++ b/pkg/services/object/internal/client/client.go @@ -144,6 +144,12 @@ func (x GetObjectRes) Object() *objectSDK.Object { // // GetObject ignores the provided session if it is not related to the requested objectSDK. func GetObject(ctx context.Context, prm GetObjectPrm) (*GetObjectRes, error) { + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + // here we ignore session if it is opened for other object since such // request will almost definitely fail. The case can occur, for example, // when session is bound to the parent object and child object is requested. @@ -238,6 +244,12 @@ func (x HeadObjectRes) Header() *objectSDK.Object { // // HeadObject ignores the provided session if it is not related to the requested objectSDK. func HeadObject(ctx context.Context, prm HeadObjectPrm) (*HeadObjectRes, error) { + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + // see details in same statement of GetObject if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) { prm.ClientParams.Session = prm.tokenSession @@ -333,6 +345,12 @@ const maxInitialBufferSize = 1024 * 1024 // 1 MiB // // PayloadRange ignores the provided session if it is not related to the requested objectSDK. func PayloadRange(ctx context.Context, prm PayloadRangePrm) (*PayloadRangeRes, error) { + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + // see details in same statement of GetObject if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) { prm.ClientParams.Session = prm.tokenSession @@ -401,6 +419,12 @@ func PutObject(ctx context.Context, prm PutObjectPrm) (*PutObjectRes, error) { ctx, span := tracing.StartSpanFromContext(ctx, "client.PutObject") defer span.End() + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + prmCli := client.PrmObjectPutInit{ XHeaders: prm.calculateXHeaders(), BearerToken: prm.tokenBearer, @@ -443,6 +467,12 @@ func PutObjectSingle(ctx context.Context, prm PutObjectPrm) (*PutObjectRes, erro ctx, span := tracing.StartSpanFromContext(ctx, "client.PutObjectSingle") defer span.End() + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + objID, isSet := prm.obj.ID() if !isSet { return nil, errors.New("missing object id") @@ -505,6 +535,12 @@ func (x SearchObjectsRes) IDList() []oid.ID { // // Returns any error which prevented the operation from completing correctly in error return. func SearchObjects(ctx context.Context, prm SearchObjectsPrm) (*SearchObjectsRes, error) { + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + prm.cliPrm.Local = prm.local prm.cliPrm.Session = prm.tokenSession prm.cliPrm.BearerToken = prm.tokenBearer