Browse Source

loopd: make RPC listener configurable

Allows to use external listener, such as in-memory.
pull/115/head
Johan T. Halseth 1 year ago
parent
commit
c7e8c9f964
No known key found for this signature in database GPG Key ID: 15BAADA29DA20D26
3 changed files with 50 additions and 17 deletions
  1. +2
    -1
      cmd/loopd/main.go
  2. +22
    -12
      loopd/daemon.go
  3. +26
    -4
      loopd/start.go

+ 2
- 1
cmd/loopd/main.go View File

@ -7,7 +7,8 @@ import (
)
func main() {
err := loopd.Start()
cfg := loopd.RPCConfig{}
err := loopd.Start(cfg)
if err != nil {
fmt.Println(err)
}

+ 22
- 12
loopd/daemon.go View File

@ -104,23 +104,30 @@ func daemon(config *config, lisCfg *listenerCfg) error {
return err
}
log.Infof("Starting REST proxy listener")
restListener, err := lisCfg.restListener()
if err != nil {
return fmt.Errorf("REST proxy unable to listen on %s",
config.RESTListen)
}
defer restListener.Close()
proxy := &http.Server{Handler: mux}
go func() {
err := proxy.Serve(restListener)
// ErrServerClosed is always returned when the proxy is shut
// down, so don't log it.
if err != nil && err != http.ErrServerClosed {
log.Error(err)
}
}()
// A nil listener indicates REST is disabled.
if restListener != nil {
log.Infof("Starting REST proxy listener")
defer restListener.Close()
proxy := &http.Server{Handler: mux}
go func() {
err := proxy.Serve(restListener)
// ErrServerClosed is always returned when the proxy is
// shut down, so don't log it.
if err != nil && err != http.ErrServerClosed {
log.Error(err)
}
}()
} else {
log.Infof("REST proxy disabled")
}
statusChan := make(chan loop.SwapInfo)
@ -178,7 +185,10 @@ func daemon(config *config, lisCfg *listenerCfg) error {
defer wg.Done()
log.Infof("RPC server listening on %s", grpcListener.Addr())
log.Infof("REST proxy listening on %s", restListener.Addr())
if restListener != nil {
log.Infof("REST proxy listening on %s", restListener.Addr())
}
err = grpcServer.Serve(grpcListener)
if err != nil {

+ 26
- 4
loopd/start.go View File

@ -27,19 +27,41 @@ var (
swapsLock sync.Mutex
)
// newListenerCfg creates and returns a new listenerCfg from the passed config.
func newListenerCfg(config *config) *listenerCfg {
// RPCConfig holds optional options that can be used to make the loop daemon
// communicate on custom connections.
type RPCConfig struct {
// RPCListener is an optional listener that if set will override the
// daemon's gRPC settings, and make the gRPC server listen on this
// listener.
// Note that setting this will also disable REST.
RPCListener net.Listener
}
// newListenerCfg creates and returns a new listenerCfg from the passed config
// and RPCConfig.
func newListenerCfg(config *config, rpcCfg RPCConfig) *listenerCfg {
return &listenerCfg{
grpcListener: func() (net.Listener, error) {
// If a custom RPC listener is set, we will listen on
// it instead of the regular tcp socket.
if rpcCfg.RPCListener != nil {
return rpcCfg.RPCListener, nil
}
return net.Listen("tcp", config.RPCListen)
},
restListener: func() (net.Listener, error) {
// If a custom RPC listener is set, we disable REST.
if rpcCfg.RPCListener != nil {
return nil, nil
}
return net.Listen("tcp", config.RESTListen)
},
}
}
func Start() error {
func Start(rpcCfg RPCConfig) error {
config := defaultConfig
// Parse command line flags.
@ -112,7 +134,7 @@ func Start() error {
// Print the version before executing either primary directive.
log.Infof("Version: %v", loop.Version())
lisCfg := newListenerCfg(&config)
lisCfg := newListenerCfg(&config, rpcCfg)
// Execute command.
if parser.Active == nil {

Loading…
Cancel
Save