并发读取file io.Reader
var sm sync.Map
var wg sync.WaitGroup
length := len(ckNode.master)
var w = make([]io.Writer, length, length)
var pr = make([]*io.PipeReader, length, length)
c := make(chan struct{}, length)
for i := 0; i < length; i++ {
wg.Add(1)
go func(index int) {
pr[index], w[index] = io.Pipe()
c <- struct{}{}
cid, err = ckNode.ipfs.Add(pr[index], shell.Pin(true))
sm.Store(cid, err)
wg.Done()
}(i)
}
// 写入通道全部启动才开始copy
for {
if len(c) == length {
break
}
}
mw := io.MultiWriter(w...) // 向mw统一写入
_, err = io.Copy(mw, file)
if err != nil {
return "", err
}
for i := 0; i < length; i++ {
if pw, ok := w[i].(*io.PipeWriter); ok {
pw.CloseWithError(err)
}
}
wg.Wait()
sm.Range(func(key, value interface{}) bool {
if value != nil {
err = value.(error)
return false
}
if cid != "" && cid != key.(string) {
err = errors.New("cid不一致出现错误!")
return false
}
cid = key.(string)
return true
})
评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果