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 .WithDialer (
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 )
0 commit comments