mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-24 11:57:44 +08:00
Let's save log.Error for things the user can take action on. Moved all our diagnostics to log.Debug. We can ideally reduce them even further.
48 lines
929 B
Go
48 lines
929 B
Go
// package chunk implements streaming block splitters
|
|
package chunk
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/jbenet/go-ipfs/util"
|
|
)
|
|
|
|
var log = util.Logger("chunk")
|
|
|
|
var DefaultBlockSize = 1024 * 256
|
|
var DefaultSplitter = &SizeSplitter{Size: DefaultBlockSize}
|
|
|
|
type BlockSplitter interface {
|
|
Split(r io.Reader) chan []byte
|
|
}
|
|
|
|
type SizeSplitter struct {
|
|
Size int
|
|
}
|
|
|
|
func (ss *SizeSplitter) Split(r io.Reader) chan []byte {
|
|
out := make(chan []byte)
|
|
go func() {
|
|
defer close(out)
|
|
|
|
// all-chunks loop (keep creating chunks)
|
|
for {
|
|
// log.Infof("making chunk with size: %d", ss.Size)
|
|
chunk := make([]byte, ss.Size)
|
|
nread, err := io.ReadFull(r, chunk)
|
|
if nread > 0 {
|
|
// log.Infof("sending out chunk with size: %d", sofar)
|
|
out <- chunk[:nread]
|
|
}
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
return
|
|
}
|
|
if err != nil {
|
|
log.Debugf("Block split error: %s", err)
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
return out
|
|
}
|