From 0f0573c0475540fadf4d2b703516e7a0457c8e99 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 3 Jan 2020 14:01:31 +0100 Subject: [PATCH] loopd/daemon: make server gRPC/REST listeners configurable --- loopd/daemon.go | 15 ++++++++++++--- loopd/start.go | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/loopd/daemon.go b/loopd/daemon.go index d6247b5..f567c57 100644 --- a/loopd/daemon.go +++ b/loopd/daemon.go @@ -18,9 +18,18 @@ import ( "google.golang.org/grpc" ) +// listenerCfg holds closures used to retrieve listeners for the gRPC services. +type listenerCfg struct { + // grpcListener returns a listener to use for the gRPC server. + grpcListener func() (net.Listener, error) + + // restListener returns a listener to use for the REST proxy. + restListener func() (net.Listener, error) +} + // daemon runs loopd in daemon mode. It will listen for grpc connections, // execute commands and pass back swap status information. -func daemon(config *config) error { +func daemon(config *config, lisCfg *listenerCfg) error { lnd, err := getLnd(config.Network, config.Lnd) if err != nil { return err @@ -74,7 +83,7 @@ func daemon(config *config) error { // Next, start the gRPC server listening for HTTP/2 connections. log.Infof("Starting gRPC listener") - grpcListener, err := net.Listen("tcp", config.RPCListen) + grpcListener, err := lisCfg.grpcListener() if err != nil { return fmt.Errorf("RPC server unable to listen on %s", config.RPCListen) @@ -96,7 +105,7 @@ func daemon(config *config) error { } log.Infof("Starting REST proxy listener") - restListener, err := net.Listen("tcp", config.RESTListen) + restListener, err := lisCfg.restListener() if err != nil { return fmt.Errorf("REST proxy unable to listen on %s", config.RESTListen) diff --git a/loopd/start.go b/loopd/start.go index 2178b68..7f3bbb6 100644 --- a/loopd/start.go +++ b/loopd/start.go @@ -2,6 +2,7 @@ package loopd import ( "fmt" + "net" "os" "path/filepath" "strings" @@ -26,6 +27,18 @@ var ( swapsLock sync.Mutex ) +// newListenerCfg creates and returns a new listenerCfg from the passed config. +func newListenerCfg(config *config) *listenerCfg { + return &listenerCfg{ + grpcListener: func() (net.Listener, error) { + return net.Listen("tcp", config.RPCListen) + }, + restListener: func() (net.Listener, error) { + return net.Listen("tcp", config.RESTListen) + }, + } +} + func Start() error { config := defaultConfig @@ -99,9 +112,11 @@ func Start() error { // Print the version before executing either primary directive. log.Infof("Version: %v", loop.Version()) + lisCfg := newListenerCfg(&config) + // Execute command. if parser.Active == nil { - return daemon(&config) + return daemon(&config, lisCfg) } if parser.Active.Name == "view" {