mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-26 06:27:31 +02:00
lnrpc: wrap subservers in GrpcHandler
In order to be able to register the subservers with the root grpc server before we have all dependencies available, we wrap them in an GrpcHandler struct. This struct will initially hold an empty reference to the subservers, which allows us to register with the GRPC server, and later populate and create the subserver instance.
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
||||
// config that is meant for us in the config dispatcher, then we'll exit with
|
||||
// an error.
|
||||
func createNewSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (
|
||||
lnrpc.SubServer, lnrpc.MacaroonPerms, error) {
|
||||
*Server, lnrpc.MacaroonPerms, error) {
|
||||
|
||||
// We'll attempt to look up the config that we expect, according to our
|
||||
// subServerName name. If we can't find this, then we'll exit with an
|
||||
@@ -46,8 +46,8 @@ func createNewSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (
|
||||
func init() {
|
||||
subServer := &lnrpc.SubServerDriver{
|
||||
SubServerName: subServerName,
|
||||
New: func(c lnrpc.SubServerConfigDispatcher) (lnrpc.SubServer, lnrpc.MacaroonPerms, error) {
|
||||
return createNewSubServer(c)
|
||||
NewGrpcHandler: func() lnrpc.GrpcHandler {
|
||||
return &ServerShell{}
|
||||
},
|
||||
}
|
||||
|
||||
|
@@ -129,6 +129,13 @@ var (
|
||||
DefaultRouterMacFilename = "router.macaroon"
|
||||
)
|
||||
|
||||
// ServerShell a is shell struct holding a reference to the actual sub-server.
|
||||
// It is used to register the gRPC sub-server with the root server before we
|
||||
// have the necessary dependencies to populate the actual sub-server.
|
||||
type ServerShell struct {
|
||||
RouterServer
|
||||
}
|
||||
|
||||
// Server is a stand alone sub RPC server which exposes functionality that
|
||||
// allows clients to route arbitrary payment through the Lightning Network.
|
||||
type Server struct {
|
||||
@@ -233,11 +240,11 @@ func (s *Server) Name() string {
|
||||
// sub RPC server to register itself with the main gRPC root server. Until this
|
||||
// is called, each sub-server won't be able to have requests routed towards it.
|
||||
//
|
||||
// NOTE: This is part of the lnrpc.SubServer interface.
|
||||
func (s *Server) RegisterWithRootServer(grpcServer *grpc.Server) error {
|
||||
// NOTE: This is part of the lnrpc.GrpcHandler interface.
|
||||
func (r *ServerShell) RegisterWithRootServer(grpcServer *grpc.Server) error {
|
||||
// We make sure that we register it with the main gRPC server to ensure
|
||||
// all our methods are routed properly.
|
||||
RegisterRouterServer(grpcServer, s)
|
||||
RegisterRouterServer(grpcServer, r)
|
||||
|
||||
log.Debugf("Router RPC server successfully register with root gRPC " +
|
||||
"server")
|
||||
@@ -249,8 +256,8 @@ func (s *Server) RegisterWithRootServer(grpcServer *grpc.Server) error {
|
||||
// RPC server to register itself with the main REST mux server. Until this is
|
||||
// called, each sub-server won't be able to have requests routed towards it.
|
||||
//
|
||||
// NOTE: This is part of the lnrpc.SubServer interface.
|
||||
func (s *Server) RegisterWithRestServer(ctx context.Context,
|
||||
// NOTE: This is part of the lnrpc.GrpcHandler interface.
|
||||
func (r *ServerShell) RegisterWithRestServer(ctx context.Context,
|
||||
mux *runtime.ServeMux, dest string, opts []grpc.DialOption) error {
|
||||
|
||||
// We make sure that we register it with the main REST server to ensure
|
||||
@@ -267,6 +274,25 @@ func (s *Server) RegisterWithRestServer(ctx context.Context,
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateSubServer populates the subserver's dependencies using the passed
|
||||
// SubServerConfigDispatcher. This method should fully initialize the
|
||||
// sub-server instance, making it ready for action. It returns the macaroon
|
||||
// permissions that the sub-server wishes to pass on to the root server for all
|
||||
// methods routed towards it.
|
||||
//
|
||||
// NOTE: This is part of the lnrpc.GrpcHandler interface.
|
||||
func (r *ServerShell) CreateSubServer(configRegistry lnrpc.SubServerConfigDispatcher) (
|
||||
lnrpc.SubServer, lnrpc.MacaroonPerms, error) {
|
||||
|
||||
subServer, macPermissions, err := createNewSubServer(configRegistry)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
r.RouterServer = subServer
|
||||
return subServer, macPermissions, nil
|
||||
}
|
||||
|
||||
// SendPaymentV2 attempts to route a payment described by the passed
|
||||
// PaymentRequest to the final destination. If we are unable to route the
|
||||
// payment, or cannot find a route that satisfies the constraints in the
|
||||
|
Reference in New Issue
Block a user