Refactoring API layer

- logging RequestID
- should return error, when headers already received

Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
Evgeniy Kulikov 2020-08-11 14:32:04 +03:00
parent 59e26d45fd
commit 64429d736d
2 changed files with 19 additions and 4 deletions

View file

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

View file

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