From 0b954818d735f9390dc47ffa767f2d4bf156f091 Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Thu, 16 Jan 2025 10:20:08 +0100 Subject: [PATCH 1/4] remove unused redis connection --- removefunds.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/removefunds.go b/removefunds.go index 53e9f02..65903c8 100644 --- a/removefunds.go +++ b/removefunds.go @@ -39,9 +39,6 @@ func ensureOnChainPaymentSent(payReqHash string) (string, error) { } func sendCoinsForReceivedPayment(payReqHash string) (string, error) { - redisConn := redisPool.Get() - defer redisConn.Close() - removeFundRequest, err := getKeyFields(payReqHash) if err != nil { log.Printf("error querying payment request hash, %v", err) From 77ea577781beebfc8689a74c4eb7388c45903ece Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Thu, 16 Jan 2025 10:23:51 +0100 Subject: [PATCH 2/4] remove overlapping redis connection --- transactions.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/transactions.go b/transactions.go index c8fa7ae..a42bf00 100644 --- a/transactions.go +++ b/transactions.go @@ -160,20 +160,29 @@ func handleTransaction(tx *lnrpc.Transaction) error { } func registerTransacionConfirmation(txID, token, notifyType string) error { + registrationKey, err := doRegisterTransacionConfirmation(txID, token, notifyType) + if err != nil { + return err + } + err = setKeyExpiration(registrationKey, transactionNotificationExpiry) + return err +} + +func doRegisterTransacionConfirmation(txID, token, notifyType string) (string, error) { redisConn := redisPool.Get() defer redisConn.Close() registrationKey := fmt.Sprintf("tx-notify-%v", txID) registrationData := map[string]string{"token": token, "type": notifyType} marshalled, err := json.Marshal(registrationData) if err != nil { - return err + return "", err } _, err = redisConn.Do("SADD", registrationKey, string(marshalled)) if err != nil { - return err + return "", err } - err = setKeyExpiration(registrationKey, transactionNotificationExpiry) - return err + + return registrationKey, nil } func handleTransactionNotifications(tx *lnrpc.Transaction) error { From e23280572665565a66ea4d955a4272d041c482cf Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Thu, 16 Jan 2025 10:27:55 +0100 Subject: [PATCH 3/4] remove redundant redis connection --- transactions.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/transactions.go b/transactions.go index a42bf00..f2acba4 100644 --- a/transactions.go +++ b/transactions.go @@ -238,15 +238,13 @@ func handleTransactionAddreses(tx *lnrpc.Transaction) error { } if n > 0 { if tx.NumConfirmations > 0 { - err = handleTransactionAddress(tx, i) + err = handleTransactionAddress(tx, i, redisConn) if err != nil { return err } go notifyClientTransaction(tx, i, "Action Required", "Breez", "Received funds are now confirmed. Please open the app to complete your transaction.", true) break // There is only one address concerning us per transaction } else { - redisConn := redisPool.Get() - defer redisConn.Close() _, err := redisConn.Do("HMSET", "input-address:"+tx.DestAddresses[i], "utx:TxHash", tx.TxHash, "utx:Amount", tx.Amount, @@ -308,9 +306,7 @@ func notifyClientTransaction(tx *lnrpc.Transaction, index int, msg, title, body }) } -func handleTransactionAddress(tx *lnrpc.Transaction, index int) error { - redisConn := redisPool.Get() - defer redisConn.Close() +func handleTransactionAddress(tx *lnrpc.Transaction, index int, redisConn redis.Conn) error { _, err := redisConn.Do("HMSET", "input-address:"+tx.DestAddresses[index], "tx:TxHash", tx.TxHash, "tx:Amount", tx.Amount, From a6353138d4d8a043910138eda0f3138ebe03e191 Mon Sep 17 00:00:00 2001 From: Jesse de Wit Date: Thu, 16 Jan 2025 10:36:22 +0100 Subject: [PATCH 4/4] don't hold redis connection during firebase request --- transactions.go | 68 ++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/transactions.go b/transactions.go index f2acba4..3093adc 100644 --- a/transactions.go +++ b/transactions.go @@ -265,47 +265,63 @@ func handleTransactionAddreses(tx *lnrpc.Transaction) error { func notifyClientTransaction(tx *lnrpc.Transaction, index int, msg, title, body string, delete bool) { key := tx.TxHash + "-notification" _, _, _ = txNotificationGroup.Do(key, func() (interface{}, error) { - redisConn := redisPool.Get() - defer redisConn.Close() - tokens, err := redis.Strings(redisConn.Do("SMEMBERS", "input-address-notification:"+tx.DestAddresses[index])) + tokens, data, err := getTxNotificationData(tx, index, msg) if err != nil { - log.Println("notifyUnconfirmed error:", err) return nil, nil } - data := map[string]string{ - "msg": msg, - "tx": tx.TxHash, - "address": tx.DestAddresses[index], - "value": strconv.FormatInt(tx.Amount, 10), - } for _, tok := range tokens { err = notifyAlertMessage(title, body, data, tok) log.Println("Error in send:", err) unregistered := err != nil && isUnregisteredError(err) if unregistered || delete { - _, err = redisConn.Do("SREM", "input-address-notification:"+tx.DestAddresses[index], tok) - if err != nil { - log.Printf("Error in notifyClientTransaction (SREM); set:%v member:%v error:%v", "input-address-notification:"+tx.DestAddresses[index], tok, err) - } - - card, err := redis.Int(redisConn.Do("SCARD", "input-address-notification:"+tx.DestAddresses[index])) - if err != nil { - log.Printf("Error in notifyClientTransaction (SCARD); set:%v error:%v", "input-address-notification:"+tx.DestAddresses[index], err) - } else { - if card == 0 { - _, err = redisConn.Do("DEL", "input-address-notification:"+tx.DestAddresses[index]) - if err != nil { - log.Printf("Error in notifyClientTransaction (DEL); set:%v error:%v", "input-address-notification:"+tx.DestAddresses[index], err) - } - } - } + unregisterTxNotification(tx, index, tok) } } return nil, nil }) } +func getTxNotificationData(tx *lnrpc.Transaction, index int, msg string) ([]string, map[string]string, error) { + redisConn := redisPool.Get() + defer redisConn.Close() + tokens, err := redis.Strings(redisConn.Do("SMEMBERS", "input-address-notification:"+tx.DestAddresses[index])) + if err != nil { + log.Println("notifyUnconfirmed error:", err) + return nil, nil, err + } + data := map[string]string{ + "msg": msg, + "tx": tx.TxHash, + "address": tx.DestAddresses[index], + "value": strconv.FormatInt(tx.Amount, 10), + } + + return tokens, data, nil +} + +func unregisterTxNotification(tx *lnrpc.Transaction, index int, tok string) { + redisConn := redisPool.Get() + defer redisConn.Close() + + _, err := redisConn.Do("SREM", "input-address-notification:"+tx.DestAddresses[index], tok) + if err != nil { + log.Printf("Error in notifyClientTransaction (SREM); set:%v member:%v error:%v", "input-address-notification:"+tx.DestAddresses[index], tok, err) + } + + card, err := redis.Int(redisConn.Do("SCARD", "input-address-notification:"+tx.DestAddresses[index])) + if err != nil { + log.Printf("Error in notifyClientTransaction (SCARD); set:%v error:%v", "input-address-notification:"+tx.DestAddresses[index], err) + } else { + if card == 0 { + _, err = redisConn.Do("DEL", "input-address-notification:"+tx.DestAddresses[index]) + if err != nil { + log.Printf("Error in notifyClientTransaction (DEL); set:%v error:%v", "input-address-notification:"+tx.DestAddresses[index], err) + } + } + } +} + func handleTransactionAddress(tx *lnrpc.Transaction, index int, redisConn redis.Conn) error { _, err := redisConn.Do("HMSET", "input-address:"+tx.DestAddresses[index], "tx:TxHash", tx.TxHash,