mirror of
https://github.com/GMWalletApp/epusdt.git
synced 2026-07-07 18:26:16 +00:00
refactor: harden sqlite runtime and packaged startup flow
- refine config loading with --config and current-directory .env - improve sqlite busy handling and runtime DB tuning - restore telegram payment notification and wallet input flow - add behavior-based config and callback recovery tests
This commit is contained in:
@@ -29,7 +29,7 @@ func MysqlInit() error {
|
||||
// panic(err)
|
||||
return err
|
||||
}
|
||||
if config.AppDebug {
|
||||
if config.SQLDebug {
|
||||
Mdb = Mdb.Debug()
|
||||
}
|
||||
sqlDB, err := Mdb.DB()
|
||||
|
||||
@@ -40,7 +40,7 @@ func PostgreSQLInit() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if config.AppDebug {
|
||||
if config.SQLDebug {
|
||||
Mdb = Mdb.Debug()
|
||||
}
|
||||
sqlDB, err := Mdb.DB()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/assimon/luuu/config"
|
||||
@@ -16,9 +17,10 @@ import (
|
||||
// SqliteInit 数据库初始化
|
||||
func SqliteInit() error {
|
||||
var err error
|
||||
dbFilename := "./conf/.db"
|
||||
if dbfile := viper.GetString("sqlite_database_filename"); len(dbfile) > 0 {
|
||||
dbFilename = filepath.Base(dbfile)
|
||||
dbFilename := config.GetPrimarySqlitePath()
|
||||
if err = os.MkdirAll(filepath.Dir(dbFilename), 0o755); err != nil {
|
||||
color.Red.Printf("[store_db] sqlite mkdir err=%s\n", err)
|
||||
return err
|
||||
}
|
||||
color.Green.Printf("[store_db] sqlite filename: %s\n", dbFilename)
|
||||
Mdb, err = openDB(dbFilename, &gorm.Config{
|
||||
@@ -33,22 +35,11 @@ func SqliteInit() error {
|
||||
// panic(err)
|
||||
return err
|
||||
}
|
||||
if config.AppDebug {
|
||||
if config.SQLDebug {
|
||||
Mdb = Mdb.Debug()
|
||||
}
|
||||
sqlDB, err := Mdb.DB()
|
||||
if err != nil {
|
||||
color.Red.Printf("[store_db] sqlite get DB,err=%s\n", err)
|
||||
// panic(err)
|
||||
return err
|
||||
}
|
||||
// sqlDB.SetMaxIdleConns(viper.GetInt("sqlite_max_idle_conns"))
|
||||
// sqlDB.SetMaxOpenConns(viper.GetInt("sqlite_max_open_conns"))
|
||||
// sqlDB.SetConnMaxLifetime(time.Hour * time.Duration(viper.GetInt("sqlite_max_life_time")))
|
||||
err = sqlDB.Ping()
|
||||
if err != nil {
|
||||
if _, err = configureSQLite(Mdb, 1); err != nil {
|
||||
color.Red.Printf("[store_db] sqlite connDB err:%s", err.Error())
|
||||
// panic(err)
|
||||
return err
|
||||
}
|
||||
log.Sugar.Debug("[store_db] sqlite connDB success")
|
||||
|
||||
@@ -27,12 +27,6 @@ func RuntimeInit() error {
|
||||
return err
|
||||
}
|
||||
|
||||
sqlDB, err := RuntimeDB.DB()
|
||||
if err != nil {
|
||||
color.Red.Printf("[runtime_db] sqlite get DB,err=%s\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
concurrency := config.GetQueueConcurrency()
|
||||
if concurrency < 2 {
|
||||
concurrency = 2
|
||||
@@ -40,19 +34,7 @@ func RuntimeInit() error {
|
||||
if concurrency > 16 {
|
||||
concurrency = 16
|
||||
}
|
||||
sqlDB.SetMaxOpenConns(concurrency)
|
||||
sqlDB.SetMaxIdleConns(1)
|
||||
|
||||
if err = RuntimeDB.Exec("PRAGMA journal_mode=WAL;").Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err = RuntimeDB.Exec("PRAGMA synchronous=NORMAL;").Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err = RuntimeDB.Exec("PRAGMA busy_timeout=5000;").Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err = sqlDB.Ping(); err != nil {
|
||||
if _, err = configureSQLite(RuntimeDB, concurrency); err != nil {
|
||||
color.Red.Printf("[runtime_db] sqlite connDB err:%s", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func configureSQLite(db *gorm.DB, maxOpenConns int) (*sql.DB, error) {
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if maxOpenConns <= 0 {
|
||||
maxOpenConns = 1
|
||||
}
|
||||
sqlDB.SetMaxOpenConns(maxOpenConns)
|
||||
sqlDB.SetMaxIdleConns(1)
|
||||
|
||||
if err := db.Exec("PRAGMA journal_mode=WAL;").Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := db.Exec("PRAGMA synchronous=NORMAL;").Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := db.Exec("PRAGMA busy_timeout=5000;").Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := sqlDB.Ping(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return sqlDB, nil
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/assimon/luuu/model/dao"
|
||||
"github.com/assimon/luuu/model/mdb"
|
||||
"github.com/assimon/luuu/model/request"
|
||||
"github.com/dromara/carbon/v2"
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -15,6 +16,13 @@ import (
|
||||
|
||||
var ErrTransactionLocked = errors.New("transaction amount is already locked")
|
||||
|
||||
type PendingCallbackOrder struct {
|
||||
TradeId string `gorm:"column:trade_id"`
|
||||
CallbackNum int `gorm:"column:callback_num"`
|
||||
CallBackConfirm int `gorm:"column:callback_confirm"`
|
||||
UpdatedAt carbon.Time `gorm:"column:updated_at"`
|
||||
}
|
||||
|
||||
func normalizeLockAmount(amount float64) (int64, string) {
|
||||
value := decimal.NewFromFloat(amount).Round(2)
|
||||
return value.Shift(2).IntPart(), value.StringFixed(2)
|
||||
@@ -63,10 +71,11 @@ func OrderSuccessWithTransaction(tx *gorm.DB, req *request.OrderProcessingReques
|
||||
return result.RowsAffected > 0, result.Error
|
||||
}
|
||||
|
||||
// GetPendingCallbackOrders returns orders that still need callback delivery.
|
||||
func GetPendingCallbackOrders(maxRetry int, limit int) ([]mdb.Orders, error) {
|
||||
var orders []mdb.Orders
|
||||
// GetPendingCallbackOrders returns the minimal callback scheduling state.
|
||||
func GetPendingCallbackOrders(maxRetry int, limit int) ([]PendingCallbackOrder, error) {
|
||||
var orders []PendingCallbackOrder
|
||||
query := dao.Mdb.Model(&mdb.Orders{}).
|
||||
Select("trade_id", "callback_num", "callback_confirm", "updated_at").
|
||||
Where("callback_num <= ?", maxRetry).
|
||||
Where("callback_confirm = ?", mdb.CallBackConfirmNo).
|
||||
Where("status = ?", mdb.StatusPaySuccess).
|
||||
|
||||
@@ -16,7 +16,7 @@ func GetCheckoutCounterByTradeId(tradeId string) (*response.CheckoutCounterRespo
|
||||
return nil, err
|
||||
}
|
||||
if orderInfo.ID <= 0 || orderInfo.Status != mdb.StatusWaitPay {
|
||||
return nil, errors.New("pending order does not exist or has expired")
|
||||
return nil, errors.New("不存在待支付订单或已过期")
|
||||
}
|
||||
|
||||
resp := &response.CheckoutCounterResponse{
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
tron "github.com/assimon/luuu/crypto"
|
||||
"github.com/assimon/luuu/config"
|
||||
tron "github.com/assimon/luuu/crypto"
|
||||
"github.com/assimon/luuu/model/data"
|
||||
"github.com/assimon/luuu/model/mdb"
|
||||
"github.com/assimon/luuu/model/request"
|
||||
@@ -73,7 +73,14 @@ func checkTrxTransfers(address string, wg *sync.WaitGroup) {
|
||||
panic("TRX API response indicates failure")
|
||||
}
|
||||
|
||||
for i, transfer := range gjson.GetBytes(resp.Body(), "data").Array() {
|
||||
transfers := gjson.GetBytes(resp.Body(), "data").Array()
|
||||
if len(transfers) == 0 {
|
||||
log.Sugar.Debugf("[TRX][%s] no transfer records found", address)
|
||||
return
|
||||
}
|
||||
log.Sugar.Debugf("[TRX][%s] fetched %d transfer records", address, len(transfers))
|
||||
|
||||
for i, transfer := range transfers {
|
||||
if transfer.Get("raw_data.contract.0.type").String() != "TransferContract" {
|
||||
continue
|
||||
}
|
||||
@@ -108,8 +115,10 @@ func checkTrxTransfers(address string, wg *sync.WaitGroup) {
|
||||
panic(err)
|
||||
}
|
||||
if tradeID == "" {
|
||||
log.Sugar.Debugf("[TRX][%s] skip unmatched tx hash=%s amount=%.2f", address, txID, amount)
|
||||
continue
|
||||
}
|
||||
log.Sugar.Infof("[TRX][%s] matched trade_id=%s hash=%s amount=%.2f", address, tradeID, txID, amount)
|
||||
|
||||
order, err := data.GetOrderInfoByTradeId(tradeID)
|
||||
if err != nil {
|
||||
@@ -139,6 +148,7 @@ func checkTrxTransfers(address string, wg *sync.WaitGroup) {
|
||||
}
|
||||
|
||||
sendPaymentNotification(order)
|
||||
log.Sugar.Infof("[TRX][%s] payment processed trade_id=%s hash=%s", address, tradeID, txID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,7 +184,14 @@ func checkTrc20Transfers(address string, wg *sync.WaitGroup) {
|
||||
panic("TRC20 API response indicates failure")
|
||||
}
|
||||
|
||||
for i, transfer := range gjson.GetBytes(resp.Body(), "data").Array() {
|
||||
transfers := gjson.GetBytes(resp.Body(), "data").Array()
|
||||
if len(transfers) == 0 {
|
||||
log.Sugar.Debugf("[TRC20][%s] no transfer records found", address)
|
||||
return
|
||||
}
|
||||
log.Sugar.Debugf("[TRC20][%s] fetched %d transfer records", address, len(transfers))
|
||||
|
||||
for i, transfer := range transfers {
|
||||
if transfer.Get("token_info.address").String() != TRC20_USDT_ID {
|
||||
continue
|
||||
}
|
||||
@@ -200,8 +217,10 @@ func checkTrc20Transfers(address string, wg *sync.WaitGroup) {
|
||||
panic(err)
|
||||
}
|
||||
if tradeID == "" {
|
||||
log.Sugar.Debugf("[TRC20][%s] skip unmatched tx hash=%s amount=%.2f", address, txID, amount)
|
||||
continue
|
||||
}
|
||||
log.Sugar.Infof("[TRC20][%s] matched trade_id=%s hash=%s amount=%.2f", address, tradeID, txID, amount)
|
||||
|
||||
order, err := data.GetOrderInfoByTradeId(tradeID)
|
||||
if err != nil {
|
||||
@@ -231,18 +250,29 @@ func checkTrc20Transfers(address string, wg *sync.WaitGroup) {
|
||||
}
|
||||
|
||||
sendPaymentNotification(order)
|
||||
log.Sugar.Infof("[TRC20][%s] payment processed trade_id=%s hash=%s", address, tradeID, txID)
|
||||
}
|
||||
}
|
||||
|
||||
func sendPaymentNotification(order *mdb.Orders) {
|
||||
msg := fmt.Sprintf(
|
||||
"Payment received\nTrade ID: %s\nOrder ID: %s\nOrder Amount: %.2f %s\nReceived Amount: %.2f %s\nAddress: %s\nCreated At: %s\nPaid At: %s",
|
||||
order.TradeId,
|
||||
order.OrderId,
|
||||
"🎉 <b>收款成功通知</b>\n\n"+
|
||||
"💰 <b>金额信息</b>\n"+
|
||||
"├ 订单金额:<code>%.2f %s</code>\n"+
|
||||
"└ 实际到账:<code>%.2f %s</code>\n\n"+
|
||||
"📋 <b>订单信息</b>\n"+
|
||||
"├ 交易号:<code>%s</code>\n"+
|
||||
"├ 订单号:<code>%s</code>\n"+
|
||||
"└ 钱包地址:<code>%s</code>\n\n"+
|
||||
"⏰ <b>时间信息</b>\n"+
|
||||
"├ 创建时间:%s\n"+
|
||||
"└ 支付时间:%s",
|
||||
order.Amount,
|
||||
strings.ToUpper(order.Currency),
|
||||
order.ActualAmount,
|
||||
strings.ToUpper(order.Token),
|
||||
order.TradeId,
|
||||
order.OrderId,
|
||||
order.ReceiveAddress,
|
||||
order.CreatedAt.ToDateTimeString(),
|
||||
carbon.Now().ToDateTimeString(),
|
||||
|
||||
Reference in New Issue
Block a user