-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_conn.go
52 lines (40 loc) · 1.21 KB
/
client_conn.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
package client
import (
"context"
"net"
"strconv"
"github.com/americanas-go/log"
"storj.io/drpc/drpcconn"
)
// NewClientConn returns a new grpc client connection.
func NewClientConn(ctx context.Context) (*drpcconn.Conn, error) {
opt, err := NewOptions()
if err != nil {
return nil, err
}
return NewClientConnWithOptions(ctx, opt)
}
// NewClientConnWithConfigPath returns a new grpc client connection with options from config path.
func NewClientConnWithConfigPath(ctx context.Context, path string) (*drpcconn.Conn, error) {
options, err := NewOptionsWithPath(path)
if err != nil {
return nil, err
}
return NewClientConnWithOptions(ctx, options)
}
// NewClientConnWithOptions returns a new grpc client connection with options.
func NewClientConnWithOptions(ctx context.Context, options *Options) (*drpcconn.Conn, error) {
var err error
var rawconn net.Conn
logger := log.FromContext(ctx)
serverAddr := options.Host + ":" + strconv.Itoa(options.Port)
rawconn, err = net.Dial("tcp", serverAddr)
if err != nil {
logger.Errorf("fail to dial: %v", err)
return nil, err
}
conn := drpcconn.New(rawconn)
// defer conn.Close()
logger.Debugf("drpc client created for host %s", serverAddr)
return conn, nil
}