Browse Source

rafthttp: make fields in Transport private

Xiang Li 11 years ago
parent
commit
a14d13f724
3 changed files with 30 additions and 29 deletions
  1. 2 9
      etcdserver/server.go
  2. 24 16
      rafthttp/transport.go
  3. 4 4
      rafthttp/transport_test.go

+ 2 - 9
etcdserver/server.go

@@ -271,15 +271,8 @@ func NewServer(cfg *ServerConfig) (*EtcdServer, error) {
 		SyncTicker:  time.Tick(500 * time.Millisecond),
 		snapCount:   cfg.SnapCount,
 	}
-	tr := &rafthttp.Transport{
-		RoundTripper: cfg.Transport,
-		ID:           id,
-		ClusterID:    cfg.Cluster.ID(),
-		Raft:         srv,
-		ServerStats:  sstats,
-		LeaderStats:  lstats,
-	}
-	tr.Start()
+
+	tr := rafthttp.NewTransporter(cfg.Transport, id, cfg.Cluster.ID(), srv, sstats, lstats)
 	// add all the remote members into sendhub
 	for _, m := range cfg.Cluster.Members() {
 		if m.Name != cfg.Name {

+ 24 - 16
rafthttp/transport.go

@@ -33,26 +33,34 @@ type Transporter interface {
 }
 
 type Transport struct {
-	RoundTripper http.RoundTripper
-	ID           types.ID
-	ClusterID    types.ID
-	Raft         Raft
-	ServerStats  *stats.ServerStats
-	LeaderStats  *stats.LeaderStats
+	roundTripper http.RoundTripper
+	id           types.ID
+	clusterID    types.ID
+	raft         Raft
+	serverStats  *stats.ServerStats
+	leaderStats  *stats.LeaderStats
 
 	mu         sync.RWMutex       // protect the peer map
 	peers      map[types.ID]*peer // remote peers
 	shouldstop chan struct{}
 }
 
-func (t *Transport) Start() {
-	t.peers = make(map[types.ID]*peer)
-	t.shouldstop = make(chan struct{}, 1)
+func NewTransporter(rt http.RoundTripper, id, cid types.ID, r Raft, ss *stats.ServerStats, ls *stats.LeaderStats) Transporter {
+	return &Transport{
+		roundTripper: rt,
+		id:           id,
+		clusterID:    cid,
+		raft:         r,
+		serverStats:  ss,
+		leaderStats:  ls,
+		peers:        make(map[types.ID]*peer),
+		shouldstop:   make(chan struct{}, 1),
+	}
 }
 
 func (t *Transport) Handler() http.Handler {
-	h := NewHandler(t.Raft, t.ClusterID)
-	sh := NewStreamHandler(t, t.ID, t.ClusterID)
+	h := NewHandler(t.raft, t.clusterID)
+	sh := NewStreamHandler(t, t.id, t.clusterID)
 	mux := http.NewServeMux()
 	mux.Handle(RaftPrefix, h)
 	mux.Handle(RaftStreamPrefix+"/", sh)
@@ -79,7 +87,7 @@ func (t *Transport) Send(msgs []raftpb.Message) {
 		}
 
 		if m.Type == raftpb.MsgApp {
-			t.ServerStats.SendAppendReq(m.Size())
+			t.serverStats.SendAppendReq(m.Size())
 		}
 
 		p.Send(m)
@@ -90,7 +98,7 @@ func (t *Transport) Stop() {
 	for _, p := range t.peers {
 		p.Stop()
 	}
-	if tr, ok := t.RoundTripper.(*http.Transport); ok {
+	if tr, ok := t.roundTripper.(*http.Transport); ok {
 		tr.CloseIdleConnections()
 	}
 }
@@ -112,9 +120,9 @@ func (t *Transport) AddPeer(id types.ID, urls []string) {
 		log.Panicf("unexpect peer url %s", peerURL)
 	}
 	u.Path = path.Join(u.Path, raftPrefix)
-	fs := t.LeaderStats.Follower(id.String())
-	t.peers[id] = NewPeer(t.RoundTripper, u.String(), id, t.ClusterID,
-		t.Raft, fs, t.shouldstop)
+	fs := t.leaderStats.Follower(id.String())
+	t.peers[id] = NewPeer(t.roundTripper, u.String(), id, t.clusterID,
+		t.raft, fs, t.shouldstop)
 }
 
 func (t *Transport) RemovePeer(id types.ID) {

+ 4 - 4
rafthttp/transport_test.go

@@ -30,7 +30,7 @@ import (
 func TestTransportAdd(t *testing.T) {
 	ls := stats.NewLeaderStats("")
 	tr := &Transport{
-		LeaderStats: ls,
+		leaderStats: ls,
 	}
 	tr.Start()
 	tr.AddPeer(1, []string{"http://a"})
@@ -53,7 +53,7 @@ func TestTransportAdd(t *testing.T) {
 
 func TestTransportRemove(t *testing.T) {
 	tr := &Transport{
-		LeaderStats: stats.NewLeaderStats(""),
+		leaderStats: stats.NewLeaderStats(""),
 	}
 	tr.Start()
 	tr.AddPeer(1, []string{"http://a"})
@@ -66,8 +66,8 @@ func TestTransportRemove(t *testing.T) {
 
 func TestTransportShouldStop(t *testing.T) {
 	tr := &Transport{
-		RoundTripper: newRespRoundTripper(http.StatusForbidden, nil),
-		LeaderStats:  stats.NewLeaderStats(""),
+		roundTripper: newRespRoundTripper(http.StatusForbidden, nil),
+		leaderStats:  stats.NewLeaderStats(""),
 	}
 	tr.Start()
 	tr.AddPeer(1, []string{"http://a"})