Skip to content

Commit

Permalink
rpcperms: add ctx and cancel to InterceptorChain
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Nov 21, 2024
1 parent 83d5d94 commit ffa0e3a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
interceptorChain := rpcperms.NewInterceptorChain(
rpcsLog, cfg.NoMacaroons, cfg.RPCMiddleware.Mandatory,
)
if err := interceptorChain.Start(); err != nil {
if err := interceptorChain.Start(ctx); err != nil {
return mkErr("error starting interceptor chain: %v", err)
}
defer func() {
Expand Down
14 changes: 7 additions & 7 deletions rpcperms/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/btcsuite/btclog/v2"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/lightningnetwork/lnd/monitoring"
Expand Down Expand Up @@ -189,7 +190,7 @@ type InterceptorChain struct {
// middleware crashes.
mandatoryMiddleware []string

quit chan struct{}
cancel fn.Option[context.CancelFunc]
sync.RWMutex
}

Expand All @@ -209,15 +210,17 @@ func NewInterceptorChain(log btclog.Logger, noMacaroons bool,
rpcsLog: log,
registeredMiddlewareNames: make(map[string]int),
mandatoryMiddleware: mandatoryMiddleware,
quit: make(chan struct{}),
}
}

// Start starts the InterceptorChain, which is needed to start the state
// subscription server it powers.
func (r *InterceptorChain) Start() error {
func (r *InterceptorChain) Start(ctx context.Context) error {
var err error
r.started.Do(func() {
_, cancel := context.WithCancel(ctx)
r.cancel = fn.Some(cancel)

err = r.ntfnServer.Start()
})

Expand All @@ -228,7 +231,7 @@ func (r *InterceptorChain) Start() error {
func (r *InterceptorChain) Stop() error {
var err error
r.stopped.Do(func() {
close(r.quit)
r.cancel.WhenSome(func(fn context.CancelFunc) { fn() })
err = r.ntfnServer.Stop()
})

Expand Down Expand Up @@ -368,9 +371,6 @@ func (r *InterceptorChain) SubscribeState(_ *lnrpc.SubscribeStateRequest,
return nil
}
return stream.Context().Err()

case <-r.quit:
return fmt.Errorf("server exiting")
}
}
}
Expand Down

0 comments on commit ffa0e3a

Please sign in to comment.