github.com/andrewrech/ih-abstract@v0.0.0-20210322142951-2fec1c8d0f38/utils.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	_ "github.com/davecgh/go-spew/spew"
     8  )
     9  
    10  // count counts processed lines per unit time.
    11  func count(counter *int64, descr string, signal chan struct{}) {
    12  	second := make(chan struct{})
    13  
    14  	counterInterval := 500000000 // nanoseconds
    15  	t := time.Duration(counterInterval)
    16  
    17  	go func() {
    18  		for {
    19  			time.Sleep(t)
    20  			second <- struct{}{}
    21  		}
    22  	}()
    23  
    24  	go func() {
    25  		for {
    26  			select {
    27  			case <-signal:
    28  				return
    29  			case <-second:
    30  				log.Println(descr, *counter, "records")
    31  			}
    32  		}
    33  	}()
    34  }
    35  
    36  // splitCh splits a []string channel into two channels, sending results from the input channel onto both output channels
    37  func splitCh(in chan []string) (out1 chan []string, out2 chan []string, done chan struct{}) {
    38  	var buf int64 = 2e7
    39  
    40  	out1 = make(chan []string, buf)
    41  	out2 = make(chan []string, buf)
    42  	done = make(chan struct{})
    43  
    44  	go func() {
    45  		for l := range in {
    46  			out1 <- l
    47  			out2 <- l
    48  		}
    49  
    50  		close(out1)
    51  		close(out2)
    52  		done <- struct{}{}
    53  	}()
    54  
    55  	return
    56  }