Browse Source

Merge pull request #2108 from yichengq/305

rafthttp: write StatusOK before start streaming
Yicheng Qin 11 years ago
parent
commit
4b6fa2d24f
2 changed files with 6 additions and 6 deletions
  1. 5 4
      rafthttp/http.go
  2. 1 2
      rafthttp/streamer.go

+ 5 - 4
rafthttp/http.go

@@ -156,16 +156,17 @@ func (h *streamHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	w.WriteHeader(http.StatusOK)
-	w.(http.Flusher).Flush()
-
-	sw := newStreamWriter(w.(WriteFlusher), from, term)
+	sw := newStreamWriter(from, term)
 	err = p.attachStream(sw)
 	if err != nil {
 		log.Printf("rafthttp: %v", err)
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
+
+	w.WriteHeader(http.StatusOK)
+	w.(http.Flusher).Flush()
+	go sw.handle(w.(WriteFlusher))
 	<-sw.stopNotify()
 }
 

+ 1 - 2
rafthttp/streamer.go

@@ -162,14 +162,13 @@ type streamWriter struct {
 
 // newStreamServer starts and returns a new started stream server.
 // The caller should call stop when finished, to shut it down.
-func newStreamWriter(w WriteFlusher, to types.ID, term uint64) *streamWriter {
+func newStreamWriter(to types.ID, term uint64) *streamWriter {
 	s := &streamWriter{
 		to:   to,
 		term: term,
 		q:    make(chan []raftpb.Entry, streamBufSize),
 		done: make(chan struct{}),
 	}
-	go s.handle(w)
 	return s
 }