mirror of
https://github.com/aljazceru/lspd.git
synced 2025-12-19 14:54:22 +01:00
more efficient cleanup, lessons learned
This commit is contained in:
@@ -36,19 +36,25 @@ func (cfe *copyFromEvents) Err() error {
|
|||||||
return cfe.err
|
return cfe.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func channelsSynchronize(client *LndClient) {
|
func channelsSynchronize(ctx context.Context, client *LndClient) {
|
||||||
lastSync := time.Now().Add(-6 * time.Minute)
|
lastSync := time.Now().Add(-6 * time.Minute)
|
||||||
for {
|
for {
|
||||||
cancellableCtx, cancel := context.WithCancel(context.Background())
|
if ctx.Err() != nil {
|
||||||
stream, err := client.chainNotifierClient.RegisterBlockEpochNtfn(cancellableCtx, &chainrpc.BlockEpoch{})
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stream, err := client.chainNotifierClient.RegisterBlockEpochNtfn(ctx, &chainrpc.BlockEpoch{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("chainNotifierClient.RegisterBlockEpochNtfn(): %v", err)
|
log.Printf("chainNotifierClient.RegisterBlockEpochNtfn(): %v", err)
|
||||||
cancel()
|
|
||||||
<-time.After(time.Second)
|
<-time.After(time.Second)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
_, err := stream.Recv()
|
_, err := stream.Recv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("stream.Recv: %v", err)
|
log.Printf("stream.Recv: %v", err)
|
||||||
@@ -56,13 +62,16 @@ func channelsSynchronize(client *LndClient) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if lastSync.Add(5 * time.Minute).Before(time.Now()) {
|
if lastSync.Add(5 * time.Minute).Before(time.Now()) {
|
||||||
<-time.After(30 * time.Second)
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-time.After(1 * time.Minute):
|
||||||
|
}
|
||||||
err = channelsSynchronizeOnce(client)
|
err = channelsSynchronizeOnce(client)
|
||||||
lastSync = time.Now()
|
lastSync = time.Now()
|
||||||
log.Printf("channelsSynchronizeOnce() err: %v", err)
|
log.Printf("channelsSynchronizeOnce() err: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cancel()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,11 +108,18 @@ func channelsSynchronizeOnce(client *LndClient) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func forwardingHistorySynchronize(client *LndClient) {
|
func forwardingHistorySynchronize(ctx context.Context, client *LndClient) {
|
||||||
for {
|
for {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err := forwardingHistorySynchronizeOnce(client)
|
err := forwardingHistorySynchronizeOnce(client)
|
||||||
log.Printf("forwardingHistorySynchronizeOnce() err: %v", err)
|
log.Printf("forwardingHistorySynchronizeOnce() err: %v", err)
|
||||||
<-time.After(1 * time.Minute)
|
select {
|
||||||
|
case <-time.After(1 * time.Minute):
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -153,12 +152,20 @@ func (c *LndLspNode) Start() {
|
|||||||
Name: fmt.Sprintf("%s: cmd", c.lspBase.name),
|
Name: fmt.Sprintf("%s: cmd", c.lspBase.name),
|
||||||
Fn: func() error {
|
Fn: func() error {
|
||||||
proc := cmd.Process
|
proc := cmd.Process
|
||||||
if proc != nil {
|
if proc == nil {
|
||||||
if runtime.GOOS == "windows" {
|
return nil
|
||||||
return proc.Signal(os.Kill)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return proc.Signal(os.Interrupt)
|
proc.Kill()
|
||||||
|
|
||||||
|
log.Printf("About to wait for lspd to exit")
|
||||||
|
status, err := proc.Wait()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("waiting for lspd process error: %v, status: %v", err, status)
|
||||||
|
}
|
||||||
|
err = cmd.Wait()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("waiting for lspd cmd error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -15,9 +15,10 @@ import (
|
|||||||
|
|
||||||
type LndHtlcInterceptor struct {
|
type LndHtlcInterceptor struct {
|
||||||
client *LndClient
|
client *LndClient
|
||||||
stopRequested bool
|
|
||||||
initWg sync.WaitGroup
|
initWg sync.WaitGroup
|
||||||
doneWg sync.WaitGroup
|
doneWg sync.WaitGroup
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLndHtlcInterceptor() *LndHtlcInterceptor {
|
func NewLndHtlcInterceptor() *LndHtlcInterceptor {
|
||||||
@@ -31,14 +32,18 @@ func NewLndHtlcInterceptor() *LndHtlcInterceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *LndHtlcInterceptor) Start() error {
|
func (i *LndHtlcInterceptor) Start() error {
|
||||||
go forwardingHistorySynchronize(i.client)
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
go channelsSynchronize(i.client)
|
i.ctx = ctx
|
||||||
i.initWg.Done()
|
i.cancel = cancel
|
||||||
|
go forwardingHistorySynchronize(ctx, i.client)
|
||||||
|
go channelsSynchronize(ctx, i.client)
|
||||||
|
|
||||||
return i.intercept()
|
return i.intercept()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *LndHtlcInterceptor) Stop() error {
|
func (i *LndHtlcInterceptor) Stop() error {
|
||||||
i.stopRequested = true
|
i.cancel()
|
||||||
|
i.doneWg.Wait()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,44 +53,53 @@ func (i *LndHtlcInterceptor) WaitStarted() LightningClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *LndHtlcInterceptor) intercept() error {
|
func (i *LndHtlcInterceptor) intercept() error {
|
||||||
|
inited := false
|
||||||
defer func() {
|
defer func() {
|
||||||
|
if !inited {
|
||||||
|
i.initWg.Done()
|
||||||
|
}
|
||||||
log.Printf("LND intercept(): stopping. Waiting for in-progress interceptions to complete.")
|
log.Printf("LND intercept(): stopping. Waiting for in-progress interceptions to complete.")
|
||||||
i.doneWg.Wait()
|
i.doneWg.Wait()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if i.stopRequested {
|
if i.ctx.Err() != nil {
|
||||||
return nil
|
return i.ctx.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Connecting LND HTLC interceptor.")
|
log.Printf("Connecting LND HTLC interceptor.")
|
||||||
cancellableCtx, cancel := context.WithCancel(context.Background())
|
interceptorClient, err := i.client.routerClient.HtlcInterceptor(i.ctx)
|
||||||
interceptorClient, err := i.client.routerClient.HtlcInterceptor(cancellableCtx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("routerClient.HtlcInterceptor(): %v", err)
|
log.Printf("routerClient.HtlcInterceptor(): %v", err)
|
||||||
cancel()
|
|
||||||
<-time.After(time.Second)
|
<-time.After(time.Second)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if i.stopRequested {
|
if i.ctx.Err() != nil {
|
||||||
cancel()
|
return i.ctx.Err()
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !inited {
|
||||||
|
inited = true
|
||||||
|
i.initWg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
request, err := interceptorClient.Recv()
|
request, err := interceptorClient.Recv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If it is just the error result of the context cancellation
|
// If it is just the error result of the context cancellation
|
||||||
// the we exit silently.
|
// the we exit silently.
|
||||||
status, ok := status.FromError(err)
|
status, ok := status.FromError(err)
|
||||||
if ok && status.Code() == codes.Canceled {
|
if ok && status.Code() == codes.Canceled {
|
||||||
|
log.Printf("Got code canceled. Break.")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise it an unexpected error, we fail the test.
|
// Otherwise it an unexpected error, we fail the test.
|
||||||
log.Printf("unexpected error in interceptor.Recv() %v", err)
|
log.Printf("unexpected error in interceptor.Recv() %v", err)
|
||||||
cancel()
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("htlc: %v\nchanID: %v\nincoming amount: %v\noutgoing amount: %v\nincomin expiry: %v\noutgoing expiry: %v\npaymentHash: %x\nonionBlob: %x\n\n",
|
fmt.Printf("htlc: %v\nchanID: %v\nincoming amount: %v\noutgoing amount: %v\nincomin expiry: %v\noutgoing expiry: %v\npaymentHash: %x\nonionBlob: %x\n\n",
|
||||||
request.IncomingCircuitKey.HtlcId,
|
request.IncomingCircuitKey.HtlcId,
|
||||||
request.IncomingCircuitKey.ChanId,
|
request.IncomingCircuitKey.ChanId,
|
||||||
@@ -131,7 +145,6 @@ func (i *LndHtlcInterceptor) intercept() error {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel()
|
|
||||||
<-time.After(time.Second)
|
<-time.After(time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
main.go
11
main.go
@@ -4,7 +4,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
)
|
)
|
||||||
@@ -86,6 +88,15 @@ func main() {
|
|||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
||||||
|
go func() {
|
||||||
|
sig := <-c
|
||||||
|
log.Printf("Received stop signal %v. Stopping.", sig)
|
||||||
|
s.Stop()
|
||||||
|
interceptor.Stop()
|
||||||
|
}()
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
log.Printf("lspd exited")
|
log.Printf("lspd exited")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user