From 64429d736dc797d30f1aec5c2edfbfebefa136e4 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Tue, 11 Aug 2020 14:32:04 +0300 Subject: [PATCH] Refactoring API layer - logging RequestID - should return error, when headers already received Signed-off-by: Evgeniy Kulikov --- api/layer/container.go | 12 ++++++++++++ api/layer/object.go | 11 +++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/api/layer/container.go b/api/layer/container.go index 5d3308db..550ae05e 100644 --- a/api/layer/container.go +++ b/api/layer/container.go @@ -7,6 +7,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/container" "github.com/nspcc-dev/neofs-api-go/refs" "github.com/nspcc-dev/neofs-api-go/service" + "github.com/nspcc-dev/neofs-s3-gate/api" "github.com/nspcc-dev/neofs-s3-gate/auth" "go.uber.org/zap" ) @@ -28,9 +29,11 @@ type ( ) func (n *layer) containerInfo(ctx context.Context, cid refs.CID) (*BucketInfo, error) { + rid := api.GetRequestID(ctx) bearer, err := auth.GetBearerToken(ctx) if err != nil { n.log.Error("could not receive bearer token", + zap.String("request_id", rid), zap.Error(err)) return nil, err } @@ -44,6 +47,7 @@ func (n *layer) containerInfo(ctx context.Context, cid refs.CID) (*BucketInfo, e if err = service.SignRequestData(n.key, req); err != nil { n.log.Error("could not prepare request", + zap.String("request_id", rid), zap.Error(err)) return nil, err } @@ -51,6 +55,7 @@ func (n *layer) containerInfo(ctx context.Context, cid refs.CID) (*BucketInfo, e conn, err := n.cli.GetConnection(ctx) if err != nil { n.log.Error("could not prepare client", + zap.String("request_id", rid), zap.Error(err)) return nil, err } @@ -62,6 +67,7 @@ func (n *layer) containerInfo(ctx context.Context, cid refs.CID) (*BucketInfo, e res, err := container.NewServiceClient(conn).Get(ctx, req) if err != nil { n.log.Error("could not list buckets", + zap.String("request_id", rid), zap.Error(err)) return nil, err } @@ -76,9 +82,11 @@ func (n *layer) containerInfo(ctx context.Context, cid refs.CID) (*BucketInfo, e } func (n *layer) containerList(ctx context.Context) ([]BucketInfo, error) { + rid := api.GetRequestID(ctx) bearer, err := auth.GetBearerToken(ctx) if err != nil { n.log.Error("could not receive bearer token", + zap.String("request_id", rid), zap.Error(err)) return nil, err } @@ -92,6 +100,7 @@ func (n *layer) containerList(ctx context.Context) ([]BucketInfo, error) { if err := service.SignRequestData(n.key, req); err != nil { n.log.Error("could not prepare request", + zap.String("request_id", rid), zap.Error(err)) return nil, err } @@ -99,6 +108,7 @@ func (n *layer) containerList(ctx context.Context) ([]BucketInfo, error) { conn, err := n.cli.GetConnection(ctx) if err != nil { n.log.Error("could not prepare client", + zap.String("request_id", rid), zap.Error(err)) return nil, err } @@ -110,6 +120,7 @@ func (n *layer) containerList(ctx context.Context) ([]BucketInfo, error) { res, err := container.NewServiceClient(conn).List(ctx, req) if err != nil { n.log.Error("could not list buckets", + zap.String("request_id", rid), zap.Error(err)) return nil, err } @@ -119,6 +130,7 @@ func (n *layer) containerList(ctx context.Context) ([]BucketInfo, error) { info, err := n.containerInfo(ctx, cid) if err != nil { n.log.Error("could not fetch container info", + zap.String("request_id", rid), zap.Error(err)) continue } diff --git a/api/layer/object.go b/api/layer/object.go index 55b7be11..c7d160db 100644 --- a/api/layer/object.go +++ b/api/layer/object.go @@ -278,8 +278,9 @@ func receiveObject(cli object.Service_GetClient) (*object.Object, error) { switch o := resp.R.(type) { case *object.GetResponse_Object: - - if _, hdr := o.Object.LastHeader(object.HeaderType(object.TombstoneHdr)); hdr != nil { + if obj != nil { + return nil, errors.New("object headers already received") + } else if _, hdr := o.Object.LastHeader(object.HeaderType(object.TombstoneHdr)); hdr != nil { return nil, errors.New("object already removed") } @@ -480,9 +481,11 @@ func (n *layer) storageGroupPut(ctx context.Context, p sgParams) (*object.Object return nil, err } - client := object.NewServiceClient(conn) // todo: think about timeout - putClient, err := client.Put(ctx) + ctx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + + putClient, err := object.NewServiceClient(conn).Put(ctx) if err != nil { return nil, err }