Browse Source

lndclient: split NewLndServices to take gRPC dialer

pull/115/head
Johan T. Halseth 1 year ago
parent
commit
2334816a59
No known key found for this signature in database GPG Key ID: 15BAADA29DA20D26
2 changed files with 28 additions and 13 deletions
  1. +27
    -12
      lndclient/lnd_services.go
  2. +1
    -1
      loopd/utils.go

+ 27
- 12
lndclient/lnd_services.go View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"path/filepath"
"time"
@ -38,9 +39,24 @@ type GrpcLndServices struct {
cleanup func()
}
// NewLndServices creates a set of required RPC services.
func NewLndServices(lndAddress, application, network, macaroonDir,
tlsPath string) (*GrpcLndServices, error) {
// NewLndServices creates creates a connection to the given lnd instance and
// creates a set of required RPC services.
func NewLndServices(lndAddress, network, macaroonDir, tlsPath string) (
*GrpcLndServices, error) {
// We need to use a custom dialer so we can also connect to unix
// sockets and not just TCP addresses.
dialer := lncfg.ClientAddressDialer(defaultRPCPort)
return NewLndServicesWithDialer(
dialer, lndAddress, network, macaroonDir, tlsPath,
)
}
// NewLndServices creates a set of required RPC services by connecting to lnd
// using the given dialer.
func NewLndServicesWithDialer(dialer dialerFunc, lndAddress, network,
macaroonDir, tlsPath string) (*GrpcLndServices, error) {
// Based on the network, if the macaroon directory isn't set, then
// we'll use the expected default locations.
@ -85,7 +101,7 @@ func NewLndServices(lndAddress, application, network, macaroonDir,
// Setup connection with lnd
log.Infof("Creating lnd connection to %v", lndAddress)
conn, err := getClientConn(lndAddress, network, tlsPath)
conn, err := getClientConn(dialer, lndAddress, tlsPath)
if err != nil {
return nil, err
}
@ -189,7 +205,9 @@ var (
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200)
)
func getClientConn(address string, network string, tlsPath string) (
type dialerFunc func(context.Context, string) (net.Conn, error)
func getClientConn(dialer dialerFunc, address string, tlsPath string) (
*grpc.ClientConn, error) {
// Load the specified TLS certificate and build transport credentials
@ -206,15 +224,12 @@ func getClientConn(address string, network string, tlsPath string) (
// Create a dial options array.
opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds),
// Use a custom dialer, to allow connections to unix sockets,
// in-memory listeners etc, and not just TCP addresses.
grpc.WithContextDialer(dialer),
}
// We need to use a custom dialer so we can also connect to unix sockets
// and not just TCP addresses.
opts = append(
opts, grpc.WithContextDialer(
lncfg.ClientAddressDialer(defaultRPCPort),
),
)
conn, err := grpc.Dial(address, opts...)
if err != nil {
return nil, fmt.Errorf("unable to connect to RPC server: %v", err)

+ 1
- 1
loopd/utils.go View File

@ -11,7 +11,7 @@ import (
// getLnd returns an instance of the lnd services proxy.
func getLnd(network string, cfg *lndConfig) (*lndclient.GrpcLndServices, error) {
return lndclient.NewLndServices(
cfg.Host, "client", network, cfg.MacaroonDir, cfg.TLSPath,
cfg.Host, network, cfg.MacaroonDir, cfg.TLSPath,
)
}

Loading…
Cancel
Save