-
Notifications
You must be signed in to change notification settings - Fork 1
/
lnconnect.go
52 lines (41 loc) · 1.22 KB
/
lnconnect.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 lnconnect
import (
"context"
"fmt"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// ErrCredentials represents a credentials error.
var ErrCredentials = fmt.Errorf("credentials error")
// Credentials is used to pass identification and connection
// parameters to InitializeConnection.
type Credentials struct {
RPCAddress string
TLSCreds credentials.TransportCredentials
RPCCreds credentials.PerRPCCredentials
}
var (
dialTimeoutSeconds = 10
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 50)
)
// InitializeConnection establishes a connection with a Lightning daemon.
func InitializeConnection(cfg Credentials) (*grpc.ClientConn, error) {
opts := []grpc.DialOption{
grpc.WithTransportCredentials(cfg.TLSCreds),
grpc.WithBlock(),
grpc.WithDefaultCallOptions(maxMsgRecvSize),
}
if cfg.RPCCreds != nil {
opts = append(opts, grpc.WithPerRPCCredentials(cfg.RPCCreds))
}
// Set a timeout for the blocking connection call
dialCtx, cancel := context.WithTimeout(context.Background(),
time.Duration(dialTimeoutSeconds)*time.Second)
defer cancel()
conn, err := grpc.DialContext(dialCtx, cfg.RPCAddress, opts...)
if err != nil {
return nil, err
}
return conn, nil
}