Skip to content

Commit 2334816

Browse files
committed
lndclient: split NewLndServices to take gRPC dialer
1 parent 80b071a commit 2334816

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

lndclient/lnd_services.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"net"
78
"path/filepath"
89
"time"
910

@@ -38,9 +39,24 @@ type GrpcLndServices struct {
3839
cleanup func()
3940
}
4041

41-
// NewLndServices creates a set of required RPC services.
42-
func NewLndServices(lndAddress, application, network, macaroonDir,
43-
tlsPath string) (*GrpcLndServices, error) {
42+
// NewLndServices creates creates a connection to the given lnd instance and
43+
// creates a set of required RPC services.
44+
func NewLndServices(lndAddress, network, macaroonDir, tlsPath string) (
45+
*GrpcLndServices, error) {
46+
47+
// We need to use a custom dialer so we can also connect to unix
48+
// sockets and not just TCP addresses.
49+
dialer := lncfg.ClientAddressDialer(defaultRPCPort)
50+
51+
return NewLndServicesWithDialer(
52+
dialer, lndAddress, network, macaroonDir, tlsPath,
53+
)
54+
}
55+
56+
// NewLndServices creates a set of required RPC services by connecting to lnd
57+
// using the given dialer.
58+
func NewLndServicesWithDialer(dialer dialerFunc, lndAddress, network,
59+
macaroonDir, tlsPath string) (*GrpcLndServices, error) {
4460

4561
// Based on the network, if the macaroon directory isn't set, then
4662
// we'll use the expected default locations.
@@ -85,7 +101,7 @@ func NewLndServices(lndAddress, application, network, macaroonDir,
85101

86102
// Setup connection with lnd
87103
log.Infof("Creating lnd connection to %v", lndAddress)
88-
conn, err := getClientConn(lndAddress, network, tlsPath)
104+
conn, err := getClientConn(dialer, lndAddress, tlsPath)
89105
if err != nil {
90106
return nil, err
91107
}
@@ -189,7 +205,9 @@ var (
189205
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200)
190206
)
191207

192-
func getClientConn(address string, network string, tlsPath string) (
208+
type dialerFunc func(context.Context, string) (net.Conn, error)
209+
210+
func getClientConn(dialer dialerFunc, address string, tlsPath string) (
193211
*grpc.ClientConn, error) {
194212

195213
// Load the specified TLS certificate and build transport credentials
@@ -206,15 +224,12 @@ func getClientConn(address string, network string, tlsPath string) (
206224
// Create a dial options array.
207225
opts := []grpc.DialOption{
208226
grpc.WithTransportCredentials(creds),
227+
228+
// Use a custom dialer, to allow connections to unix sockets,
229+
// in-memory listeners etc, and not just TCP addresses.
230+
grpc.WithContextDialer(dialer),
209231
}
210232

211-
// We need to use a custom dialer so we can also connect to unix sockets
212-
// and not just TCP addresses.
213-
opts = append(
214-
opts, grpc.WithContextDialer(
215-
lncfg.ClientAddressDialer(defaultRPCPort),
216-
),
217-
)
218233
conn, err := grpc.Dial(address, opts...)
219234
if err != nil {
220235
return nil, fmt.Errorf("unable to connect to RPC server: %v", err)

loopd/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// getLnd returns an instance of the lnd services proxy.
1212
func getLnd(network string, cfg *lndConfig) (*lndclient.GrpcLndServices, error) {
1313
return lndclient.NewLndServices(
14-
cfg.Host, "client", network, cfg.MacaroonDir, cfg.TLSPath,
14+
cfg.Host, network, cfg.MacaroonDir, cfg.TLSPath,
1515
)
1616
}
1717

0 commit comments

Comments
 (0)