From 2bb893b47852b2afceb35e73c60b7eed177084fa Mon Sep 17 00:00:00 2001 From: blueblue Date: Thu, 7 Sep 2017 13:49:39 +0800 Subject: [PATCH] rafthttp: add remote in pipeline and snapshot handler when corresponding peer or remote do not exist Fixes: #8506 --- rafthttp/http.go | 12 ++---------- rafthttp/util.go | 11 +++++++++++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/rafthttp/http.go b/rafthttp/http.go index 55a31f5d2..ea1b5fc6d 100644 --- a/rafthttp/http.go +++ b/rafthttp/http.go @@ -91,11 +91,7 @@ func (h *pipelineHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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 // 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 } - 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} // let snapshots be very large since they can exceed 512MB for large installations diff --git a/rafthttp/util.go b/rafthttp/util.go index 12e548c77..2f78415fe 100644 --- a/rafthttp/util.go +++ b/rafthttp/util.go @@ -175,3 +175,14 @@ func setPeerURLsHeader(req *http.Request, urls types.URLs) { } req.Header.Set("X-PeerURLs", strings.Join(peerURLs, ",")) } + +// addRemoteFromRequest add remote according to request header +func addRemoteFromRequest(tr Transporter, r *http.Request) bool { + 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, ",")) + return true + } + } + return false +}