OSDN Git Service

update
authorHAOYUatHZ <haoyu@protonmail.com>
Sun, 16 Jun 2019 02:46:53 +0000 (10:46 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Sun, 16 Jun 2019 02:46:53 +0000 (10:46 +0800)
federation/warder.go

index 7d7d551..4acbccc 100644 (file)
@@ -36,7 +36,30 @@ func NewWarder(cfg *config.Config, db *gorm.DB, txCh chan *orm.CrossTransaction)
 
 func (w *warder) Run() {
        go w.collectPendingTx()
+       go w.processCrossTxRoutine()
+}
+
+func (w *warder) collectPendingTx() {
+       ticker := time.NewTicker(w.colletInterval)
+       for ; true; <-ticker.C {
+               txs := []*orm.CrossTransaction{}
+               if err := w.db.Preload("Chain").Preload("Reqs").
+                       // do not use "Where(&orm.CrossTransaction{Status: common.CrossTxPendingStatus})" directly
+                       // otherwise the field "status" is ignored
+                       Model(&orm.CrossTransaction{}).Where("status = ?", common.CrossTxPendingStatus).
+                       Find(&txs).Error; err == gorm.ErrRecordNotFound {
+                       continue
+               } else if err != nil {
+                       log.Warnln("collectPendingTx", err)
+               }
+
+               for _, tx := range txs {
+                       w.txCh <- tx
+               }
+       }
+}
 
+func (w *warder) processCrossTxRoutine() {
        for ormTx := range w.txCh {
                if err := w.validateCrossTx(ormTx); err != nil {
                        log.Warnln("invalid cross-chain tx", ormTx)
@@ -78,26 +101,6 @@ func (w *warder) Run() {
        }
 }
 
-func (w *warder) collectPendingTx() {
-       ticker := time.NewTicker(w.colletInterval)
-       for ; true; <-ticker.C {
-               txs := []*orm.CrossTransaction{}
-               if err := w.db.Preload("Chain").Preload("Reqs").
-                       // do not use "Where(&orm.CrossTransaction{Status: common.CrossTxPendingStatus})" directly
-                       // otherwise the field "status" is ignored
-                       Model(&orm.CrossTransaction{}).Where("status = ?", common.CrossTxPendingStatus).
-                       Find(&txs).Error; err == gorm.ErrRecordNotFound {
-                       continue
-               } else if err != nil {
-                       log.Warnln("collectPendingTx", err)
-               }
-
-               for _, tx := range txs {
-                       w.txCh <- tx
-               }
-       }
-}
-
 func (w *warder) validateCrossTx(tx *orm.CrossTransaction) error {
        switch tx.Status {
        case common.CrossTxRejectedStatus: