[#6] services/util: Remove HandleUnaryRequest

There is no need in a wrapper with many from-`interface{}` conversions.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2022-12-30 17:48:50 +03:00
parent f762855a5d
commit 8269c67701
19 changed files with 86 additions and 400 deletions

View file

@ -19,10 +19,8 @@ func initAccountingService(c *cfg) {
server := accountingTransportGRPC.New( server := accountingTransportGRPC.New(
accountingService.NewSignService( accountingService.NewSignService(
&c.key.PrivateKey, &c.key.PrivateKey,
accountingService.NewResponseService(
accountingService.NewExecutionService( accountingService.NewExecutionService(
accounting.NewExecutor(balanceMorphWrapper), accounting.NewExecutor(balanceMorphWrapper),
),
c.respSvc, c.respSvc,
), ),
), ),

View file

@ -196,16 +196,13 @@ func initContainerService(c *cfg) {
server := containerTransportGRPC.New( server := containerTransportGRPC.New(
containerService.NewSignService( containerService.NewSignService(
&c.key.PrivateKey, &c.key.PrivateKey,
containerService.NewResponseService(
&usedSpaceService{ &usedSpaceService{
Server: containerService.NewExecutionService(containerMorph.NewExecutor(cnrRdr, cnrWrt)), Server: containerService.NewExecutionService(containerMorph.NewExecutor(cnrRdr, cnrWrt), c.respSvc),
loadWriterProvider: loadRouter, loadWriterProvider: loadRouter,
loadPlacementBuilder: loadPlacementBuilder, loadPlacementBuilder: loadPlacementBuilder,
routeBuilder: routeBuilder, routeBuilder: routeBuilder,
cfg: c, cfg: c,
}, },
c.respSvc,
),
), ),
) )
@ -566,6 +563,8 @@ func (c *usedSpaceService) AnnounceUsedSpace(ctx context.Context, req *container
resp := new(containerV2.AnnounceUsedSpaceResponse) resp := new(containerV2.AnnounceUsedSpaceResponse)
resp.SetBody(respBody) resp.SetBody(respBody)
c.cfg.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }

View file

@ -152,7 +152,6 @@ func initNetmapService(c *cfg) {
server := netmapTransportGRPC.New( server := netmapTransportGRPC.New(
netmapService.NewSignService( netmapService.NewSignService(
&c.key.PrivateKey, &c.key.PrivateKey,
netmapService.NewResponseService(
netmapService.NewExecutionService( netmapService.NewExecutionService(
c, c,
c.apiVersion, c.apiVersion,
@ -162,7 +161,6 @@ func initNetmapService(c *cfg) {
morphClientNetMap: c.cfgNetmap.wrapper, morphClientNetMap: c.cfgNetmap.wrapper,
msPerBlockRdr: c.cfgMorph.client.MsPerBlock, msPerBlockRdr: c.cfgMorph.client.MsPerBlock,
}, },
),
c.respSvc, c.respSvc,
), ),
), ),

View file

@ -198,7 +198,6 @@ func initReputationService(c *cfg) {
server := grpcreputation.New( server := grpcreputation.New(
reputationrpc.NewSignService( reputationrpc.NewSignService(
&c.key.PrivateKey, &c.key.PrivateKey,
reputationrpc.NewResponseService(
&reputationServer{ &reputationServer{
cfg: c, cfg: c,
log: c.log, log: c.log,
@ -206,8 +205,6 @@ func initReputationService(c *cfg) {
intermediateRouter: intermediateTrustRouter, intermediateRouter: intermediateTrustRouter,
routeBuilder: localRouteBuilder, routeBuilder: localRouteBuilder,
}, },
c.respSvc,
),
), ),
) )
@ -288,6 +285,7 @@ func (s *reputationServer) AnnounceLocalTrust(ctx context.Context, req *v2reputa
resp := new(v2reputation.AnnounceLocalTrustResponse) resp := new(v2reputation.AnnounceLocalTrustResponse)
resp.SetBody(new(v2reputation.AnnounceLocalTrustResponseBody)) resp.SetBody(new(v2reputation.AnnounceLocalTrustResponseBody))
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }
@ -316,6 +314,7 @@ func (s *reputationServer) AnnounceIntermediateResult(ctx context.Context, req *
resp := new(v2reputation.AnnounceIntermediateResultResponse) resp := new(v2reputation.AnnounceIntermediateResultResponse)
resp.SetBody(new(v2reputation.AnnounceIntermediateResultResponseBody)) resp.SetBody(new(v2reputation.AnnounceIntermediateResultResponseBody))
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }

View file

@ -53,10 +53,7 @@ func initSessionService(c *cfg) {
server := sessionTransportGRPC.New( server := sessionTransportGRPC.New(
sessionSvc.NewSignService( sessionSvc.NewSignService(
&c.key.PrivateKey, &c.key.PrivateKey,
sessionSvc.NewResponseService( sessionSvc.NewExecutionService(c.privateTokenStore, c.respSvc, c.log),
sessionSvc.NewExecutionService(c.privateTokenStore, c.log),
c.respSvc,
),
), ),
) )

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/TrueCloudLab/frostfs-api-go/v2/accounting" "github.com/TrueCloudLab/frostfs-api-go/v2/accounting"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util/response"
) )
type ServiceExecutor interface { type ServiceExecutor interface {
@ -13,12 +14,14 @@ type ServiceExecutor interface {
type executorSvc struct { type executorSvc struct {
exec ServiceExecutor exec ServiceExecutor
respSvc *response.Service
} }
// NewExecutionService wraps ServiceExecutor and returns Accounting Service interface. // NewExecutionService wraps ServiceExecutor and returns Accounting Service interface.
func NewExecutionService(exec ServiceExecutor) Server { func NewExecutionService(exec ServiceExecutor, respSvc *response.Service) Server {
return &executorSvc{ return &executorSvc{
exec: exec, exec: exec,
respSvc: respSvc,
} }
} }
@ -31,5 +34,6 @@ func (s *executorSvc) Balance(ctx context.Context, req *accounting.BalanceReques
resp := new(accounting.BalanceResponse) resp := new(accounting.BalanceResponse)
resp.SetBody(respBody) resp.SetBody(respBody)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }

View file

@ -1,37 +0,0 @@
package accounting
import (
"context"
"github.com/TrueCloudLab/frostfs-api-go/v2/accounting"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util/response"
)
type responseService struct {
respSvc *response.Service
svc Server
}
// NewResponseService returns accounting service instance that passes internal service
// call to response service.
func NewResponseService(accSvc Server, respSvc *response.Service) Server {
return &responseService{
respSvc: respSvc,
svc: accSvc,
}
}
func (s *responseService) Balance(ctx context.Context, req *accounting.BalanceRequest) (*accounting.BalanceResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.Balance(ctx, req.(*accounting.BalanceRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*accounting.BalanceResponse), nil
}

View file

@ -6,6 +6,7 @@ import (
"github.com/TrueCloudLab/frostfs-api-go/v2/container" "github.com/TrueCloudLab/frostfs-api-go/v2/container"
"github.com/TrueCloudLab/frostfs-api-go/v2/session" "github.com/TrueCloudLab/frostfs-api-go/v2/session"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util/response"
) )
type ServiceExecutor interface { type ServiceExecutor interface {
@ -21,12 +22,15 @@ type executorSvc struct {
Server Server
exec ServiceExecutor exec ServiceExecutor
respSvc *response.Service
} }
// NewExecutionService wraps ServiceExecutor and returns Container Service interface. // NewExecutionService wraps ServiceExecutor and returns Container Service interface.
func NewExecutionService(exec ServiceExecutor) Server { func NewExecutionService(exec ServiceExecutor, respSvc *response.Service) Server {
return &executorSvc{ return &executorSvc{
exec: exec, exec: exec,
respSvc: respSvc,
} }
} }
@ -44,6 +48,7 @@ func (s *executorSvc) Put(ctx context.Context, req *container.PutRequest) (*cont
resp := new(container.PutResponse) resp := new(container.PutResponse)
resp.SetBody(respBody) resp.SetBody(respBody)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }
@ -61,6 +66,7 @@ func (s *executorSvc) Delete(ctx context.Context, req *container.DeleteRequest)
resp := new(container.DeleteResponse) resp := new(container.DeleteResponse)
resp.SetBody(respBody) resp.SetBody(respBody)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }
@ -73,6 +79,7 @@ func (s *executorSvc) Get(ctx context.Context, req *container.GetRequest) (*cont
resp := new(container.GetResponse) resp := new(container.GetResponse)
resp.SetBody(respBody) resp.SetBody(respBody)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }
@ -85,6 +92,7 @@ func (s *executorSvc) List(ctx context.Context, req *container.ListRequest) (*co
resp := new(container.ListResponse) resp := new(container.ListResponse)
resp.SetBody(respBody) resp.SetBody(respBody)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }
@ -102,6 +110,7 @@ func (s *executorSvc) SetExtendedACL(ctx context.Context, req *container.SetExte
resp := new(container.SetExtendedACLResponse) resp := new(container.SetExtendedACLResponse)
resp.SetBody(respBody) resp.SetBody(respBody)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }
@ -114,5 +123,6 @@ func (s *executorSvc) GetExtendedACL(ctx context.Context, req *container.GetExte
resp := new(container.GetExtendedACLResponse) resp := new(container.GetExtendedACLResponse)
resp.SetBody(respBody) resp.SetBody(respBody)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }

View file

@ -1,115 +0,0 @@
package container
import (
"context"
"github.com/TrueCloudLab/frostfs-api-go/v2/container"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util/response"
)
type responseService struct {
respSvc *response.Service
svc Server
}
// NewResponseService returns container service instance that passes internal service
// call to response service.
func NewResponseService(cnrSvc Server, respSvc *response.Service) Server {
return &responseService{
respSvc: respSvc,
svc: cnrSvc,
}
}
func (s *responseService) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.Put(ctx, req.(*container.PutRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*container.PutResponse), nil
}
func (s *responseService) Delete(ctx context.Context, req *container.DeleteRequest) (*container.DeleteResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.Delete(ctx, req.(*container.DeleteRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*container.DeleteResponse), nil
}
func (s *responseService) Get(ctx context.Context, req *container.GetRequest) (*container.GetResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.Get(ctx, req.(*container.GetRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*container.GetResponse), nil
}
func (s *responseService) List(ctx context.Context, req *container.ListRequest) (*container.ListResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.List(ctx, req.(*container.ListRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*container.ListResponse), nil
}
func (s *responseService) SetExtendedACL(ctx context.Context, req *container.SetExtendedACLRequest) (*container.SetExtendedACLResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.SetExtendedACL(ctx, req.(*container.SetExtendedACLRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*container.SetExtendedACLResponse), nil
}
func (s *responseService) GetExtendedACL(ctx context.Context, req *container.GetExtendedACLRequest) (*container.GetExtendedACLResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.GetExtendedACL(ctx, req.(*container.GetExtendedACLRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*container.GetExtendedACLResponse), nil
}
func (s *responseService) AnnounceUsedSpace(ctx context.Context, req *container.AnnounceUsedSpaceRequest) (*container.AnnounceUsedSpaceResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.AnnounceUsedSpace(ctx, req.(*container.AnnounceUsedSpaceRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*container.AnnounceUsedSpaceResponse), nil
}

View file

@ -8,6 +8,7 @@ import (
"github.com/TrueCloudLab/frostfs-api-go/v2/netmap" "github.com/TrueCloudLab/frostfs-api-go/v2/netmap"
"github.com/TrueCloudLab/frostfs-api-go/v2/refs" "github.com/TrueCloudLab/frostfs-api-go/v2/refs"
"github.com/TrueCloudLab/frostfs-node/pkg/core/version" "github.com/TrueCloudLab/frostfs-node/pkg/core/version"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util/response"
netmapSDK "github.com/TrueCloudLab/frostfs-sdk-go/netmap" netmapSDK "github.com/TrueCloudLab/frostfs-sdk-go/netmap"
versionsdk "github.com/TrueCloudLab/frostfs-sdk-go/version" versionsdk "github.com/TrueCloudLab/frostfs-sdk-go/version"
) )
@ -18,6 +19,8 @@ type executorSvc struct {
state NodeState state NodeState
netInfo NetworkInfo netInfo NetworkInfo
respSvc *response.Service
} }
// NodeState encapsulates information // NodeState encapsulates information
@ -42,8 +45,8 @@ type NetworkInfo interface {
Dump(versionsdk.Version) (*netmapSDK.NetworkInfo, error) Dump(versionsdk.Version) (*netmapSDK.NetworkInfo, error)
} }
func NewExecutionService(s NodeState, v versionsdk.Version, netInfo NetworkInfo) Server { func NewExecutionService(s NodeState, v versionsdk.Version, netInfo NetworkInfo, respSvc *response.Service) Server {
if s == nil || netInfo == nil || !version.IsValid(v) { if s == nil || netInfo == nil || !version.IsValid(v) || respSvc == nil {
// this should never happen, otherwise it programmers bug // this should never happen, otherwise it programmers bug
panic("can't create netmap execution service") panic("can't create netmap execution service")
} }
@ -51,6 +54,7 @@ func NewExecutionService(s NodeState, v versionsdk.Version, netInfo NetworkInfo)
res := &executorSvc{ res := &executorSvc{
state: s, state: s,
netInfo: netInfo, netInfo: netInfo,
respSvc: respSvc,
} }
v.WriteToV2(&res.version) v.WriteToV2(&res.version)
@ -96,6 +100,7 @@ func (s *executorSvc) LocalNodeInfo(
resp := new(netmap.LocalNodeInfoResponse) resp := new(netmap.LocalNodeInfoResponse)
resp.SetBody(body) resp.SetBody(body)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }
@ -126,10 +131,11 @@ func (s *executorSvc) NetworkInfo(
resp := new(netmap.NetworkInfoResponse) resp := new(netmap.NetworkInfoResponse)
resp.SetBody(body) resp.SetBody(body)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }
func (s *executorSvc) Snapshot(_ context.Context, req *netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) { func (s *executorSvc) Snapshot(_ context.Context, _ *netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) {
var nm netmap.NetMap var nm netmap.NetMap
err := s.state.ReadCurrentNetMap(&nm) err := s.state.ReadCurrentNetMap(&nm)
@ -143,5 +149,6 @@ func (s *executorSvc) Snapshot(_ context.Context, req *netmap.SnapshotRequest) (
resp := new(netmap.SnapshotResponse) resp := new(netmap.SnapshotResponse)
resp.SetBody(body) resp.SetBody(body)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }

View file

@ -1,63 +0,0 @@
package netmap
import (
"context"
"github.com/TrueCloudLab/frostfs-api-go/v2/netmap"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util/response"
)
type responseService struct {
respSvc *response.Service
svc Server
}
// NewResponseService returns netmap service instance that passes internal service
// call to response service.
func NewResponseService(nmSvc Server, respSvc *response.Service) Server {
return &responseService{
respSvc: respSvc,
svc: nmSvc,
}
}
func (s *responseService) LocalNodeInfo(ctx context.Context, req *netmap.LocalNodeInfoRequest) (*netmap.LocalNodeInfoResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.LocalNodeInfo(ctx, req.(*netmap.LocalNodeInfoRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*netmap.LocalNodeInfoResponse), nil
}
func (s *responseService) NetworkInfo(ctx context.Context, req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.NetworkInfo(ctx, req.(*netmap.NetworkInfoRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*netmap.NetworkInfoResponse), nil
}
func (s *responseService) Snapshot(ctx context.Context, req *netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.Snapshot(ctx, req.(*netmap.SnapshotRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*netmap.SnapshotResponse), nil
}

View file

@ -91,16 +91,13 @@ func (s *ResponseService) Put(ctx context.Context) (PutObjectStream, error) {
} }
func (s *ResponseService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) { func (s *ResponseService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req, resp, err := s.svc.Head(ctx, req)
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.Head(ctx, req.(*object.HeadRequest))
},
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.(*object.HeadResponse), nil s.respSvc.SetMeta(resp)
return resp, nil
} }
func (s *searchStreamResponser) Send(resp *object.SearchResponse) error { func (s *searchStreamResponser) Send(resp *object.SearchResponse) error {
@ -117,16 +114,13 @@ func (s *ResponseService) Search(req *object.SearchRequest, stream SearchStream)
} }
func (s *ResponseService) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) { func (s *ResponseService) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req, resp, err := s.svc.Delete(ctx, req)
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.Delete(ctx, req.(*object.DeleteRequest))
},
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.(*object.DeleteResponse), nil s.respSvc.SetMeta(resp)
return resp, nil
} }
func (s *getRangeStreamResponser) Send(resp *object.GetRangeResponse) error { func (s *getRangeStreamResponser) Send(resp *object.GetRangeResponse) error {
@ -143,14 +137,11 @@ func (s *ResponseService) GetRange(req *object.GetRangeRequest, stream GetObject
} }
func (s *ResponseService) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) { func (s *ResponseService) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req, resp, err := s.svc.GetRangeHash(ctx, req)
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.GetRangeHash(ctx, req.(*object.GetRangeHashRequest))
},
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return resp.(*object.GetRangeHashResponse), nil s.respSvc.SetMeta(resp)
return resp, nil
} }

View file

@ -1,50 +0,0 @@
package reputationrpc
import (
"context"
"github.com/TrueCloudLab/frostfs-api-go/v2/reputation"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util/response"
)
type responseService struct {
respSvc *response.Service
svc Server
}
// NewResponseService returns reputation service server instance that passes
// internal service call to response service.
func NewResponseService(cnrSvc Server, respSvc *response.Service) Server {
return &responseService{
respSvc: respSvc,
svc: cnrSvc,
}
}
func (s *responseService) AnnounceLocalTrust(ctx context.Context, req *reputation.AnnounceLocalTrustRequest) (*reputation.AnnounceLocalTrustResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.AnnounceLocalTrust(ctx, req.(*reputation.AnnounceLocalTrustRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*reputation.AnnounceLocalTrustResponse), nil
}
func (s *responseService) AnnounceIntermediateResult(ctx context.Context, req *reputation.AnnounceIntermediateResultRequest) (*reputation.AnnounceIntermediateResultResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.AnnounceIntermediateResult(ctx, req.(*reputation.AnnounceIntermediateResultRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*reputation.AnnounceIntermediateResultResponse), nil
}

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/TrueCloudLab/frostfs-api-go/v2/session" "github.com/TrueCloudLab/frostfs-api-go/v2/session"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util/response"
"github.com/TrueCloudLab/frostfs-node/pkg/util/logger" "github.com/TrueCloudLab/frostfs-node/pkg/util/logger"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -16,14 +17,17 @@ type ServiceExecutor interface {
type executorSvc struct { type executorSvc struct {
exec ServiceExecutor exec ServiceExecutor
respSvc *response.Service
log *logger.Logger log *logger.Logger
} }
// NewExecutionService wraps ServiceExecutor and returns Session Service interface. // NewExecutionService wraps ServiceExecutor and returns Session Service interface.
func NewExecutionService(exec ServiceExecutor, l *logger.Logger) Server { func NewExecutionService(exec ServiceExecutor, respSvc *response.Service, l *logger.Logger) Server {
return &executorSvc{ return &executorSvc{
exec: exec, exec: exec,
log: l, log: l,
respSvc: respSvc,
} }
} }
@ -41,5 +45,6 @@ func (s *executorSvc) Create(ctx context.Context, req *session.CreateRequest) (*
resp := new(session.CreateResponse) resp := new(session.CreateResponse)
resp.SetBody(respBody) resp.SetBody(respBody)
s.respSvc.SetMeta(resp)
return resp, nil return resp, nil
} }

View file

@ -1,37 +0,0 @@
package session
import (
"context"
"github.com/TrueCloudLab/frostfs-api-go/v2/session"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util/response"
)
type responseService struct {
respSvc *response.Service
svc Server
}
// NewResponseService returns session service instance that passes internal service
// call to response service.
func NewResponseService(ssSvc Server, respSvc *response.Service) Server {
return &responseService{
respSvc: respSvc,
svc: ssSvc,
}
}
func (s *responseService) Create(ctx context.Context, req *session.CreateRequest) (*session.CreateResponse, error) {
resp, err := s.respSvc.HandleUnaryRequest(ctx, req,
func(ctx context.Context, req any) (util.ResponseMessage, error) {
return s.svc.Create(ctx, req.(*session.CreateRequest))
},
)
if err != nil {
return nil, err
}
return resp.(*session.CreateResponse), nil
}

View file

@ -9,7 +9,7 @@ import (
// ClientMessageStreamer represents client-side message streamer // ClientMessageStreamer represents client-side message streamer
// that sets meta values to the response. // that sets meta values to the response.
type ClientMessageStreamer struct { type ClientMessageStreamer struct {
cfg *cfg srv *Service
send util.RequestMessageWriter send util.RequestMessageWriter
@ -32,7 +32,7 @@ func (s *ClientMessageStreamer) CloseAndRecv() (util.ResponseMessage, error) {
return nil, fmt.Errorf("(%T) could not close stream and receive response: %w", s, err) return nil, fmt.Errorf("(%T) could not close stream and receive response: %w", s, err)
} }
setMeta(resp, s.cfg) s.srv.SetMeta(resp)
return resp, nil return resp, nil
} }
@ -40,7 +40,7 @@ func (s *ClientMessageStreamer) CloseAndRecv() (util.ResponseMessage, error) {
// CreateRequestStreamer wraps stream methods and returns ClientMessageStreamer instance. // CreateRequestStreamer wraps stream methods and returns ClientMessageStreamer instance.
func (s *Service) CreateRequestStreamer(sender util.RequestMessageWriter, closer util.ClientStreamCloser) *ClientMessageStreamer { func (s *Service) CreateRequestStreamer(sender util.RequestMessageWriter, closer util.ClientStreamCloser) *ClientMessageStreamer {
return &ClientMessageStreamer{ return &ClientMessageStreamer{
cfg: s.cfg, srv: s,
send: sender, send: sender,
close: closer, close: closer,
} }

View file

@ -9,7 +9,7 @@ import (
// ServerMessageStreamer represents server-side message streamer // ServerMessageStreamer represents server-side message streamer
// that sets meta values to all response messages. // that sets meta values to all response messages.
type ServerMessageStreamer struct { type ServerMessageStreamer struct {
cfg *cfg srv *Service
recv util.ResponseMessageReader recv util.ResponseMessageReader
} }
@ -22,7 +22,7 @@ func (s *ServerMessageStreamer) Recv() (util.ResponseMessage, error) {
return nil, fmt.Errorf("could not receive response message for signing: %w", err) return nil, fmt.Errorf("could not receive response message for signing: %w", err)
} }
setMeta(m, s.cfg) s.srv.SetMeta(m)
return m, nil return m, nil
} }
@ -30,7 +30,7 @@ func (s *ServerMessageStreamer) Recv() (util.ResponseMessage, error) {
// HandleServerStreamRequest builds internal streamer via handlers, wraps it to ServerMessageStreamer and returns the result. // HandleServerStreamRequest builds internal streamer via handlers, wraps it to ServerMessageStreamer and returns the result.
func (s *Service) HandleServerStreamRequest(respWriter util.ResponseMessageWriter) util.ResponseMessageWriter { func (s *Service) HandleServerStreamRequest(respWriter util.ResponseMessageWriter) util.ResponseMessageWriter {
return func(resp util.ResponseMessage) error { return func(resp util.ResponseMessage) error {
setMeta(resp, s.cfg) s.SetMeta(resp)
return respWriter(resp) return respWriter(resp)
} }

View file

@ -44,11 +44,12 @@ func NewService(opts ...Option) *Service {
} }
} }
func setMeta(resp util.ResponseMessage, cfg *cfg) { // SetMeta sets adds meta-header to resp.
func (s *Service) SetMeta(resp util.ResponseMessage) {
meta := new(session.ResponseMetaHeader) meta := new(session.ResponseMetaHeader)
meta.SetVersion(&cfg.version) meta.SetVersion(&s.cfg.version)
meta.SetTTL(1) // FIXME: #1160 TTL must be calculated meta.SetTTL(1) // FIXME: #1160 TTL must be calculated
meta.SetEpoch(cfg.state.CurrentEpoch()) meta.SetEpoch(s.cfg.state.CurrentEpoch())
if origin := resp.GetMetaHeader(); origin != nil { if origin := resp.GetMetaHeader(); origin != nil {
// FIXME: #1160 what if origin is set by local server? // FIXME: #1160 what if origin is set by local server?

View file

@ -1,21 +0,0 @@
package response
import (
"context"
"fmt"
"github.com/TrueCloudLab/frostfs-node/pkg/services/util"
)
// HandleUnaryRequest call passes request to handler, sets response meta header values and returns it.
func (s *Service) HandleUnaryRequest(ctx context.Context, req any, handler util.UnaryHandler) (util.ResponseMessage, error) {
// process request
resp, err := handler(ctx, req)
if err != nil {
return nil, fmt.Errorf("could not handle request: %w", err)
}
setMeta(resp, s.cfg)
return resp, nil
}