4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ "net"
7
8
"path/filepath"
8
9
"time"
9
10
@@ -38,9 +39,24 @@ type GrpcLndServices struct {
38
39
cleanup func ()
39
40
}
40
41
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 ) {
44
60
45
61
// Based on the network, if the macaroon directory isn't set, then
46
62
// we'll use the expected default locations.
@@ -85,7 +101,7 @@ func NewLndServices(lndAddress, application, network, macaroonDir,
85
101
86
102
// Setup connection with lnd
87
103
log .Infof ("Creating lnd connection to %v" , lndAddress )
88
- conn , err := getClientConn (lndAddress , network , tlsPath )
104
+ conn , err := getClientConn (dialer , lndAddress , tlsPath )
89
105
if err != nil {
90
106
return nil , err
91
107
}
@@ -189,7 +205,9 @@ var (
189
205
maxMsgRecvSize = grpc .MaxCallRecvMsgSize (1 * 1024 * 1024 * 200 )
190
206
)
191
207
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 ) (
193
211
* grpc.ClientConn , error ) {
194
212
195
213
// Load the specified TLS certificate and build transport credentials
@@ -206,15 +224,12 @@ func getClientConn(address string, network string, tlsPath string) (
206
224
// Create a dial options array.
207
225
opts := []grpc.DialOption {
208
226
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 ),
209
231
}
210
232
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
- )
218
233
conn , err := grpc .Dial (address , opts ... )
219
234
if err != nil {
220
235
return nil , fmt .Errorf ("unable to connect to RPC server: %v" , err )
0 commit comments