-
Notifications
You must be signed in to change notification settings - Fork 75
/
client.go
47 lines (39 loc) · 1.31 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
// This package contains a tiny wrapper over the generated gRPC rendezvous
// client that allows to close the client explicitly.
package rendezvous
import (
"context"
"github.com/sonm-io/core/proto"
"github.com/sonm-io/core/util/xgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Client extends the generated gRPC client allowing to close the underlying
// connection.
// Users should call Client.Close to terminate all the pending operations.
type Client interface {
sonm.RendezvousClient
// Close closes this client freeing the associated resources.
//
// All pending operations will be terminated with error.
Close() error
}
type client struct {
sonm.RendezvousClient
conn *grpc.ClientConn
}
// NewRendezvousClient constructs a new rendezvous client.
//
// The address provided will be used for establishing a TCP connection while
// optional credentials - for authentication. Additionally other dial options
// can be specified.
func NewRendezvousClient(ctx context.Context, addr string, credentials credentials.TransportCredentials, opts ...grpc.DialOption) (Client, error) {
conn, err := xgrpc.NewClient(ctx, addr, credentials, opts...)
if err != nil {
return nil, err
}
return &client{sonm.NewRendezvousClient(conn), conn}, nil
}
func (m *client) Close() error {
return m.conn.Close()
}