lndclient: split NewLndServices to take gRPC dialer

pull/115/head
Johan T. Halseth 4 years ago
parent 80b071a2e6
commit 2334816a59
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"net"
"path/filepath" "path/filepath"
"time" "time"
@ -38,9 +39,24 @@ type GrpcLndServices struct {
cleanup func() cleanup func()
} }
// NewLndServices creates a set of required RPC services. // NewLndServices creates creates a connection to the given lnd instance and
func NewLndServices(lndAddress, application, network, macaroonDir, // creates a set of required RPC services.
tlsPath string) (*GrpcLndServices, error) { 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 // Based on the network, if the macaroon directory isn't set, then
// we'll use the expected default locations. // we'll use the expected default locations.
@ -85,7 +101,7 @@ func NewLndServices(lndAddress, application, network, macaroonDir,
// Setup connection with lnd // Setup connection with lnd
log.Infof("Creating lnd connection to %v", lndAddress) log.Infof("Creating lnd connection to %v", lndAddress)
conn, err := getClientConn(lndAddress, network, tlsPath) conn, err := getClientConn(dialer, lndAddress, tlsPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -189,7 +205,9 @@ var (
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200) 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) { *grpc.ClientConn, error) {
// Load the specified TLS certificate and build transport credentials // 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. // Create a dial options array.
opts := []grpc.DialOption{ opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds), 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...) conn, err := grpc.Dial(address, opts...)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to connect to RPC server: %v", err) return nil, fmt.Errorf("unable to connect to RPC server: %v", err)

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

Loading…
Cancel
Save