add graceful shutdown context

This commit is contained in:
kiwiidb
2023-01-16 17:59:41 +01:00
parent 301b58b7a8
commit 9144117c32
2 changed files with 19 additions and 15 deletions

View File

@@ -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
}

27
main.go
View File

@@ -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 {