github.com/tursom/GoCollections@v0.3.10/concurrent/util/Pipeline.go (about)

     1  /*
     2   * Copyright (c) 2022 tursom. All rights reserved.
     3   * Use of this source code is governed by a GPL-3
     4   * license that can be found in the LICENSE file.
     5   */
     6  
     7  package util
     8  
     9  import (
    10  	"github.com/tursom/GoCollections/concurrent/collections"
    11  	"github.com/tursom/GoCollections/lang"
    12  )
    13  
    14  type (
    15  	pipeline[T, R any] struct {
    16  		value  T
    17  		sender collections.SequenceSender[R]
    18  	}
    19  )
    20  
    21  func NewPipeline[T, R any](producer <-chan T, concurrency int, consumer func(T) R) lang.ReceiveChannel[R] {
    22  	var sequence collections.Sequence[R]
    23  	c := lang.NewChannel[pipeline[T, R]](0)
    24  	go func() {
    25  		defer close(c)
    26  		for value := range producer {
    27  			c.Send(pipeline[T, R]{
    28  				value:  value,
    29  				sender: sequence.Alloc(),
    30  			})
    31  		}
    32  	}()
    33  
    34  	for i := 0; i < concurrency; i++ {
    35  		go func() {
    36  			defer sequence.Close()
    37  			for value := range c {
    38  				r := consumer(value.value)
    39  				value.sender.Send(r)
    40  			}
    41  		}()
    42  	}
    43  	return sequence.Channel()
    44  }