2020-09-25 13:04:55 +00:00
|
|
|
package rangesvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2020-09-29 16:44:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2020-09-25 13:04:55 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2020-11-18 13:04:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
2020-09-29 16:44:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-09-25 13:04:55 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type remoteRangeWriter struct {
|
|
|
|
ctx context.Context
|
|
|
|
|
2020-09-29 16:44:59 +00:00
|
|
|
keyStorage *util.KeyStorage
|
2020-09-25 13:04:55 +00:00
|
|
|
|
|
|
|
node *network.Address
|
|
|
|
|
2020-09-29 16:44:59 +00:00
|
|
|
token *token.SessionToken
|
|
|
|
|
2020-10-20 13:44:45 +00:00
|
|
|
bearer *token.BearerToken
|
|
|
|
|
2020-09-25 13:04:55 +00:00
|
|
|
addr *object.Address
|
|
|
|
|
|
|
|
rng *object.Range
|
2020-11-18 13:04:33 +00:00
|
|
|
|
|
|
|
clientCache *cache.ClientCache
|
2020-09-25 13:04:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *remoteRangeWriter) WriteTo(w io.Writer) (int64, error) {
|
2020-09-29 16:44:59 +00:00
|
|
|
key, err := r.keyStorage.GetKey(r.token)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrapf(err, "(%T) could not receive private key", r)
|
|
|
|
}
|
|
|
|
|
2020-09-24 08:27:14 +00:00
|
|
|
addr, err := r.node.IPAddrString()
|
2020-09-24 07:37:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-09-25 13:04:55 +00:00
|
|
|
|
2020-11-18 13:04:33 +00:00
|
|
|
c, err := r.clientCache.Get(key, addr)
|
2020-09-25 13:04:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrapf(err, "(%T) could not create SDK client %s", r, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: change ObjectPayloadRangeData to implement WriterTo
|
|
|
|
chunk, err := c.ObjectPayloadRangeData(r.ctx, new(client.RangeDataParams).
|
|
|
|
WithRange(r.rng).
|
|
|
|
WithAddress(r.addr),
|
|
|
|
client.WithTTL(1), // FIXME: use constant
|
2020-09-29 16:44:59 +00:00
|
|
|
client.WithSession(r.token),
|
2020-10-20 13:44:45 +00:00
|
|
|
client.WithBearer(r.bearer),
|
2020-09-25 13:04:55 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrapf(err, "(%T) could not read object payload range from %s", r, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := w.Write(chunk)
|
|
|
|
|
|
|
|
return int64(n), err
|
|
|
|
}
|