-
Notifications
You must be signed in to change notification settings - Fork 26
/
service_client.go
165 lines (132 loc) · 3.77 KB
/
service_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package weshnet
import (
"context"
"fmt"
"io"
"time"
"github.com/dgraph-io/badger/v2/options"
"github.com/ipfs/go-datastore"
badger "github.com/ipfs/go-ds-badger2"
"google.golang.org/grpc"
"berty.tech/weshnet/pkg/grpcutil"
"berty.tech/weshnet/pkg/ipfsutil"
ipfs_mobile "berty.tech/weshnet/pkg/ipfsutil/mobile"
"berty.tech/weshnet/pkg/protocoltypes"
)
type ServiceClient interface {
protocoltypes.ProtocolServiceClient
io.Closer
}
func NewServiceClient(opts Opts) (ServiceClient, error) {
svc, err := NewService(opts)
if err != nil {
return nil, err
}
s := grpc.NewServer()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
c, err := NewClientFromService(ctx, s, svc)
if err != nil {
return nil, fmt.Errorf("uanble to create client from server: %w", err)
}
return &serviceClient{
ServiceClient: c,
server: s,
service: svc,
}, nil
}
// NewInMemoryServiceClient creates a new in-memory Wesh protocol service and returns a gRPC
// ServiceClient which uses a direct in-memory connection. When finished, you must call Close().
// This creates a new Wesh account where the key store is in memory. (If you don't
// export the data then it is lost when you call Close(). ) The IPFS node, cached data,
// and configuration are also in memory.
func NewInMemoryServiceClient() (ServiceClient, error) {
var opts Opts
opts.DatastoreDir = InMemoryDirectory
return NewServiceClient(opts)
}
func NewPersistentServiceClient(path string) (ServiceClient, error) {
var opts Opts
bopts := badger.DefaultOptions
bopts.ValueLogLoadingMode = options.FileIO
ds, err := badger.NewDatastore(path, &bopts)
if err != nil {
return nil, fmt.Errorf("unable to init badger datastore: %w", err)
}
repo, err := ipfsutil.LoadRepoFromPath(path)
if err != nil {
return nil, err
}
mrepo := ipfs_mobile.NewRepoMobile(path, repo)
mnode, err := ipfsutil.NewIPFSMobile(context.TODO(), mrepo, &ipfsutil.MobileOptions{
ExtraOpts: map[string]bool{
"pubsub": true,
},
})
if err != nil {
return nil, err
}
opts.IpfsCoreAPI, err = ipfsutil.NewExtendedCoreAPIFromNode(mnode.IpfsNode)
if err != nil {
return nil, err
}
opts.RootDatastore = ds
cl, err := NewServiceClient(opts)
if err != nil {
return nil, err
}
return &persistantServiceClient{
ServiceClient: cl,
ds: ds,
}, nil
}
const ClientBufferSize = 4 * 1024 * 1024
type serviceClient struct {
ServiceClient // inehrit from client
service Service
server *grpc.Server
}
type persistantServiceClient struct {
ServiceClient
ds datastore.Batching
}
func (p *persistantServiceClient) Close() error {
err := p.ServiceClient.Close()
if dserr := p.ds.Close(); err == nil && dserr != nil {
// only return ds error if no error have been catch earlier
err = fmt.Errorf("unable to close datastore: %w", dserr)
}
return err
}
func (c *serviceClient) Close() error {
c.server.GracefulStop() // gracefully stop grpc server
_ = c.ServiceClient.Close() // close client and discard error
return c.service.Close() // return real service error
}
type client struct {
protocoltypes.ProtocolServiceClient
l *grpcutil.BufListener
cc *grpc.ClientConn
}
func (c *client) Close() error {
err := c.cc.Close()
_ = c.l.Close()
return err
}
func NewClientFromService(ctx context.Context, s *grpc.Server, svc Service, opts ...grpc.DialOption) (ServiceClient, error) {
bl := grpcutil.NewBufListener(ClientBufferSize)
cc, err := bl.NewClientConn(ctx, opts...)
if err != nil {
return nil, err
}
protocoltypes.RegisterProtocolServiceServer(s, svc)
go func() {
// we dont need to log the error
_ = s.Serve(bl)
}()
return &client{
ProtocolServiceClient: protocoltypes.NewProtocolServiceClient(cc),
cc: cc,
l: bl,
}, nil
}