2020-09-30 08:39:45 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
2020-12-02 23:45:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/util"
|
2020-09-30 08:39:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
TransportSplitter struct {
|
2020-12-02 23:45:25 +00:00
|
|
|
next ServiceServer
|
2020-09-30 08:39:45 +00:00
|
|
|
|
|
|
|
chunkSize uint64
|
|
|
|
addrAmount uint64
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
getStreamMsgSizeCtrl struct {
|
|
|
|
util.ServerStream
|
|
|
|
|
|
|
|
stream GetObjectStream
|
|
|
|
|
2020-09-30 08:39:45 +00:00
|
|
|
chunkSize int
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
searchStreamMsgSizeCtrl struct {
|
|
|
|
util.ServerStream
|
|
|
|
|
|
|
|
stream SearchStream
|
|
|
|
|
2020-09-30 08:39:45 +00:00
|
|
|
addrAmount uint64
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
rangeStreamMsgSizeCtrl struct {
|
|
|
|
util.ServerStream
|
|
|
|
|
|
|
|
stream GetObjectRangeStream
|
|
|
|
|
2020-09-30 08:39:45 +00:00
|
|
|
chunkSize int
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *getStreamMsgSizeCtrl) Send(resp *object.GetResponse) error {
|
|
|
|
body := resp.GetBody()
|
|
|
|
|
|
|
|
part := body.GetObjectPart()
|
|
|
|
|
|
|
|
chunkPart, ok := part.(*object.GetObjectPartChunk)
|
|
|
|
if !ok {
|
|
|
|
return s.stream.Send(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var newResp *object.GetResponse
|
|
|
|
|
|
|
|
for buf := bytes.NewBuffer(chunkPart.GetChunk()); buf.Len() > 0; {
|
|
|
|
if newResp == nil {
|
|
|
|
newResp = new(object.GetResponse)
|
|
|
|
newResp.SetBody(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkPart.SetChunk(buf.Next(s.chunkSize))
|
|
|
|
newResp.SetMetaHeader(resp.GetMetaHeader())
|
|
|
|
newResp.SetVerificationHeader(resp.GetVerificationHeader())
|
|
|
|
|
|
|
|
if err := s.stream.Send(newResp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTransportSplitter(size, amount uint64, next ServiceServer) *TransportSplitter {
|
2020-09-30 08:39:45 +00:00
|
|
|
return &TransportSplitter{
|
|
|
|
next: next,
|
|
|
|
chunkSize: size,
|
|
|
|
addrAmount: amount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (c *TransportSplitter) Get(req *object.GetRequest, stream GetObjectStream) error {
|
|
|
|
return c.next.Get(req, &getStreamMsgSizeCtrl{
|
|
|
|
ServerStream: stream,
|
|
|
|
stream: stream,
|
|
|
|
chunkSize: int(c.chunkSize),
|
|
|
|
})
|
2020-09-30 08:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c TransportSplitter) Put(ctx context.Context) (object.PutObjectStreamer, error) {
|
|
|
|
return c.next.Put(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c TransportSplitter) Head(ctx context.Context, request *object.HeadRequest) (*object.HeadResponse, error) {
|
|
|
|
return c.next.Head(ctx, request)
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
func (c TransportSplitter) Search(req *object.SearchRequest, stream SearchStream) error {
|
|
|
|
return c.next.Search(req, &searchStreamMsgSizeCtrl{
|
|
|
|
ServerStream: stream,
|
|
|
|
stream: stream,
|
|
|
|
addrAmount: c.addrAmount,
|
|
|
|
})
|
2020-09-30 08:39:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c TransportSplitter) Delete(ctx context.Context, request *object.DeleteRequest) (*object.DeleteResponse, error) {
|
|
|
|
return c.next.Delete(ctx, request)
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (s *rangeStreamMsgSizeCtrl) Send(resp *object.GetRangeResponse) error {
|
|
|
|
body := resp.GetBody()
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
chunkPart, ok := body.GetRangePart().(*object.GetRangePartChunk)
|
|
|
|
if !ok {
|
|
|
|
return s.stream.Send(resp)
|
|
|
|
}
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
var newResp *object.GetRangeResponse
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
for buf := bytes.NewBuffer(chunkPart.GetChunk()); buf.Len() > 0; {
|
|
|
|
if newResp == nil {
|
|
|
|
newResp = new(object.GetRangeResponse)
|
|
|
|
newResp.SetBody(body)
|
2020-09-30 08:39:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
chunkPart.SetChunk(buf.Next(s.chunkSize))
|
|
|
|
body.SetRangePart(chunkPart)
|
|
|
|
newResp.SetMetaHeader(resp.GetMetaHeader())
|
|
|
|
newResp.SetVerificationHeader(resp.GetVerificationHeader())
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
if err := s.stream.Send(newResp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (c TransportSplitter) GetRange(req *object.GetRangeRequest, stream GetObjectRangeStream) error {
|
|
|
|
return c.next.GetRange(req, &rangeStreamMsgSizeCtrl{
|
|
|
|
ServerStream: stream,
|
|
|
|
stream: stream,
|
|
|
|
chunkSize: int(c.chunkSize),
|
|
|
|
})
|
|
|
|
}
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (c TransportSplitter) GetRangeHash(ctx context.Context, request *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
|
|
|
|
return c.next.GetRangeHash(ctx, request)
|
2020-09-30 08:39:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
func (s *searchStreamMsgSizeCtrl) Send(resp *object.SearchResponse) error {
|
|
|
|
body := resp.GetBody()
|
|
|
|
ids := body.GetIDList()
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
var newResp *object.SearchResponse
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
for ln := uint64(len(ids)); len(ids) > 0; {
|
|
|
|
if newResp == nil {
|
|
|
|
newResp = new(object.SearchResponse)
|
|
|
|
newResp.SetBody(body)
|
|
|
|
}
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
cut := s.addrAmount
|
|
|
|
if cut > ln {
|
|
|
|
cut = ln
|
|
|
|
}
|
|
|
|
|
|
|
|
body.SetIDList(ids[:cut])
|
|
|
|
newResp.SetMetaHeader(resp.GetMetaHeader())
|
|
|
|
newResp.SetVerificationHeader(resp.GetVerificationHeader())
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
if err := s.stream.Send(newResp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-30 08:39:45 +00:00
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
ids = ids[cut:]
|
2020-09-30 08:39:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
return nil
|
2020-09-30 08:39:45 +00:00
|
|
|
}
|