-
Notifications
You must be signed in to change notification settings - Fork 402
/
client.go
101 lines (84 loc) · 2.54 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package piecestore
import (
"context"
"io"
"github.com/zeebo/errs"
"go.uber.org/zap"
"google.golang.org/grpc"
"storj.io/storj/internal/memory"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
"storj.io/storj/pkg/transport"
)
// Error is the default error class for piecestore client.
var Error = errs.Class("piecestore")
// Config defines piecestore client parameters fro upload and download.
type Config struct {
UploadBufferSize int64
DownloadBufferSize int64
InitialStep int64
MaximumStep int64
}
// DefaultConfig are the default params used for upload and download.
var DefaultConfig = Config{
UploadBufferSize: 256 * memory.KiB.Int64(),
DownloadBufferSize: 256 * memory.KiB.Int64(),
InitialStep: 64 * memory.KiB.Int64(),
MaximumStep: 1 * memory.MiB.Int64(),
}
// Client implements uploading, downloading and deleting content from a piecestore.
type Client struct {
log *zap.Logger
client pb.PiecestoreClient
conn *grpc.ClientConn
config Config
}
// Dial dials the target piecestore endpoint.
func Dial(ctx context.Context, transport transport.Client, target *pb.Node, log *zap.Logger, config Config) (*Client, error) {
conn, err := transport.DialNode(ctx, target)
if err != nil {
return nil, Error.Wrap(err)
}
return &Client{
log: log,
client: pb.NewPiecestoreClient(conn),
conn: conn,
config: config,
}, nil
}
// Delete uses delete order limit to delete a piece on piece store.
func (client *Client) Delete(ctx context.Context, limit *pb.OrderLimit, privateKey storj.PiecePrivateKey) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = client.client.Delete(ctx, &pb.PieceDeleteRequest{
Limit: limit,
})
return Error.Wrap(err)
}
// Retain uses a bloom filter to tell the piece store which pieces to keep.
func (client *Client) Retain(ctx context.Context, req *pb.RetainRequest) (err error) {
defer mon.Task()(&ctx)(&err)
_, err = client.client.Retain(ctx, req)
return Error.Wrap(err)
}
// Close closes the underlying connection.
func (client *Client) Close() error {
return client.conn.Close()
}
// next allocation step find the next trusted step.
func (client *Client) nextAllocationStep(previous int64) int64 {
// TODO: ensure that this is frame idependent
next := previous * 3 / 2
if next > client.config.MaximumStep {
next = client.config.MaximumStep
}
return next
}
// ignoreEOF is an utility func for ignoring EOF error, when it's not important.
func ignoreEOF(err error) error {
if err == io.EOF {
return nil
}
return err
}