mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-21 06:34:58 +01:00
add graceful shutdown context
This commit is contained in:
@@ -243,11 +243,6 @@ func (svc *LndhubService) InvoiceUpdateSubscription(ctx context.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
svc.Logger.Errorf("Error processing invoice update subscription: %v", err)
|
svc.Logger.Errorf("Error processing invoice update subscription: %v", err)
|
||||||
sentry.CaptureException(err)
|
sentry.CaptureException(err)
|
||||||
// TODO: close the stream somehoe before retrying?
|
|
||||||
// Wait 30 seconds and try to reconnect
|
|
||||||
// TODO: implement some backoff
|
|
||||||
time.Sleep(30 * time.Second)
|
|
||||||
invoiceSubscriptionStream, _ = svc.ConnectInvoiceSubscription(ctx)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,7 +251,7 @@ func (svc *LndhubService) InvoiceUpdateSubscription(ctx context.Context) error {
|
|||||||
// Processing open invoices here could cause a race condition:
|
// Processing open invoices here could cause a race condition:
|
||||||
// We could get this notification faster than we finish the AddInvoice call
|
// We could get this notification faster than we finish the AddInvoice call
|
||||||
if rawInvoice.State == lnrpc.Invoice_OPEN {
|
if rawInvoice.State == lnrpc.Invoice_OPEN {
|
||||||
svc.Logger.Infof("Invoice state is open. Ignoring update. r_hash:%v", hex.EncodeToString(rawInvoice.RHash))
|
svc.Logger.Debugf("Invoice state is open. Ignoring update. r_hash:%v", hex.EncodeToString(rawInvoice.RHash))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
27
main.go
27
main.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
cache "github.com/SporkHubr/echo-http-cache"
|
cache "github.com/SporkHubr/echo-http-cache"
|
||||||
@@ -169,28 +170,33 @@ func main() {
|
|||||||
docs.SwaggerInfo.Host = c.Host
|
docs.SwaggerInfo.Host = c.Host
|
||||||
e.GET("/swagger/*", echoSwagger.WrapHandler)
|
e.GET("/swagger/*", echoSwagger.WrapHandler)
|
||||||
|
|
||||||
|
var backgroundWg sync.WaitGroup
|
||||||
|
ctx, cancelBackgroundRoutines := context.WithCancel(context.Background())
|
||||||
// Subscribe to LND invoice updates in the background
|
// Subscribe to LND invoice updates in the background
|
||||||
go svc.InvoiceUpdateSubscription(context.Background())
|
backgroundWg.Add(1)
|
||||||
|
go func() {
|
||||||
|
err = svc.InvoiceUpdateSubscription(ctx)
|
||||||
|
if err != nil {
|
||||||
|
svc.Logger.Error(err)
|
||||||
|
}
|
||||||
|
backgroundWg.Done()
|
||||||
|
}()
|
||||||
|
|
||||||
// Check the status of all pending outgoing payments
|
// Check the status of all pending outgoing payments
|
||||||
// A goroutine will be spawned for each one
|
// A goroutine will be spawned for each one
|
||||||
err = svc.CheckAllPendingOutgoingPayments(context.Background())
|
err = svc.CheckAllPendingOutgoingPayments(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
svc.Logger.Error(err)
|
svc.Logger.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//Start webhook subscription
|
//Start webhook subscription
|
||||||
if svc.Config.WebhookUrl != "" {
|
if svc.Config.WebhookUrl != "" {
|
||||||
webhookCtx, cancelWebhook := context.WithCancel(context.Background())
|
go svc.StartWebhookSubscribtion(ctx, svc.Config.WebhookUrl)
|
||||||
go svc.StartWebhookSubscribtion(webhookCtx, svc.Config.WebhookUrl)
|
|
||||||
defer cancelWebhook()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if svc.Config.EnableGRPC {
|
if svc.Config.EnableGRPC {
|
||||||
//start grpc server
|
//start grpc server
|
||||||
grpcContext, grpcCancel := context.WithCancel(context.Background())
|
go svc.StartGrpcServer(ctx)
|
||||||
go svc.StartGrpcServer(grpcContext)
|
|
||||||
defer grpcCancel()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Start Prometheus server if necessary
|
//Start Prometheus server if necessary
|
||||||
@@ -233,7 +239,10 @@ func main() {
|
|||||||
e.Logger.Fatal(err)
|
e.Logger.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//cancel and wait for graceful shutdown of background routines
|
||||||
|
cancelBackgroundRoutines()
|
||||||
|
backgroundWg.Wait()
|
||||||
|
fmt.Println("LNDhub exiting gracefully. Goodbye.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func createRateLimitMiddleware(seconds int, burst int) echo.MiddlewareFunc {
|
func createRateLimitMiddleware(seconds int, burst int) echo.MiddlewareFunc {
|
||||||
|
|||||||
Reference in New Issue
Block a user