浏览代码

Merge pull request #8513 from shenlanse/bug-fix

rafthttp: add remote in pipeline and snapshot handler
Anthony Romano 8 年之前
父节点
当前提交
846255b95e
共有 2 个文件被更改,包括 11 次插入10 次删除
  1. 2 10
      rafthttp/http.go
  2. 9 0
      rafthttp/util.go

+ 2 - 10
rafthttp/http.go

@@ -91,11 +91,7 @@ func (h *pipelineHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 		return
 	}
 	}
 
 
-	if from, err := types.IDFromString(r.Header.Get("X-Server-From")); err != nil {
-		if urls := r.Header.Get("X-PeerURLs"); urls != "" {
-			h.tr.AddRemote(from, strings.Split(urls, ","))
-		}
-	}
+	addRemoteFromRequest(h.tr, r)
 
 
 	// Limit the data size that could be read from the request body, which ensures that read from
 	// Limit the data size that could be read from the request body, which ensures that read from
 	// connection will not time out accidentally due to possible blocking in underlying implementation.
 	// connection will not time out accidentally due to possible blocking in underlying implementation.
@@ -176,11 +172,7 @@ func (h *snapshotHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 		return
 	}
 	}
 
 
-	if from, err := types.IDFromString(r.Header.Get("X-Server-From")); err != nil {
-		if urls := r.Header.Get("X-PeerURLs"); urls != "" {
-			h.tr.AddRemote(from, strings.Split(urls, ","))
-		}
-	}
+	addRemoteFromRequest(h.tr, r)
 
 
 	dec := &messageDecoder{r: r.Body}
 	dec := &messageDecoder{r: r.Body}
 	// let snapshots be very large since they can exceed 512MB for large installations
 	// let snapshots be very large since they can exceed 512MB for large installations

+ 9 - 0
rafthttp/util.go

@@ -175,3 +175,12 @@ func setPeerURLsHeader(req *http.Request, urls types.URLs) {
 	}
 	}
 	req.Header.Set("X-PeerURLs", strings.Join(peerURLs, ","))
 	req.Header.Set("X-PeerURLs", strings.Join(peerURLs, ","))
 }
 }
+
+// addRemoteFromRequest adds a remote peer according to an http request header
+func addRemoteFromRequest(tr Transporter, r *http.Request) {
+	if from, err := types.IDFromString(r.Header.Get("X-Server-From")); err == nil {
+		if urls := r.Header.Get("X-PeerURLs"); urls != "" {
+			tr.AddRemote(from, strings.Split(urls, ","))
+		}
+	}
+}