swarm: for loop on reader

This commit is contained in:
Anton Evangelatov 2018-04-22 13:28:16 +02:00
parent 09713314c7
commit 426bc4f26b
3 changed files with 19 additions and 3 deletions

View file

@ -574,7 +574,23 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *Request) {
contentType = typ
}
w.Header().Set("Content-Type", contentType)
http.ServeContent(w, &r.Request, "", time.Now(), reader)
var res []byte
var err error
for {
res, err = ioutil.ReadAll(reader)
if err != nil {
log.Error("handle.get", "ruid", r.ruid, "error", err)
time.Sleep(200 * time.Millisecond)
continue
}
log.Debug("handle.get.readall success", "ruid", r.ruid)
break
}
rdr := bytes.NewReader(res)
http.ServeContent(w, &r.Request, "", time.Now(), rdr)
case r.uri.Hash():
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)

View file

@ -131,7 +131,7 @@ type TreeChunker struct {
The chunks are not meant to be validated by the chunker when joining. This
is because it is left to the DPA to decide which sources are trusted.
*/
func TreeJoin(key Key, getter Getter, depth int) LazySectionReader {
func TreeJoin(key Key, getter Getter, depth int) *LazyChunkReader {
return NewTreeJoiner(NewJoinerParams(key, getter, depth, DefaultChunkSize)).Join()
}

View file

@ -84,7 +84,7 @@ func NewDPA(store ChunkStore, params *DPAParams) *DPA {
// Chunk retrieval blocks on netStore requests with a timeout so reader will
// report error if retrieval of chunks within requested range time out.
// It returns a reader with the chunk data and whether the content was encrypted
func (self *DPA) Retrieve(key Key) (reader LazySectionReader, isEncrypted bool) {
func (self *DPA) Retrieve(key Key) (reader *LazyChunkReader, isEncrypted bool) {
isEncrypted = len(key) > self.hashFunc().Size()
getter := NewHasherStore(self.ChunkStore, self.hashFunc, isEncrypted)
reader = TreeJoin(key, getter, 0)