github.com/turingchain2020/turingchain@v1.1.21/system/mempool/pipeline.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mempool
     6  
     7  import (
     8  	"sync"
     9  
    10  	"github.com/turingchain2020/turingchain/queue"
    11  )
    12  
    13  //pipeline 适用于 一个问题,分成很多步完成,每步的输出作为下一步的输入
    14  
    15  func step(done <-chan struct{}, in <-chan *queue.Message, cb func(*queue.Message) *queue.Message) <-chan *queue.Message {
    16  	out := make(chan *queue.Message)
    17  	go func() {
    18  		defer close(out)
    19  		for n := range in {
    20  			select {
    21  			case out <- cb(n):
    22  			case <-done:
    23  				return
    24  			}
    25  		}
    26  	}()
    27  	return out
    28  }
    29  
    30  func merge(done <-chan struct{}, cs []<-chan *queue.Message) <-chan *queue.Message {
    31  	var wg sync.WaitGroup
    32  	out := make(chan *queue.Message)
    33  	output := func(c <-chan *queue.Message) {
    34  		defer wg.Done()
    35  		for n := range c {
    36  			select {
    37  			case out <- n:
    38  			case <-done:
    39  				return
    40  			}
    41  		}
    42  	}
    43  	wg.Add(len(cs))
    44  	for _, c := range cs {
    45  		go output(c)
    46  	}
    47  	go func() {
    48  		wg.Wait()
    49  		close(out)
    50  	}()
    51  	return out
    52  }