OSDN Git Service

Opt mathch collector (#482)
authorPoseidon <shenao.78@163.com>
Thu, 6 Feb 2020 11:21:34 +0000 (19:21 +0800)
committerGitHub <noreply@github.com>
Thu, 6 Feb 2020 11:21:34 +0000 (19:21 +0800)
* opt_mathch_collector

* opt_mathch_collector

application/mov/match_collector.go

index 824bdd3..e46dd50 100644 (file)
@@ -35,7 +35,7 @@ func newMatchTxCollector(engine *match.Engine, iterator *database.TradePairItera
                tradePairIterator: iterator,
                workerNum:         workerNum,
                workerNumCh:       make(chan int, workerNum),
-               processCh:         make(chan *matchTxResult, 32),
+               processCh:         make(chan *matchTxResult),
                tradePairCh:       make(chan *common.TradePair, workerNum),
                closeCh:           make(chan struct{}),
                gasLeft:           gasLeft,
@@ -63,15 +63,6 @@ func (m *matchCollector) collect() ([]*types.Tx, error) {
        defer close(m.closeCh)
 
        var matchedTxs []*types.Tx
-       appendMatchedTxs := func(data *matchTxResult) bool {
-               gasUsed := calcMatchedTxGasUsed(data.matchedTx)
-               if m.gasLeft -= gasUsed; m.gasLeft >= 0 {
-                       matchedTxs = append(matchedTxs, data.matchedTx)
-                       return false
-               }
-               return true
-       }
-
        completed := 0
        for !m.isTimeout() {
                select {
@@ -80,20 +71,14 @@ func (m *matchCollector) collect() ([]*types.Tx, error) {
                                return nil, data.err
                        }
 
-                       if done := appendMatchedTxs(data); done {
+                       gasUsed := calcMatchedTxGasUsed(data.matchedTx)
+                       if m.gasLeft -= gasUsed; m.gasLeft >= 0 {
+                               matchedTxs = append(matchedTxs, data.matchedTx)
+                       } else {
                                return matchedTxs, nil
                        }
                case <-m.workerNumCh:
                        if completed++; completed == m.workerNum {
-                               // read the remaining process results
-                               close(m.processCh)
-                               for data := range m.processCh {
-                                       if data.err != nil {
-                                               return nil, data.err
-                                       }
-
-                                       appendMatchedTxs(data)
-                               }
                                return matchedTxs, nil
                        }
                }