background routine: add context cancel support

This commit is contained in:
kiwiidb
2022-04-22 14:32:48 +02:00
parent 8dd8c14cd7
commit 624909484e
2 changed files with 31 additions and 24 deletions

View File

@@ -271,6 +271,7 @@ func (suite *WebSocketTestSuite) TestWebSocketMissingInvoice() {
func (suite *WebSocketTestSuite) TearDownSuite() { func (suite *WebSocketTestSuite) TearDownSuite() {
suite.invoiceUpdateSubCancelFn() suite.invoiceUpdateSubCancelFn()
suite.websocketServer.Close() suite.websocketServer.Close()
clearTable(suite.service, "invoices")
} }
func TestWebSocketSuite(t *testing.T) { func TestWebSocketSuite(t *testing.T) {

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"encoding/hex" "encoding/hex"
"fmt"
"strings" "strings"
"time" "time"
@@ -122,6 +123,10 @@ func (svc *LndhubService) InvoiceUpdateSubscription(ctx context.Context) error {
return err return err
} }
for { for {
select {
case <-ctx.Done():
return fmt.Errorf("Context was canceled")
default:
// receive the next invoice update // receive the next invoice update
rawInvoice, err := invoiceSubscriptionStream.Recv() rawInvoice, err := invoiceSubscriptionStream.Recv()
if err != nil { if err != nil {
@@ -150,4 +155,5 @@ func (svc *LndhubService) InvoiceUpdateSubscription(ctx context.Context) error {
sentry.CaptureException(processingError) sentry.CaptureException(processingError)
} }
} }
}
} }