Kaynağa Gözat

add http timeout

Xiang Li 12 yıl önce
ebeveyn
işleme
880cd71df5
2 değiştirilmiş dosya ile 25 ekleme ve 8 silme
  1. 19 1
      etcd.go
  2. 6 7
      transporter.go

+ 19 - 1
etcd.go

@@ -13,6 +13,7 @@ import (
 	"github.com/coreos/go-raft"
 	"github.com/coreos/go-raft"
 	"io/ioutil"
 	"io/ioutil"
 	"log"
 	"log"
+	"net"
 	"net/http"
 	"net/http"
 	"os"
 	"os"
 	"strings"
 	"strings"
@@ -89,6 +90,7 @@ const (
 const (
 const (
 	ELECTIONTIMTOUT  = 200 * time.Millisecond
 	ELECTIONTIMTOUT  = 200 * time.Millisecond
 	HEARTBEATTIMEOUT = 50 * time.Millisecond
 	HEARTBEATTIMEOUT = 50 * time.Millisecond
+	HTTPTIMEOUT      = time.Second
 )
 )
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------
@@ -263,12 +265,22 @@ func createTransporter(st int) transporter {
 
 
 	switch st {
 	switch st {
 	case HTTP:
 	case HTTP:
-		t.client = nil
+		t.https = false
+
+		tr := &http.Transport{
+			Dial: dialTimeout,
+		}
+
+		t.client = &http.Client{
+			Transport: tr,
+		}
 		return t
 		return t
 
 
 	case HTTPS:
 	case HTTPS:
 		fallthrough
 		fallthrough
 	case HTTPSANDVERIFY:
 	case HTTPSANDVERIFY:
+		t.https = true
+
 		tlsCert, err := tls.LoadX509KeyPair(serverCertFile, serverKeyFile)
 		tlsCert, err := tls.LoadX509KeyPair(serverCertFile, serverKeyFile)
 
 
 		if err != nil {
 		if err != nil {
@@ -280,6 +292,7 @@ func createTransporter(st int) transporter {
 				Certificates:       []tls.Certificate{tlsCert},
 				Certificates:       []tls.Certificate{tlsCert},
 				InsecureSkipVerify: true,
 				InsecureSkipVerify: true,
 			},
 			},
+			Dial:               dialTimeout,
 			DisableCompression: true,
 			DisableCompression: true,
 		}
 		}
 
 
@@ -291,6 +304,11 @@ func createTransporter(st int) transporter {
 	return transporter{}
 	return transporter{}
 }
 }
 
 
+// Dial with timeout
+func dialTimeout(network, addr string) (net.Conn, error) {
+	return net.DialTimeout(network, addr, HTTPTIMEOUT)
+}
+
 // Start to listen and response raft command
 // Start to listen and response raft command
 func startRaftTransport(port int, st int) {
 func startRaftTransport(port int, st int) {
 
 

+ 6 - 7
transporter.go

@@ -12,10 +12,9 @@ import (
 
 
 // Transporter layer for communication between raft nodes
 // Transporter layer for communication between raft nodes
 type transporter struct {
 type transporter struct {
-	name string
-	// If https is used for server internal communcation,
-	// we will have a http client. Or it will be nil.
 	client *http.Client
 	client *http.Client
+	// https
+	https bool
 }
 }
 
 
 // Sends AppendEntries RPCs to a peer when the server is the leader.
 // Sends AppendEntries RPCs to a peer when the server is the leader.
@@ -104,22 +103,22 @@ func (t transporter) GetLeaderClientAddress() string {
 // Send server side POST request
 // Send server side POST request
 func (t transporter) Post(path string, body io.Reader) (*http.Response, error) {
 func (t transporter) Post(path string, body io.Reader) (*http.Response, error) {
 
 
-	if t.client != nil {
+	if t.https {
 		resp, err := t.client.Post("https://"+path, "application/json", body)
 		resp, err := t.client.Post("https://"+path, "application/json", body)
 		return resp, err
 		return resp, err
 	} else {
 	} else {
-		resp, err := http.Post("http://"+path, "application/json", body)
+		resp, err := t.client.Post("http://"+path, "application/json", body)
 		return resp, err
 		return resp, err
 	}
 	}
 }
 }
 
 
 // Send server side GET request
 // Send server side GET request
 func (t transporter) Get(path string) (*http.Response, error) {
 func (t transporter) Get(path string) (*http.Response, error) {
-	if t.client != nil {
+	if t.https {
 		resp, err := t.client.Get("https://" + path)
 		resp, err := t.client.Get("https://" + path)
 		return resp, err
 		return resp, err
 	} else {
 	} else {
-		resp, err := http.Get("http://" + path)
+		resp, err := t.client.Get("http://" + path)
 		return resp, err
 		return resp, err
 	}
 	}
 }
 }