[#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 <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2024-12-28 11:36:46 +03:00
parent a2249b981a
commit defb9dc797
Signed by: dstepanov-yadro
GPG key ID: 237AF1A763293BC0

View file

@ -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