func readStream(host, dir, file string) (stream.Stream, error) { log.Println("RECOVER STREAM: Recovering file", file, "from", host) resp, err := http.Get(host + "/stream/" + file) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != 200 { return nil, errors.New(fmt.Sprint("Non successfully response from host: ", file, ": ", resp.StatusCode)) } path := filepath.Join(dir, file) out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0755) if err != nil { return nil, err } _, err = io.Copy(out, resp.Body) if err != nil { return nil, err } return stream.Open(path) }
func currentStream(r *cluster.Reader, streams map[uint64]stream.Stream, current uint64) stream.Stream { open.Lock() defer open.Unlock() if streams[current] == nil { s, err := stream.Open(r.Path(current)) if err != nil { log.Fatal(err) } streams[current] = s } return streams[current] }
func (r *Reader) retrieveStream(commit uint64, fetchMissing bool) (stream.Stream, error) { if commit == r.current { return r.stream, nil } r.mutex(commit).Lock() defer r.mutex(commit).Unlock() if r.streams[commit] == nil { var err error (func() { if r.streams[commit] == nil { var s stream.Stream var missing bool s, err = stream.Open(r.Path(commit)) if err != nil && strings.Contains(err.Error(), "no such file or directory") { missing = true } if s != nil && !s.Closed() { println("found open stream:", commit) missing = true s = nil } if missing && fetchMissing { s, err = RecoverStream(r.peers, r.dir, fmt.Sprintf("events.%024v.stream", commit)) } if err == nil { r.streams[commit] = s } } })() if err != nil { return nil, err } } return r.streams[commit], nil }
func (n *Node) recoverHandler(w http.ResponseWriter, req *http.Request) { file := strings.Replace(req.URL.Path, "/stream/", "", 1) path := filepath.Join(n.db.dir, file) s, err := stream.Open(path) if err != nil { w.WriteHeader(500) } else if !s.Closed() { w.WriteHeader(404) } else { if f, err := os.Open(path); err != nil { w.WriteHeader(500) } else { io.Copy(w, f) f.Close() } } }