Browse Source

rafthttp: limit the data size read from connection each time

Yicheng Qin 11 years ago
parent
commit
1e797c1e38
1 changed files with 9 additions and 1 deletions
  1. 9 1
      rafthttp/http.go

+ 9 - 1
rafthttp/http.go

@@ -17,6 +17,7 @@
 package rafthttp
 
 import (
+	"io"
 	"io/ioutil"
 	"log"
 	"net/http"
@@ -30,6 +31,10 @@ import (
 	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
 )
 
+const (
+	ConnReadLimitByte = 64 * 1024
+)
+
 var (
 	RaftPrefix       = "/raft"
 	RaftStreamPrefix = path.Join(RaftPrefix, "stream")
@@ -83,7 +88,10 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	b, err := ioutil.ReadAll(r.Body)
+	// 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 block in underlying implementation.
+	limitedr := io.LimitReader(r.Body, ConnReadLimitByte)
+	b, err := ioutil.ReadAll(limitedr)
 	if err != nil {
 		log.Println("rafthttp: error reading raft message:", err)
 		http.Error(w, "error reading raft message", http.StatusBadRequest)