-
Notifications
You must be signed in to change notification settings - Fork 4
/
meta_store_remote.go
77 lines (59 loc) · 1.58 KB
/
meta_store_remote.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
package fsdup
import (
"context"
"google.golang.org/grpc"
"heckel.io/fsdup/pb"
"sync"
)
type remoteMetaStore struct {
serverAddr string
client pb.HubClient
sync.Mutex
}
func NewRemoteMetaStore(serverAddr string) *remoteMetaStore {
debugf("NewRemoteMetaStore(%s)", serverAddr)
return &remoteMetaStore{
serverAddr: serverAddr,
client: nil,
}
}
func (s *remoteMetaStore) ReadManifest(manifestId string) (*manifest, error) {
if err := s.ensureConnected(); err != nil {
return nil, err
}
debugf("remoteMetaStore.ReadManifest(%s)", manifestId)
response, err := s.client.ReadManifest(context.Background(), &pb.ReadManifestRequest{Id: manifestId})
if err != nil {
return nil, err
}
manifest, err := NewManifestFromProto(response.Manifest)
if err != nil {
return nil, err
}
return manifest, nil
}
func (s *remoteMetaStore) WriteManifest(manifestId string, manifest *manifest) error {
if err := s.ensureConnected(); err != nil {
return err
}
debugf("remoteMetaStore.WriteManifest(%s)", manifestId)
_, err := s.client.WriteManifest(context.Background(), &pb.WriteManifestRequest{Id: manifestId, Manifest: manifest.Proto()})
if err != nil {
return err
}
return nil
}
func (s *remoteMetaStore) ensureConnected() error {
s.Lock()
defer s.Unlock()
if s.client != nil {
return nil
}
conn, err := grpc.Dial(s.serverAddr, grpc.WithBlock(), grpc.WithInsecure(),
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(128 * 1024 * 1024), grpc.MaxCallRecvMsgSize(128 * 1024 * 1024))) // FIXME
if err != nil {
return err
}
s.client = pb.NewHubClient(conn)
return nil
}