From 9144117c320dbad29b9fad6dbd8b9fbffb6a2c4c Mon Sep 17 00:00:00 2001 From: kiwiidb Date: Mon, 16 Jan 2023 17:59:41 +0100 Subject: [PATCH] add graceful shutdown context --- lib/service/invoicesubscription.go | 7 +------ main.go | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/service/invoicesubscription.go b/lib/service/invoicesubscription.go index 7440ffb..8545cc6 100644 --- a/lib/service/invoicesubscription.go +++ b/lib/service/invoicesubscription.go @@ -243,11 +243,6 @@ func (svc *LndhubService) InvoiceUpdateSubscription(ctx context.Context) error { if err != nil { svc.Logger.Errorf("Error processing invoice update subscription: %v", 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 } @@ -256,7 +251,7 @@ func (svc *LndhubService) InvoiceUpdateSubscription(ctx context.Context) error { // Processing open invoices here could cause a race condition: // We could get this notification faster than we finish the AddInvoice call 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 } diff --git a/main.go b/main.go index 5bbc7f4..c96e09e 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "os/signal" + "sync" "time" cache "github.com/SporkHubr/echo-http-cache" @@ -169,28 +170,33 @@ func main() { docs.SwaggerInfo.Host = c.Host e.GET("/swagger/*", echoSwagger.WrapHandler) + var backgroundWg sync.WaitGroup + ctx, cancelBackgroundRoutines := context.WithCancel(context.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 // A goroutine will be spawned for each one - err = svc.CheckAllPendingOutgoingPayments(context.Background()) + err = svc.CheckAllPendingOutgoingPayments(ctx) if err != nil { svc.Logger.Error(err) } //Start webhook subscription if svc.Config.WebhookUrl != "" { - webhookCtx, cancelWebhook := context.WithCancel(context.Background()) - go svc.StartWebhookSubscribtion(webhookCtx, svc.Config.WebhookUrl) - defer cancelWebhook() + go svc.StartWebhookSubscribtion(ctx, svc.Config.WebhookUrl) } if svc.Config.EnableGRPC { //start grpc server - grpcContext, grpcCancel := context.WithCancel(context.Background()) - go svc.StartGrpcServer(grpcContext) - defer grpcCancel() + go svc.StartGrpcServer(ctx) } //Start Prometheus server if necessary @@ -233,7 +239,10 @@ func main() { 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 {