interleaver: Run producer and consumer in separate go routines.

This commit is contained in:
Sascha L. Teichmann 2014-08-24 19:27:16 +02:00
parent 82be84d449
commit fc50897d2d

View File

@ -49,17 +49,32 @@ func selectKeyEncoder(interleaved bool) common.KeyEncoder {
} }
func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) (err error) { func copyProducerToConsumer(producer BlockProducer, consumer BlockConsumer) (err error) {
var block Block
i := 0 blocks := make(chan *Block)
for { done := make(chan struct{})
if err = producer.Next(&block); err != nil { defer close(done)
if err == ErrNoMoreBlocks {
err = nil go func() {
break defer close(blocks)
for {
block := new(Block)
if err = producer.Next(block); err != nil {
if err != ErrNoMoreBlocks {
log.Printf("Reading failed: %s\n", err)
}
return
}
select {
case blocks <- block:
case <-done:
return
} }
return
} }
if err = consumer.Consume(&block); err != nil { }()
i := 0
for block := range blocks {
if err = consumer.Consume(block); err != nil {
return return
} }
i++ i++