mirror of
https://github.com/aljazceru/lspd.git
synced 2025-12-19 14:54:22 +01:00
A little prettier start and stop
This commit is contained in:
@@ -15,7 +15,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type LndHtlcInterceptor struct {
|
type LndHtlcInterceptor struct {
|
||||||
client *LndClient
|
client *LndClient
|
||||||
|
stopRequested bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLndHtlcInterceptor(client *LndClient) *LndHtlcInterceptor {
|
func NewLndHtlcInterceptor(client *LndClient) *LndHtlcInterceptor {
|
||||||
@@ -30,12 +31,16 @@ func (i *LndHtlcInterceptor) Start() error {
|
|||||||
return i.intercept()
|
return i.intercept()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *LndHtlcInterceptor) Stop() error {
|
func (i *LndHtlcInterceptor) Stop() {
|
||||||
return nil
|
i.stopRequested = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *LndHtlcInterceptor) intercept() error {
|
func (i *LndHtlcInterceptor) intercept() error {
|
||||||
for {
|
for {
|
||||||
|
if i.stopRequested {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
cancellableCtx, cancel := context.WithCancel(context.Background())
|
cancellableCtx, cancel := context.WithCancel(context.Background())
|
||||||
clientCtx := metadata.AppendToOutgoingContext(cancellableCtx, "macaroon", os.Getenv("LND_MACAROON_HEX"))
|
clientCtx := metadata.AppendToOutgoingContext(cancellableCtx, "macaroon", os.Getenv("LND_MACAROON_HEX"))
|
||||||
interceptorClient, err := client.routerClient.HtlcInterceptor(clientCtx)
|
interceptorClient, err := client.routerClient.HtlcInterceptor(clientCtx)
|
||||||
@@ -47,6 +52,9 @@ func (i *LndHtlcInterceptor) intercept() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
if i.stopRequested {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
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
|
||||||
|
|||||||
35
main.go
35
main.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
)
|
)
|
||||||
@@ -25,6 +26,7 @@ func main() {
|
|||||||
|
|
||||||
client = NewLndClient()
|
client = NewLndClient()
|
||||||
interceptor := NewLndHtlcInterceptor(client)
|
interceptor := NewLndHtlcInterceptor(client)
|
||||||
|
s := NewGrpcServer()
|
||||||
|
|
||||||
info, err := client.GetInfo()
|
info, err := client.GetInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -37,16 +39,35 @@ func main() {
|
|||||||
nodePubkey = info.Pubkey
|
nodePubkey = info.Pubkey
|
||||||
}
|
}
|
||||||
|
|
||||||
go interceptor.Start()
|
|
||||||
|
|
||||||
go forwardingHistorySynchronize(client)
|
go forwardingHistorySynchronize(client)
|
||||||
go channelsSynchronize(client)
|
go channelsSynchronize(client)
|
||||||
|
|
||||||
s := NewGrpcServer()
|
var wg sync.WaitGroup
|
||||||
err = s.Start()
|
wg.Add(2)
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("%v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
err := interceptor.Start()
|
||||||
|
if err == nil {
|
||||||
|
log.Printf("Interceptor stopped.")
|
||||||
|
} else {
|
||||||
|
log.Printf("FATAL. Interceptor stopped with error: %v", err)
|
||||||
|
}
|
||||||
|
s.Stop()
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
err := s.Start()
|
||||||
|
if err == nil {
|
||||||
|
log.Printf("GRPC server stopped.")
|
||||||
|
} else {
|
||||||
|
log.Printf("FATAL. GRPC server stopped with error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
interceptor.Stop()
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
log.Printf("lspd exited")
|
log.Printf("lspd exited")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user