Browse Source

clean up trans.go

Xiang Li 12 years ago
parent
commit
8e48a20c85
3 changed files with 18 additions and 14 deletions
  1. 3 5
      etcd.go
  2. 15 7
      raft_handlers.go
  3. 0 2
      transporter.go

+ 3 - 5
etcd.go

@@ -320,7 +320,7 @@ func startRaft(tlsConfig TLSConfig) {
 					if len(machine) == 0 {
 					if len(machine) == 0 {
 						continue
 						continue
 					}
 					}
-					err = joinCluster(raftServer, machine)
+					err = joinCluster(raftServer, machine, tlsConfig.Scheme)
 					if err != nil {
 					if err != nil {
 						if err.Error() == errors[103] {
 						if err.Error() == errors[103] {
 							fmt.Println(err)
 							fmt.Println(err)
@@ -367,8 +367,6 @@ func startRaft(tlsConfig TLSConfig) {
 func newTransporter(scheme string, tlsConf tls.Config) transporter {
 func newTransporter(scheme string, tlsConf tls.Config) transporter {
 	t := transporter{}
 	t := transporter{}
 
 
-	t.scheme = scheme
-
 	tr := &http.Transport{
 	tr := &http.Transport{
 		Dial: dialTimeout,
 		Dial: dialTimeout,
 	}
 	}
@@ -577,7 +575,7 @@ func newCertPool(CAFile string) (tls.ClientAuthType, *x509.CertPool) {
 }
 }
 
 
 // Send join requests to the leader.
 // Send join requests to the leader.
-func joinCluster(s *raft.Server, raftURL string) error {
+func joinCluster(s *raft.Server, raftURL string, scheme string) error {
 	var b bytes.Buffer
 	var b bytes.Buffer
 
 
 	command := &JoinCommand{
 	command := &JoinCommand{
@@ -595,7 +593,7 @@ func joinCluster(s *raft.Server, raftURL string) error {
 		panic("wrong type")
 		panic("wrong type")
 	}
 	}
 
 
-	joinURL := url.URL{Host: raftURL, Scheme: raftTransporter.scheme, Path: "/join"}
+	joinURL := url.URL{Host: raftURL, Scheme: scheme, Path: "/join"}
 
 
 	debugf("Send Join Request to %s", raftURL)
 	debugf("Send Join Request to %s", raftURL)
 
 

+ 15 - 7
raft_handlers.go

@@ -12,7 +12,8 @@ import (
 
 
 // Get all the current logs
 // Get all the current logs
 func GetLogHttpHandler(w http.ResponseWriter, req *http.Request) {
 func GetLogHttpHandler(w http.ResponseWriter, req *http.Request) {
-	debugf("[recv] GET %s/log", raftTransporter.scheme+raftServer.Name())
+	u, _ := nameToRaftURL(raftServer.Name())
+	debugf("[recv] GET %s/log", u)
 	w.Header().Set("Content-Type", "application/json")
 	w.Header().Set("Content-Type", "application/json")
 	w.WriteHeader(http.StatusOK)
 	w.WriteHeader(http.StatusOK)
 	json.NewEncoder(w).Encode(raftServer.LogEntries())
 	json.NewEncoder(w).Encode(raftServer.LogEntries())
@@ -23,7 +24,8 @@ func VoteHttpHandler(w http.ResponseWriter, req *http.Request) {
 	rvreq := &raft.RequestVoteRequest{}
 	rvreq := &raft.RequestVoteRequest{}
 	err := decodeJsonRequest(req, rvreq)
 	err := decodeJsonRequest(req, rvreq)
 	if err == nil {
 	if err == nil {
-		debugf("[recv] POST %s/vote [%s]", raftTransporter.scheme+raftServer.Name(), rvreq.CandidateName)
+		u, _ := nameToRaftURL(raftServer.Name())
+		debugf("[recv] POST %s/vote [%s]", u, rvreq.CandidateName)
 		if resp := raftServer.RequestVote(rvreq); resp != nil {
 		if resp := raftServer.RequestVote(rvreq); resp != nil {
 			w.WriteHeader(http.StatusOK)
 			w.WriteHeader(http.StatusOK)
 			json.NewEncoder(w).Encode(resp)
 			json.NewEncoder(w).Encode(resp)
@@ -40,7 +42,8 @@ func AppendEntriesHttpHandler(w http.ResponseWriter, req *http.Request) {
 	err := decodeJsonRequest(req, aereq)
 	err := decodeJsonRequest(req, aereq)
 
 
 	if err == nil {
 	if err == nil {
-		debugf("[recv] POST %s/log/append [%d]", raftTransporter.scheme+raftServer.Name(), len(aereq.Entries))
+		u, _ := nameToRaftURL(raftServer.Name())
+		debugf("[recv] POST %s/log/append [%d]", u, len(aereq.Entries))
 		if resp := raftServer.AppendEntries(aereq); resp != nil {
 		if resp := raftServer.AppendEntries(aereq); resp != nil {
 			w.WriteHeader(http.StatusOK)
 			w.WriteHeader(http.StatusOK)
 			json.NewEncoder(w).Encode(resp)
 			json.NewEncoder(w).Encode(resp)
@@ -59,7 +62,8 @@ func SnapshotHttpHandler(w http.ResponseWriter, req *http.Request) {
 	aereq := &raft.SnapshotRequest{}
 	aereq := &raft.SnapshotRequest{}
 	err := decodeJsonRequest(req, aereq)
 	err := decodeJsonRequest(req, aereq)
 	if err == nil {
 	if err == nil {
-		debugf("[recv] POST %s/snapshot/ ", raftTransporter.scheme+raftServer.Name())
+		u, _ := nameToRaftURL(raftServer.Name())
+		debugf("[recv] POST %s/snapshot/ ", u)
 		if resp := raftServer.RequestSnapshot(aereq); resp != nil {
 		if resp := raftServer.RequestSnapshot(aereq); resp != nil {
 			w.WriteHeader(http.StatusOK)
 			w.WriteHeader(http.StatusOK)
 			json.NewEncoder(w).Encode(resp)
 			json.NewEncoder(w).Encode(resp)
@@ -75,7 +79,8 @@ func SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *http.Request) {
 	aereq := &raft.SnapshotRecoveryRequest{}
 	aereq := &raft.SnapshotRecoveryRequest{}
 	err := decodeJsonRequest(req, aereq)
 	err := decodeJsonRequest(req, aereq)
 	if err == nil {
 	if err == nil {
-		debugf("[recv] POST %s/snapshotRecovery/ ", raftTransporter.scheme+raftServer.Name())
+		u, _ := nameToRaftURL(raftServer.Name())
+		debugf("[recv] POST %s/snapshotRecovery/ ", u)
 		if resp := raftServer.SnapshotRecoveryRequest(aereq); resp != nil {
 		if resp := raftServer.SnapshotRecoveryRequest(aereq); resp != nil {
 			w.WriteHeader(http.StatusOK)
 			w.WriteHeader(http.StatusOK)
 			json.NewEncoder(w).Encode(resp)
 			json.NewEncoder(w).Encode(resp)
@@ -88,7 +93,8 @@ func SnapshotRecoveryHttpHandler(w http.ResponseWriter, req *http.Request) {
 
 
 // Get the port that listening for etcd connecting of the server
 // Get the port that listening for etcd connecting of the server
 func EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) {
 func EtcdURLHttpHandler(w http.ResponseWriter, req *http.Request) {
-	debugf("[recv] Get %s/etcdURL/ ", raftTransporter.scheme+raftServer.Name())
+	u, _ := nameToRaftURL(raftServer.Name())
+	debugf("[recv] Get %s/etcdURL/ ", u)
 	w.WriteHeader(http.StatusOK)
 	w.WriteHeader(http.StatusOK)
 	w.Write([]byte(argInfo.EtcdURL))
 	w.Write([]byte(argInfo.EtcdURL))
 }
 }
@@ -109,7 +115,9 @@ func JoinHttpHandler(w http.ResponseWriter, req *http.Request) {
 
 
 // Response to the name request
 // Response to the name request
 func NameHttpHandler(w http.ResponseWriter, req *http.Request) {
 func NameHttpHandler(w http.ResponseWriter, req *http.Request) {
-	debugf("[recv] Get %s/name/ ", raftTransporter.scheme+raftServer.Name())
+	u, _ := nameToRaftURL(raftServer.Name())
+
+	debugf("[recv] Get %s/name/ ", u)
 	w.WriteHeader(http.StatusOK)
 	w.WriteHeader(http.StatusOK)
 	w.Write([]byte(raftServer.Name()))
 	w.Write([]byte(raftServer.Name()))
 }
 }

+ 0 - 2
transporter.go

@@ -12,8 +12,6 @@ import (
 // Transporter layer for communication between raft nodes
 // Transporter layer for communication between raft nodes
 type transporter struct {
 type transporter struct {
 	client *http.Client
 	client *http.Client
-	// scheme
-	scheme string
 }
 }
 
 
 // Sends AppendEntries RPCs to a peer when the server is the leader.
 // Sends AppendEntries RPCs to a peer when the server is the leader.